Skip to content

Instantly share code, notes, and snippets.

@dmytrostriletskyi
Last active February 15, 2018 13:54
Show Gist options
  • Save dmytrostriletskyi/fdcae4fe520b7e38f4ebc4cd1e786376 to your computer and use it in GitHub Desktop.
Save dmytrostriletskyi/fdcae4fe520b7e38f4ebc4cd1e786376 to your computer and use it in GitHub Desktop.
An example of the verification the received data from Telegram
from django_telegram_login.authentication import verify_telegram_authentication
from django_telegram_login.errors import (
NotTelegramDataError,
TelegramDataIsOutdatedError,
)
def index(request):
# Initially, the index page may have no get params in URL
# For example, if it is a home page, a user should be redirected from the widget
if not request.GET.get('hash'):
return HttpResponse('Handle the missing Telegram data in the response.')
try:
result = verify_telegram_authentication(bot_token=bot_token, request_data=request.GET)
except TelegramDataIsOutdatedError:
return HttpResponse('The authentication data was received more than a day ago.')
except NotTelegramDataError:
return HttpResponse('The data is not related to the Telegram user!')
# Or handle it as you wish. For instance, save to the database.
return HttpResponse('Hello, ' + result['first_name'] + '!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment