diff --git a/django_telegrambot/apps.py b/django_telegrambot/apps.py index 238859b..5890e83 100644 --- a/django_telegrambot/apps.py +++ b/django_telegrambot/apps.py @@ -10,12 +10,13 @@ import os.path import logging + logger = logging.getLogger(__name__) TELEGRAM_BOT_MODULE_NAME = 'telegrambot' -class DjangoTelegramBot(AppConfig): +class DjangoTelegramBot(AppConfig): name = 'django_telegrambot' verbose_name = 'Django TelegramBot' ready_run = False @@ -26,38 +27,40 @@ class DjangoTelegramBot(AppConfig): bots = [] @classmethod - def getDispatcher(cls, id = None, safe=True): - if id == None: + def getDispatcher(cls, bot_id=None, safe=True): + if bot_id is None: return cls.dispatchers[0] else: try: - index = cls.bot_tokens.index(id) + index = cls.bot_tokens.index(bot_id) except ValueError: - if not safe : return None + if not safe: + return None try: - index = cls.bot_usernames.index(id) + index = cls.bot_usernames.index(bot_id) except ValueError: return None return cls.dispatchers[index] @classmethod - def getBot(cls, id = None, safe = True): - if id == None: + def getBot(cls, bot_id=None, safe=True): + if bot_id is None: return cls.bots[0] else: try: - index = cls.bot_tokens.index(id) + index = cls.bot_tokens.index(bot_id) except ValueError: - if not safe : return None + if not safe: + return None try: - index = cls.bot_usernames.index(id) + index = cls.bot_usernames.index(bot_id) except ValueError: return None return cls.bots[index] - def ready(self): - if DjangoTelegramBot.ready_run: return + if DjangoTelegramBot.ready_run: + return DjangoTelegramBot.ready_run = True if not hasattr(settings, 'TELEGRAM_BOT_TOKENS'): @@ -77,13 +80,12 @@ def ready(self): use_certificate = False if hasattr(settings, 'TELEGRAM_WEBHOOK_CERTIFICATE'): - CERT = settings.TELEGRAM_WEBHOOK_CERTIFICATE - if(os.path.exists(CERT)): + cert = settings.TELEGRAM_WEBHOOK_CERTIFICATE + if os.path.exists(cert): use_certificate = True - logger.info('TELEGRAM_WEBHOOK_CERTIFICATE found in {}'.format(CERT)) + logger.info('TELEGRAM_WEBHOOK_CERTIFICATE found in {}'.format(cert)) else: - logger.error('TELEGRAM_WEBHOOK_CERTIFICATE not found in {} '.format(CERT)) - + logger.error('TELEGRAM_WEBHOOK_CERTIFICATE not found in {} '.format(cert)) for index, token in enumerate(tokens): @@ -94,27 +96,26 @@ def ready(self): DjangoTelegramBot.bot_tokens.append(bot.token) DjangoTelegramBot.bot_usernames.append(bot.username) - hookurl = '{}{}/{}/'.format(webhook_site,webhook_base, token) - if (use_certificate): - setted = bot.setWebhook(hookurl, certificate=open(CERT,'rb')) + hookurl = '{}{}/{}/'.format(webhook_site, webhook_base, token) + if use_certificate: + setted = bot.setWebhook(hookurl, certificate=open(cert, 'rb')) else: setted = bot.setWebhook(hookurl, certificate=None) - logger.info('Telegram Bot <{}> setting webhook [ {} ] : {}'.format(bot.username,hookurl,setted)) + logger.info('Telegram Bot <{}> setting webhook [ {} ] : {}'.format(bot.username, hookurl, setted)) - #per compatibilità salvo il primo bot nella proprietà DjangoTelegramBot.dispatcher - if index==0: + # per compatibilità salvo il primo bot nella proprietà DjangoTelegramBot.dispatcher + if index == 0: DjangoTelegramBot.dispatcher = DjangoTelegramBot.dispatchers[0] logger.debug('Telegram Bot <{}> set as default bot'.format(bot.username)) - def module_exists(module_name, method_name, execute): try: - #m = __import__(module_name).telegrambot + # m = __import__(module_name).telegrambot m = importlib.import_module(module_name) if execute and hasattr(m, method_name): logger.debug('Run {}.main()'.format(module_name)) - getattr(m,method_name)() + getattr(m, method_name)() else: logger.debug('Run {}'.format(module_name)) @@ -130,4 +131,3 @@ def module_exists(module_name, method_name, execute): module_name = '%s.%s' % (app_config.name, TELEGRAM_BOT_MODULE_NAME) if module_exists(module_name, 'main', True): logger.info('Loaded {}'.format(module_name)) -