pgbot/pgbot

59 lines
1.5 KiB
Text
Raw Normal View History

2023-10-31 01:45:36 +02:00
#!/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(config['db_spec'])
return config, db_conn, client
def main():
config, db_conn, client = init(sys.argv[1:])
namegen = pgbotlib.misc.NameGenerator(config, db_conn)
responder = pgbotlib.response.Responder(config, client, db_conn, namegen)
2023-10-31 01:45:36 +02:00
commander = pgbotlib.commands.Commander(config, client, config['admins'],
db_conn, namegen, responder)
2023-10-31 01:45:36 +02:00
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):
if event.message.text.startswith('/'):
await commander.action(event)
else:
await responder.respond(event)
client.run_until_disconnected()
if __name__ == '__main__':
main()