twitools/telegrambot.py
2017-12-06 18:20:13 +01:00

46 lines
1.8 KiB
Python
Executable file

#!/usr/bin/env python3
import bottools.methods, logging, logging.handlers, os, setuptools, sys, telegram.ext
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.WARN)
logger = logging.getLogger(__name__)
logger.addHandler(logging.handlers.SysLogHandler(address='/dev/log'))
def log(bot, update, error):
logger.warn("Error %s caused by '%s'" % (error, update))
def shutdown(e = None, u = None, restart = False):
if e:
logger.warn(e)
if u:
u.stop()
if restart:
os.execl(sys.executable, sys.executable, *sys.argv)
else:
sys.exit(1 if e else 0)
if __name__ == "__main__":
try:
updater = telegram.ext.Updater(token=setuptools.token())
updater.dispatcher.add_error_handler(log)
for k, v in bottools.methods.commands.items():
updater.dispatcher.add_handler(telegram.ext.CommandHandler(k, v, pass_args = True if v in bottools.methods.pargs else False))
updater.dispatcher.add_handler(telegram.ext.RegexHandler("^@TweepBot \/.*", bottools.methods.mentionHelper))
for t in [telegram.ext.Filters.photo, telegram.ext.Filters.sticker, telegram.ext.Filters.video, telegram.ext.Filters.document]:
updater.dispatcher.add_handler(telegram.ext.MessageHandler(t, bottools.methods.captionHelper))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.location, bottools.methods.storeLocation))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.text, bottools.methods.tweet))
updater.dispatcher.add_handler(telegram.ext.MessageHandler(telegram.ext.Filters.command, bottools.methods.unknown))
updater.dispatcher.add_handler(telegram.ext.CallbackQueryHandler(bottools.methods.callback))
updater.start_polling()
updater.idle()
except Exception as e:
shutdown(e, updater, True)