This Python package provides an authentication mechanism for Telegram web apps. It implements the algorithm for validating data received from a Telegram web app, ensuring the authenticity and integrity of the data. For detailed information on validating data received via a web app, please refer to the [official Telegram documentation](https://core.telegram.org/bots/webapps#validating-data-received-via-the-web-app).
Project description
Telegram Authentication Module
This module provides functionality for handling authentication and validation of initial data received from Telegram. It includes parsing, preparing, validating, and encoding/decoding initial data. It also provides data models for representing Telegram users and initial data.
Installation
To use this module, you need to have Python 3.x installed. Install the necessary dependencies using pip:
pip install telegram_webapps_authentication
Example
Here is an example of how to use the Authenticator
class with FastAPI:
from typing import Optional
from fastapi import FastAPI, HTTPException, Header, Depends
from pydantic import BaseModel
from telegram_webapps_authentication import Authenticator, TelegramUser
app = FastAPI()
BOT_TOKEN = "YOUR_BOT_TOKEN" # Replace it with your real telegram bot token
authenticator = Authenticator(BOT_TOKEN)
class Message(BaseModel):
text: str
@app.get("/validate")
def validate_init_data(authorization: Optional[str] = Header()):
try:
if authenticator.validate_init_data(authorization):
initial_data = authenticator.get_initial_data(authorization)
return {"message": initial_data.dict()}
else:
raise HTTPException(status_code=400, detail="Data is not valid")
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/get_user")
def get_user_instance(authorization: Optional[str] = Header()):
user = authenticator.get_telegram_user(authorization)
return user
@app.post("/message")
async def send_message(
message: Message,
user: TelegramUser = Depends(authenticator.get_telegram_user),
):
"""
Your backend logic
"""
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for telegram_webapps_authentication-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94076a768049432cbf8f9e51a2d4de73e9504deda8f18ce2bb5c2a481e726dc6 |
|
MD5 | 12fe4f41f9e9a95325eecab8173f16e4 |
|
BLAKE2b-256 | d7ad3efeef5b32f934a8f56a2728040c59d9c945bee9618aaf278cec7ddcf1e2 |
Hashes for telegram_webapps_authentication-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61604cd75bc689fe9ebe307eeb02553bba337e946b4b81b209d22774a1d0e29f |
|
MD5 | 59cd4527a370b2bbc3491bfd74866c5d |
|
BLAKE2b-256 | ca954552fc21aa813cd01a3dcd6e4f1026be45881feb2874f51b58c82abe9087 |