2016-10-29 16:47:38 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from telegram.ext import Updater
|
2016-11-12 23:55:48 +01:00
|
|
|
|
2016-12-05 18:53:41 +01:00
|
|
|
from src.config import config, chat_purge_queue
|
2016-12-06 18:52:51 +01:00
|
|
|
from src.handler import *
|
2016-11-01 22:01:40 +01:00
|
|
|
|
2016-10-29 16:47:38 +02:00
|
|
|
|
|
|
|
class Bot:
|
2016-12-11 18:41:40 +01:00
|
|
|
"""
|
|
|
|
Main initializer and dispatcher of messages
|
|
|
|
"""
|
2016-11-12 14:56:42 +01:00
|
|
|
def __init__(self):
|
2016-10-29 16:47:38 +02:00
|
|
|
self.updater = Updater(token=config['bot']['token'])
|
|
|
|
self.dispatcher = self.updater.dispatcher
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
logging.info("Bot started")
|
2016-11-12 13:37:14 +01:00
|
|
|
|
2016-12-05 18:53:41 +01:00
|
|
|
self.dispatcher.add_handler(MessageHandler())
|
2016-12-04 14:13:04 +01:00
|
|
|
self.dispatcher.add_handler(CommandHandler())
|
2016-12-05 18:53:41 +01:00
|
|
|
self.dispatcher.add_handler(StatusHandler(chat_purge_queue.instance(self.updater.job_queue)))
|
2016-11-09 21:41:56 +01:00
|
|
|
|
2016-11-29 21:25:33 +01:00
|
|
|
if config['updates']['mode'] == 'polling':
|
|
|
|
self.updater.start_polling()
|
|
|
|
elif config['updates']['mode'] == 'webhook':
|
|
|
|
key = open(config['updates']['key'], 'rb') if config['updates']['key'] is not None else None
|
|
|
|
cert = open(config['updates']['cert'], 'rb') if config['updates']['cert'] is not None else None
|
|
|
|
|
|
|
|
self.updater.start_webhook(listen=config['updates']['host'],
|
|
|
|
port=config.getint('updates', 'port'),
|
|
|
|
url_path=config['bot']['token'],
|
|
|
|
key=key,
|
|
|
|
cert=cert,
|
|
|
|
webhook_url=config['updates']['url'])
|
|
|
|
|
2016-11-09 21:41:56 +01:00
|
|
|
self.updater.idle()
|