From ab9f3d49b2af879492daaf73bd28a932f9ab2b3b Mon Sep 17 00:00:00 2001 From: "Francesco Scarlato (Jungmann)" Date: Thu, 25 May 2017 20:00:34 +0200 Subject: [PATCH] Fix Invalid Token Error --- django_telegrambot/apps.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/django_telegrambot/apps.py b/django_telegrambot/apps.py index 2c06507..dd4ab80 100644 --- a/django_telegrambot/apps.py +++ b/django_telegrambot/apps.py @@ -8,7 +8,7 @@ from django.utils.module_loading import module_has_submodule from telegram.ext import Dispatcher from telegram.ext import Updater -from telegram.error import InvalidToken +from telegram.error import InvalidToken, TelegramError import os.path import logging @@ -167,20 +167,25 @@ def ready(self): if self.mode == WEBHOOK_MODE: try: bot = telegram.Bot(token=token) - except InvalidToken: - logger.error('Invalid Token : {}'.format(token)) - return - DjangoTelegramBot.dispatchers.append(Dispatcher(bot, None, workers=0)) - hookurl = '{}/{}/{}/'.format(webhook_site, webhook_base, token) + DjangoTelegramBot.dispatchers.append(Dispatcher(bot, None, workers=0)) + hookurl = '{}/{}/{}/'.format(webhook_site, webhook_base, token) - max_connections = b.get('WEBHOOK_MAX_CONNECTIONS', 40) + max_connections = b.get('WEBHOOK_MAX_CONNECTIONS', 40) - setted = bot.setWebhook(hookurl, certificate=certificate, timeout=timeout, max_connections=max_connections, allowed_updates=allowed_updates) - webhook_info = bot.getWebhookInfo() - real_allowed = webhook_info.allowed_updates if webhook_info.allowed_updates else ["ALL"] + setted = bot.setWebhook(hookurl, certificate=certificate, timeout=timeout, max_connections=max_connections, allowed_updates=allowed_updates) + webhook_info = bot.getWebhookInfo() + real_allowed = webhook_info.allowed_updates if webhook_info.allowed_updates else ["ALL"] + + bot.more_info = webhook_info + logger.info('Telegram Bot <{}> setting webhook [ {} ] max connections:{} allowed updates:{} pending updates:{} : {}'.format(bot.username, webhook_info.url, webhook_info.max_connections, real_allowed, webhook_info.pending_update_count, setted)) + + except InvalidToken: + logger.error('Invalid Token : {}'.format(token)) + return + except TelegramError as er: + logger.error('Error : {}'.format(repr(er))) + return - bot.more_info = webhook_info - logger.info('Telegram Bot <{}> setting webhook [ {} ] max connections:{} allowed updates:{} pending updates:{} : {}'.format(bot.username, webhook_info.url, webhook_info.max_connections, real_allowed, webhook_info.pending_update_count, setted)) else: try: updater = Updater(token=token) @@ -192,7 +197,9 @@ def ready(self): except InvalidToken: logger.error('Invalid Token : {}'.format(token)) return - + except TelegramError as er: + logger.error('Error : {}'.format(repr(er))) + return DjangoTelegramBot.bots.append(bot) DjangoTelegramBot.bot_tokens.append(token)