From 4408ff2319c3acebed36056c6bce573a5bd26448 Mon Sep 17 00:00:00 2001 From: "Francesco Scarlato (Jungmann)" Date: Mon, 8 Feb 2016 22:55:27 +0000 Subject: [PATCH] ver 0.1.7 --- django_telegrambot/__init__.py | 2 +- django_telegrambot/apps.py | 12 ++++++------ django_telegrambot/views.py | 16 ++++++++++------ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/django_telegrambot/__init__.py b/django_telegrambot/__init__.py index f2c4ce0..534a23c 100644 --- a/django_telegrambot/__init__.py +++ b/django_telegrambot/__init__.py @@ -1,2 +1,2 @@ -__version__ = '0.1.7b' +__version__ = '0.1.7' 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 ac4946d..8f7d7f4 100644 --- a/django_telegrambot/apps.py +++ b/django_telegrambot/apps.py @@ -56,17 +56,17 @@ def ready(self): DjangoTelegramBot.ready_run = True if not hasattr(settings, 'TELEGRAM_BOT_TOKENS'): - logger.debug('Required TELEGRAM_BOT_TOKENS missing in settings') + logger.warn('Required TELEGRAM_BOT_TOKENS missing in settings') return tokens = settings.TELEGRAM_BOT_TOKENS if not hasattr(settings, 'TELEGRAM_WEBHOOK_SITE'): - logger.debug('Required TELEGRAM_WEBHOOK_SITE missing in settings') + logger.warn('Required TELEGRAM_WEBHOOK_SITE missing in settings') return webhook_site = settings.TELEGRAM_WEBHOOK_SITE if not hasattr(settings, 'TELEGRAM_WEBHOOK_BASE'): - logger.debug('Required TELEGRAM_WEBHOOK_BASE missing in settings') + logger.warn('Required TELEGRAM_WEBHOOK_BASE missing in settings') return webhook_base = settings.TELEGRAM_WEBHOOK_BASE @@ -75,7 +75,7 @@ def ready(self): CERT = settings.TELEGRAM_WEBHOOK_CERTIFICATE if(os.path.exists(CERT)): use_certificate = True - logger.debug('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)) @@ -89,7 +89,7 @@ def ready(self): else: setted = bot.setWebhook(hookurl, certificate=None) - logger.debug('Telegram Bot <{}> setting webhook [ {} ] : {}'.format(bot.username,hookurl,setted)) + logger.info('Telegram Bot <{}> setting webhook [ {} ] : {}'.format(bot.username,hookurl,setted)) DjangoTelegramBot.dispatchers.append(telegram.Dispatcher(bot, None)) DjangoTelegramBot.bots.append(bot) @@ -122,5 +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): - logger.debug('Loaded {}'.format(module_name)) + logger.info('Loaded {}'.format(module_name)) \ No newline at end of file diff --git a/django_telegrambot/views.py b/django_telegrambot/views.py index 9e92e0c..e7cb4b5 100644 --- a/django_telegrambot/views.py +++ b/django_telegrambot/views.py @@ -3,6 +3,7 @@ from django.http import JsonResponse from django_telegrambot.apps import DjangoTelegramBot from django.views.decorators.csrf import csrf_exempt +import sys import json import telegram # import the logging library @@ -18,32 +19,35 @@ def webhook (request, bot_token): #verifico la validità del token - if DjangoTelegramBot.getBot(bot_token, safe=False) is None: + bot = DjangoTelegramBot.getBot(bot_token, safe=False) + if bot is None: + logger.warn('Request for not found token : {}'.format(bot_token)) return JsonResponse({}) try: data = json.loads(request.body.decode("utf-8")) - logger.debug(data) + except: - logger.info('Telegram bot receive invalid request' ) + logger.warn('Telegram bot <{}> receive invalid request : {}'.format(bot.username, repr(request))) return JsonResponse({}) dispatcher = DjangoTelegramBot.getDispatcher(bot_token, safe=False) if dispatcher is None: + logger.error('Dispatcher for bot <{}> not found : {}'.format(bot.username, bot_token)) return JsonResponse({}) try: update = telegram.Update.de_json(data) dispatcher.processUpdate(update) - logger.debug('Processed Update: {}'.format(update)) + logger.debug('Bot <{}> : Processed update {}'.format(bot.username, update)) # Dispatch any errors except TelegramError as te: - logger.warn("Error was raised while processing Update.") + logger.warn("Bot <{}> : Error was raised while processing Update.".format(bot.username)) dispatcher.dispatchError(update, te) # All other errors should not stop the thread, just print them except: - logger.error("An uncaught error was raised while processing an update") + logger.error("Bot <{}> : An uncaught error was raised while processing an update\n{}".format(bot.username, sys.exc_info()[0])) finally: return JsonResponse({})