Skip to content

Commit

Permalink
ver 0.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Scarlato (Jungmann) committed Feb 8, 2016
1 parent 641180b commit 4408ff2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion django_telegrambot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '0.1.7b'
__version__ = '0.1.7'
default_app_config = 'django_telegrambot.apps.DjangoTelegramBot'
12 changes: 6 additions & 6 deletions django_telegrambot/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))

Expand All @@ -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)
Expand Down Expand Up @@ -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))

16 changes: 10 additions & 6 deletions django_telegrambot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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({})

0 comments on commit 4408ff2

Please sign in to comment.