A simple example of implementing authorization on a website using the Telegram login widget.
Telegram Official Documentation: https://core.telegram.org/widgets/login
- Set the TELEGRAM_TOKEN and TELEGRAM_LOGIN environment variables.
Linux
export TELEGRAM_TOKEN=**12345678:aaaaabbbbbcccccdddddeeeeeffffff**
export TELEGRAM_LOGIN=**MY_TEST_BOT**
Windows (PowerShell)
$Env:TELEGRAM_TOKEN = "12345678:aaaaabbbbbcccccdddddeeeeeffffff"
$Env:TELEGRAM_LOGIN = "MY_TEST_BOT"
- Register your domain name using the command /setdomain in the @BotFather telegram bot.
- Create virtual environment and activate it.
- Install python libraries using the pip command pip install -r .\requirements.txt
- run python main.py
The project implements the generation of the Telegram Login Widget provided by the TelegramLoginWidget class from app.widget.
redirect_telegram_login_widget()
# Returns JavaScript code to redirect to the Telegram Login Widget.
callback_telegram_login_widget()
# Returns JavaScript code for the callback of the Telegram Login Widget.
If authorization was successful, the method waits for the Javascript function to be called.
...
callback_widget = callback_telegram_login_widget(func='onTelegramAuth', arg='user')
# Where onTelegramAuth is name of JS function that should be run after successful authorization.
return templates.TemplateResponse(
'login.html',
context={
'request': request,
'callback_telegram_login_widget': callback_widget
}
)
...
Put this code in your HTML template:
<script type="text/javascript">
function onTelegramAuth(user) {
alert(
'Logged in as ' + user.first_name + ' ' + user.last_name + '!');
}
</script>
<form>
<div>
<!-- {{ callback_telegram_login_widget|safe }} -->
{% autoescape off %}
{{ callback_telegram_login_widget }}
{% endautoescape %}
</div>
</form>
This code is created only as an example. To use this code, additional mechanisms for issuing tokens and controlling authorized users are required. Also, the code structure is not a reference and requires adjustments.