pgbot/pgbot

58 lines
1.5 KiB
Text
Raw Normal View History

2023-10-31 01:45:36 +02:00
#!/usr/bin/env python3
import asyncio
2023-10-31 01:45:36 +02:00
import sys
import threading
import telethon
import yaml
import pgbotlib.dbstuff
import pgbotlib.commands
import pgbotlib.cron
2023-10-31 01:45:36 +02:00
import pgbotlib.misc
import pgbotlib.response
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
@client.on(telethon.events.NewMessage())
async def handle_new_message(event):
if event.message.text.startswith('.'):
2023-10-31 01:45:36 +02:00
await commander.action(event)
else:
await responder.respond(event)
cron = pgbotlib.cron.Cron(config, client, responder)
cron.plan()
loop = asyncio.get_event_loop()
loop.run_forever()
2023-10-31 01:45:36 +02:00
if __name__ == '__main__':
print("Starting pgbot...")
2023-10-31 01:45:36 +02:00
main()