diff --git a/django_telegrambot/apps.py b/django_telegrambot/apps.py index 794677a..238859b 100644 --- a/django_telegrambot/apps.py +++ b/django_telegrambot/apps.py @@ -1,15 +1,19 @@ # coding=utf-8 # django_telegram_bot/apps.py from django.apps import AppConfig +from django.apps import apps from django.conf import settings import importlib import telegram +from django.utils.module_loading import module_has_submodule from telegram.ext import Dispatcher import os.path import logging logger = logging.getLogger(__name__) +TELEGRAM_BOT_MODULE_NAME = 'telegrambot' + class DjangoTelegramBot(AppConfig): name = 'django_telegrambot' @@ -20,7 +24,7 @@ class DjangoTelegramBot(AppConfig): bot_usernames = [] dispatchers = [] bots = [] - + @classmethod def getDispatcher(cls, id = None, safe=True): if id == None: @@ -35,7 +39,7 @@ def getDispatcher(cls, id = None, safe=True): except ValueError: return None return cls.dispatchers[index] - + @classmethod def getBot(cls, id = None, safe = True): if id == None: @@ -50,27 +54,27 @@ def getBot(cls, id = None, safe = True): except ValueError: return None return cls.bots[index] - - + + def ready(self): if DjangoTelegramBot.ready_run: return DjangoTelegramBot.ready_run = True - - if not hasattr(settings, 'TELEGRAM_BOT_TOKENS'): + + if not hasattr(settings, 'TELEGRAM_BOT_TOKENS'): logger.warn('Required TELEGRAM_BOT_TOKENS missing in settings') return tokens = settings.TELEGRAM_BOT_TOKENS - - if not hasattr(settings, 'TELEGRAM_WEBHOOK_SITE'): + + if not hasattr(settings, 'TELEGRAM_WEBHOOK_SITE'): logger.warn('Required TELEGRAM_WEBHOOK_SITE missing in settings') return webhook_site = settings.TELEGRAM_WEBHOOK_SITE - - if not hasattr(settings, 'TELEGRAM_WEBHOOK_BASE'): + + if not hasattr(settings, 'TELEGRAM_WEBHOOK_BASE'): logger.warn('Required TELEGRAM_WEBHOOK_BASE missing in settings') return webhook_base = settings.TELEGRAM_WEBHOOK_BASE - + use_certificate = False if hasattr(settings, 'TELEGRAM_WEBHOOK_CERTIFICATE'): CERT = settings.TELEGRAM_WEBHOOK_CERTIFICATE @@ -79,12 +83,12 @@ def ready(self): logger.info('TELEGRAM_WEBHOOK_CERTIFICATE found in {}'.format(CERT)) else: logger.error('TELEGRAM_WEBHOOK_CERTIFICATE not found in {} '.format(CERT)) - - + + for index, token in enumerate(tokens): - + bot = telegram.Bot(token=token) - + DjangoTelegramBot.dispatchers.append(Dispatcher(bot, None, workers=0)) DjangoTelegramBot.bots.append(bot) DjangoTelegramBot.bot_tokens.append(bot.token) @@ -95,15 +99,15 @@ def ready(self): 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)) - + #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 @@ -113,16 +117,17 @@ def module_exists(module_name, method_name, execute): getattr(m,method_name)() else: logger.debug('Run {}'.format(module_name)) - + except ImportError as er: logger.debug('{} : {}'.format(module_name, repr(er))) return False - + return True - + # import telegram bot handlers for all INSTALLED_APPS - for app in settings.INSTALLED_APPS: - module_name = '{}.telegrambot'.format( app ) - if module_exists(module_name, 'main', True): - logger.info('Loaded {}'.format(module_name)) - + for app_config in apps.get_app_configs(): + if module_has_submodule(app_config.module, TELEGRAM_BOT_MODULE_NAME): + module_name = '%s.%s' % (app_config.name, TELEGRAM_BOT_MODULE_NAME) + if module_exists(module_name, 'main', True): + logger.info('Loaded {}'.format(module_name)) +