Skip to content

Commit

Permalink
Version 0.1.3, Fix certificate settings, Add DjangoTelegramBot.getBot()
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Scarlato (Jungmann) committed Jan 28, 2016
1 parent 1c16aaf commit 661bf17
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
++++++++++++++++++
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`` ::
Expand Down
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.2'
__version__ = '0.1.3'
default_app_config = 'django_telegrambot.apps.DjangoTelegramBot'
38 changes: 26 additions & 12 deletions django_telegrambot/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)

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

0 comments on commit 661bf17

Please sign in to comment.