diff --git a/HISTORY.rst b/HISTORY.rst index af05966..db7754d 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,11 @@ History ------- +0.1.3 (2016-01-26) +++++++++++++++++++ + +* Fix setting certificate. +* Add method DjangoTelegramBot.getBot(); get bot instance by token or username. 0.1.2 (2016-01-26) ++++++++++++++++++ diff --git a/README.rst b/README.rst index 8fe64f2..c59b2aa 100644 --- a/README.rst +++ b/README.rst @@ -41,7 +41,7 @@ And set your bots:: TELEGRAM_BOT_TOKENS = ('BOT_1_token','BOT_2_token',) TELEGRAM_WEBHOOK_SITE = 'https://mysite.it' TELEGRAM_WEBHOOK_BASE = '/baseurl' - #TELEGRAM_BOT_CERTIFICATE = 'cert.pem' #If your site use self-signed certificate, must be set with location of your public key certificate. (More info at https://core.telegram.org/bots/self-signed ) + #TELEGRAM_WEBHOOK_CERTIFICATE = 'cert.pem' #If your site use self-signed certificate, must be set with location of your public key certificate. (More info at https://core.telegram.org/bots/self-signed ) Include in your urls.py the ``django_telegrambot.urls`` using the same value of ``TELEGRAM_WEBHOOK_BASE`` :: diff --git a/django_telegrambot/__init__.py b/django_telegrambot/__init__.py index eae7f25..e3ff9be 100644 --- a/django_telegrambot/__init__.py +++ b/django_telegrambot/__init__.py @@ -1,2 +1,2 @@ -__version__ = '0.1.2' +__version__ = '0.1.3' 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 ff64821..af46581 100644 --- a/django_telegrambot/apps.py +++ b/django_telegrambot/apps.py @@ -14,20 +14,37 @@ class DjangoTelegramBot(AppConfig): bot_tokens = [] bot_usernames = [] dispatchers = [] + bots = [] @classmethod - def getDispatcher(cls, id = None): + def getDispatcher(cls, id = None, safe=True): if id == None: return dispatchers[0] else: try: index = cls.bot_tokens.index(id) except ValueError: + if not safe : return None try: index = cls.bot_usernames.index(id) except ValueError: return None return cls.dispatchers[index] + + @classmethod + def getBot(cls, id = None, safe = True): + if id == None: + return bots[0] + else: + try: + index = cls.bot_tokens.index(id) + except ValueError: + if not safe : return None + try: + index = cls.bot_usernames.index(id) + except ValueError: + return None + return cls.bots[index] def ready(self): @@ -40,28 +57,25 @@ def ready(self): if not hasattr(settings, 'TELEGRAM_WEBHOOK_SITE'): return webhook_site = settings.TELEGRAM_WEBHOOK_SITE - if not hasattr(settings, 'TELEGRAM_WEBHOOK_BASE'): return webhook_base = settings.TELEGRAM_WEBHOOK_BASE - #print(token) - - ''' - if hasattr(settings, 'TELEGRAM_BOT_CUSTOM_WEBHOOK'): - hookurl = settings.TELEGRAM_BOT_CUSTOM_WEBHOOK - ''' - certificate = None - if hasattr(settings, 'TELEGRAM_BOT_CERTIFICATE'): - certificate = settings.TELEGRAM_BOT_CERTIFICATE + if hasattr(settings, 'TELEGRAM_WEBHOOK_CERTIFICATE'): + CERT = settings.TELEGRAM_WEBHOOK_CERTIFICATE for index, token in enumerate(tokens): bot = telegram.Bot(token=token) hookurl = '%s%s/%s/' % (webhook_site,webhook_base, token) - setted = bot.setWebhook(hookurl, certificate=certificate) + if hasattr(settings, 'TELEGRAM_WEBHOOK_CERTIFICATE'): + setted = bot.setWebhook(hookurl, certificate=open(CERT,'rb')) + else: + setted = bot.setWebhook(hookurl, certificate=None) + print 'Telegram Bot <%s> setting webhook [ %s ] : %s'%(bot.username,hookurl,setted) DjangoTelegramBot.dispatchers.append(telegram.Dispatcher(bot, None)) + DjangoTelegramBot.bots.append(bot) DjangoTelegramBot.bot_tokens.append(bot.token) DjangoTelegramBot.bot_usernames.append(bot.username) diff --git a/django_telegrambot/views.py b/django_telegrambot/views.py index ecb4c14..3a69b93 100644 --- a/django_telegrambot/views.py +++ b/django_telegrambot/views.py @@ -9,17 +9,16 @@ import logging # Get an instance of a logger -logger = logging.getLogger() +logger = logging.getLogger(__name__) # Create your views here. @csrf_exempt def webhook (request, bot_token): - try: - #verifico la validità del token - DjangoTelegramBot.bot_tokens.index(bot_token) - except ValueError: + + #verifico la validità del token + if DjangoTelegramBot.getBot(bot_token, safe=False) is None: return JsonResponse({}) try: @@ -31,7 +30,7 @@ def webhook (request, bot_token): update = telegram.Update.de_json(data) - dispatcher = DjangoTelegramBot.getDispatcher(bot_token) + dispatcher = DjangoTelegramBot.getDispatcher(bot_token, safe=False) dispatcher.processUpdate(update) return JsonResponse({})