Skip to content

Commit

Permalink
Port urls.py to Django 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuruddinjr committed Jan 9, 2018
1 parent 8c357a4 commit ad54844
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions django_telegrambot/urls.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@

from django.conf.urls import url
from django.urls import path, re_path
from . import views
from django.conf import settings

webhook_base = settings.DJANGO_TELEGRAMBOT.get('WEBHOOK_PREFIX','/')
webhook_base = settings.DJANGO_TELEGRAMBOT.get('WEBHOOK_PREFIX', '')
if webhook_base.startswith("/"):
webhook_base = webhook_base[1:]
if not webhook_base.endswith("/"):
if webhook_base is not '' and not webhook_base.endswith("/"):
webhook_base += "/"

urlpatterns = [
url(r'admin/django-telegrambot/$', views.home, name='django-telegrambot'),
url(r'{}(?P<bot_token>.+?)/$'.format(webhook_base), views.webhook, name='webhook'),
path('admin/django-telegrambot/', views.home, name='django-telegrambot'),
re_path('{}(?P<bot_token>[-_:a-zA-Z0-9]+)/'.format(webhook_base), views.webhook, name='webhook'),
]
8 changes: 4 additions & 4 deletions django_telegrambot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@staff_member_required
def home(request):
bot_list = DjangoTelegramBot.bots
context = {'bot_list': bot_list, 'update_mode':settings.DJANGO_TELEGRAMBOT.get('MODE', 'WEBHOOK')}
context = {'bot_list': bot_list, 'update_mode': settings.DJANGO_TELEGRAMBOT.get('MODE', 'WEBHOOK')}
return render(request, 'django_telegrambot/index.html', context)


Expand All @@ -30,14 +30,14 @@ def webhook (request, bot_token):
#verifico la validità del token
bot = DjangoTelegramBot.getBot(bot_id=bot_token, safe=False)
if bot is None:
logger.warn('Request for not found token : {}'.format(bot_token))
logger.warning('Request for not found token : {}'.format(bot_token))
return JsonResponse({})

try:
data = json.loads(request.body.decode("utf-8"))

except:
logger.warn('Telegram bot <{}> receive invalid request : {}'.format(bot.username, repr(request)))
logger.warning('Telegram bot <{}> receive invalid request : {}'.format(bot.username, repr(request)))
return JsonResponse({})

dispatcher = DjangoTelegramBot.getDispatcher(bot_token, safe=False)
Expand All @@ -51,7 +51,7 @@ def webhook (request, bot_token):
logger.debug('Bot <{}> : Processed update {}'.format(bot.username, update))
# Dispatch any errors
except TelegramError as te:
logger.warn("Bot <{}> : Error was raised while processing Update.".format(bot.username))
logger.warning("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
Expand Down

0 comments on commit ad54844

Please sign in to comment.