From abef19b56f384dd651ab89a6e817dce03de073d8 Mon Sep 17 00:00:00 2001 From: "Francesco Scarlato (Jungmann)" Date: Thu, 25 May 2017 19:48:25 +0200 Subject: [PATCH] Fix Invalid Token Error --- django_telegrambot/apps.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/django_telegrambot/apps.py b/django_telegrambot/apps.py index 38b77e1..869925d 100644 --- a/django_telegrambot/apps.py +++ b/django_telegrambot/apps.py @@ -8,6 +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 import os.path import logging @@ -164,7 +165,11 @@ def ready(self): timeout = b.get('TIMEOUT', None) if self.mode == WEBHOOK_MODE: - bot = telegram.Bot(token=token) + 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) @@ -177,12 +182,17 @@ def ready(self): 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: - updater = Updater(token=token) - bot = updater.bot - bot.delete_webhook() - DjangoTelegramBot.updaters.append(updater) - DjangoTelegramBot.dispatchers.append(updater.dispatcher) - DjangoTelegramBot.__used_tokens.add(token) + try: + updater = Updater(token=token) + bot = updater.bot + bot.delete_webhook() + DjangoTelegramBot.updaters.append(updater) + DjangoTelegramBot.dispatchers.append(updater.dispatcher) + DjangoTelegramBot.__used_tokens.add(token) + except InvalidToken + logger.error('Invalid Token : {}'.format(token)) + return + DjangoTelegramBot.bots.append(bot) DjangoTelegramBot.bot_tokens.append(token)