Last active
February 15, 2018 13:54
-
-
Save dmytrostriletskyi/fdcae4fe520b7e38f4ebc4cd1e786376 to your computer and use it in GitHub Desktop.
An example of the verification the received data from Telegram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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