pgbot/pgbot
2023-10-31 01:45:36 +02:00

62 lines
1.7 KiB
Python
Executable file

#!/usr/bin/env python3
import sys
import threading
import telethon
import yaml
import pgbotlib.dbstuff
import pgbotlib.commands
import pgbotlib.misc
import pgbotlib.response
import pgbotlib.sched
def init(args: list) -> tuple:
conf_path = args[0] if args else 'config.yml'
try:
with open(conf_path, 'r', encoding='utf-8') as data:
config = yaml.safe_load(data.read())
except FileNotFoundError as err:
sys.exit(err)
client = telethon.TelegramClient(
'bot_session', config['api_id'],
config['api_hash']).start(bot_token=config['bot_token'])
# db_conn = pgbotlib.dbstuff.DBConn(
# f'dbname={config['db_name']} user={config['db_user']}')
db_conn = pgbotlib.dbstuff.DBConn(config['db_spec'])
return config, db_conn, client
def main():
config, db_conn, client = init(sys.argv[1:])
responder = pgbotlib.response.Responder(config, client, db_conn)
commander = pgbotlib.commands.Commander(config, client, config['admins'],
db_conn, responder)
sched_thread = threading.Thread(
target=pgbotlib.sched.spawn_scheduler,
args=(config, client, responder),
daemon=True)
sched_thread.start()
@client.on(telethon.events.NewMessage())
async def handle_new_message(event):
chat = await event.get_chat()
result = await client.get_messages(chat.id, ids=[event.message.reply_to.reply_to_msg_id])
print(result)
if event.message.text.startswith('/'):
await commander.action(event)
else:
await responder.respond(event)
client.run_until_disconnected()
if __name__ == '__main__':
main()