Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django 2.0 compatible #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions sampleproject/bot/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf.urls import url
from django.urls import path

from . import views

urlpatterns = [
url(r'^$', views.index, name='index'),
path('', views.index, name='index'),
]
2 changes: 1 addition & 1 deletion sampleproject/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
sys.path.append(path)

if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sampleproject.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sampleproject.local_settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
Expand Down
1 change: 1 addition & 0 deletions sampleproject/sampleproject/local_settings.sample.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#settings.py
from .settings import *
DJANGO_TELEGRAMBOT = {

'MODE' : 'WEBHOOK', #(Optional [str]) # The default value is WEBHOOK,
Expand Down
8 changes: 4 additions & 4 deletions sampleproject/sampleproject/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.urls import re_path, path, include
from django.contrib import admin

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('django_telegrambot.urls')),
url(r'^$', include('bot.urls')),
path('admin/', admin.site.urls),
path('', include('django_telegrambot.urls')),
path('', include('bot.urls')),
]