This bot serves as an authorization mechanism similar to Google OAuth but operates within Telegram. It authorizes users and redirects them to a specified URL with user information.
- Users are given a link to open the bot in Telegram:
https://t.me/<bot_nickname>?start=<params>. paramsis composed ofhash-data-lang, where:
- hash is a MD5 hash generated from
secret_key.data - data is the information that will be returned in
redirect_url. Max length is 24 chars. - lang is a two-letter code determining the language to be used (all languages are specified in the TEXT variable in config.py).
- After clicking a button in the bot, users are redirected to a configured URL with their details.
- Set
REDIRECT_URL(config.py) andSECRET_KEYin the bot's configuration file.
BOT_TOKEN=xxxx:xxxxxxxxxx
SECRET_KEY=your_secret_keypip3 install -r requirements.txt
python3 bot.pypoetry init
python3 bot.py- User clicks the bot link and opens the bot in Telegram.
- The bot verifies the params using the
secret_key. - If verification is successful, the bot provides a button for the user to click.
- Upon clicking the button, the user is redirected to
redirect_urlwith the following parameters:
data: The information passed in the originalparams.auth_at: The time of authorization in UNIX format.user_id: The Telegram user ID.username: The Telegram username of the user.first_name: The first name of the user.last_name: The last name of the user.hash: A MD5 hash generated fromsecret_key.data.auth_at.user_id.username.first_name.last_name.
The hash parameter ensures the integrity and authenticity of the user data being sent to the redirect URL:
hash = md5(secret_key.data.auth_at.user_id.username.first_name.last_name)
import hashlib
SECRET_KEY = "your_secret_key"
def generate_link(bot_nickname: str, data: str, lang: str) -> str:
if len(data) > 24:
raise ValueError("Data length must be 24 characters or less.")
hash_string = f"{SECRET_KEY}.{data}"
hash_value = hashlib.md5(hash_string.encode()).hexdigest()
params = f"{hash_value}-{data}-{lang}"
return f"https://t.me/{bot_nickname}?start={params}"
def validate_response(
data: str, auth_at: int, user_id: int, username: str,
first_name: str, last_name: str, received_hash: str
) -> bool:
hash_string = f"{SECRET_KEY}.{data}.{auth_at}.{user_id}.{username}.{first_name}.{last_name}"
expected_hash = hashlib.md5(hash_string.encode()).hexdigest()
return expected_hash == received_hash<?php
define('SECRET_KEY', 'your_secret_key');
function generate_link($bot_nickname, $data, $lang) {
if (strlen($data) > 24) {
throw new Exception("Data length must be 24 characters or less.");
}
$hash_string = SECRET_KEY . '.' . $data;
$hash_value = md5($hash_string);
$params = $hash_value . '-' . $data . '-' . $lang;
return "https://t.me/$bot_nickname?start=$params";
}
function validate_response($data, $auth_at, $user_id, $username, $first_name, $last_name, $received_hash) {
$hash_string = SECRET_KEY . '.' . $data . '.' . $auth_at . '.' . $user_id . '.' . $username . '.' . $first_name . '.' . $last_name;
$expected_hash = md5($hash_string);
return $expected_hash === $received_hash;
}
?>