pgbot/pgbot

58 lines
1.5 KiB
Python
Executable file

#!/usr/bin/env python3
import asyncio
import sys
import threading
import telethon
import yaml
import pgbotlib.defaults
import pgbotlib.dbstuff
import pgbotlib.commands
import pgbotlib.cron
import pgbotlib.misc
import pgbotlib.response
def init(args: list) -> tuple:
conf_path = args[0] if args else pgbotlib.defaults.CONFIG
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)
commander = pgbotlib.commands.Commander(config, client, config['admins'],
db_conn, namegen, responder)
@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)
cron = pgbotlib.cron.Cron(config, client, responder)
cron.plan()
loop = asyncio.get_event_loop()
loop.run_forever()
if __name__ == '__main__':
print("Starting pgbot...")
main()