diff --git a/django_telegrambot/__init__.py b/django_telegrambot/__init__.py index 5f08e56..f2c4ce0 100644 --- a/django_telegrambot/__init__.py +++ b/django_telegrambot/__init__.py @@ -1,2 +1,2 @@ -__version__ = '0.1.6' +__version__ = '0.1.7b' default_app_config = 'django_telegrambot.apps.DjangoTelegramBot' \ No newline at end of file diff --git a/django_telegrambot/apps.py b/django_telegrambot/apps.py index 3b2fbe2..ac4946d 100644 --- a/django_telegrambot/apps.py +++ b/django_telegrambot/apps.py @@ -4,6 +4,10 @@ from django.conf import settings import importlib import telegram +import os.path + +import logging +logger = logging.getLogger(__name__) class DjangoTelegramBot(AppConfig): @@ -51,28 +55,41 @@ def ready(self): if DjangoTelegramBot.ready_run: return DjangoTelegramBot.ready_run = True - if not hasattr(settings, 'TELEGRAM_BOT_TOKENS'): return + if not hasattr(settings, 'TELEGRAM_BOT_TOKENS'): + logger.debug('Required TELEGRAM_BOT_TOKENS missing in settings') + return tokens = settings.TELEGRAM_BOT_TOKENS - if not hasattr(settings, 'TELEGRAM_WEBHOOK_SITE'): return + if not hasattr(settings, 'TELEGRAM_WEBHOOK_SITE'): + logger.debug('Required TELEGRAM_WEBHOOK_SITE missing in settings') + return webhook_site = settings.TELEGRAM_WEBHOOK_SITE - if not hasattr(settings, 'TELEGRAM_WEBHOOK_BASE'): return + if not hasattr(settings, 'TELEGRAM_WEBHOOK_BASE'): + logger.debug('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 + if(os.path.exists(CERT)): + use_certificate = True + logger.debug('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) hookurl = '{}{}/{}/'.format(webhook_site,webhook_base, token) - if hasattr(settings, 'TELEGRAM_WEBHOOK_CERTIFICATE'): + if (use_certificate): setted = bot.setWebhook(hookurl, certificate=open(CERT,'rb')) else: setted = bot.setWebhook(hookurl, certificate=None) - print ('Telegram Bot <{}> setting webhook [ {} ] : {}'.format(bot.username,hookurl,setted)) + logger.debug('Telegram Bot <{}> setting webhook [ {} ] : {}'.format(bot.username,hookurl,setted)) DjangoTelegramBot.dispatchers.append(telegram.Dispatcher(bot, None)) DjangoTelegramBot.bots.append(bot) @@ -82,6 +99,7 @@ def ready(self): #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): @@ -89,12 +107,13 @@ def module_exists(module_name, method_name, execute): #m = __import__(module_name).telegrambot m = importlib.import_module(module_name) if execute and hasattr(m, method_name): - print ('Run {}.main()'.format(module_name)) + logger.debug('Run {}.main()'.format(module_name)) getattr(m,method_name)() else: - print ('Run {}'.format(module_name)) + logger.debug('Run {}'.format(module_name)) - except ImportError: + except ImportError as er: + logger.debug('{} : {}'.format(module_name, repr(er))) return False return True @@ -103,9 +122,5 @@ def module_exists(module_name, method_name, execute): for app in settings.INSTALLED_APPS: module_name = '{}.telegrambot'.format( app ) if module_exists(module_name, 'main', True): - print ('Loaded {}'.format( module_name)) - - - #import telegrambot - #telegrambot.main() # in alternativa si potrebbe imporre l'uso di un main in telegrambot.py - \ No newline at end of file + logger.debug('Loaded {}'.format(module_name)) + \ No newline at end of file diff --git a/django_telegrambot/views.py b/django_telegrambot/views.py index 17e3c48..9e92e0c 100644 --- a/django_telegrambot/views.py +++ b/django_telegrambot/views.py @@ -23,7 +23,7 @@ def webhook (request, bot_token): try: data = json.loads(request.body.decode("utf-8")) - logger.info(data) + logger.debug(data) except: logger.info('Telegram bot receive invalid request' ) return JsonResponse({}) @@ -35,7 +35,7 @@ def webhook (request, bot_token): try: update = telegram.Update.de_json(data) dispatcher.processUpdate(update) - logger.debug('Processed Update: {} with context {}'.format(update, context)) + logger.debug('Processed Update: {}'.format(update)) # Dispatch any errors except TelegramError as te: logger.warn("Error was raised while processing Update.")