Skip to main content

🚀 A Rust-based Python package for validating Telegram WebApp authorization.

Project description

🦀🐍 Telegram WebApp Auth Library

This library is a simple way to authenticate users in your web application using Telegram.

Example

For a complete example, check out the Example Bot repository.

Installation

Using pip

pip install teleapp-auth

Using Poetry

poetry add teleapp-auth

Using pipenv

pipenv install teleapp-auth

Using uv

uv pip install teleapp-auth

Usage

from fastapi import Request, FastAPI
from teleapp_auth import get_secret_key, parse_webapp_data, validate_webapp_data


app = FastAPI()
secret_key = get_secret_key("BOT_TOKEN")


@app.post("/check_data")
async def check_data(request: Request) -> bool:
    request_json = await request.json()
    auth_data = request_json.get("auth_data")  # Telegram auth data from the request
    webapp_data = parse_webapp_data(auth_data)  # Parse the auth data to the WebAppInitData object

    # Example of the parsed data:
    # WebAppInitData {
    #   query_id: "AAEt6-JYAAAAAC3r4lj2oADQ",
    #   user: WebAppUser {
    #       id: 1,
    #       is_bot: None,
    #       first_name: "Test",
    #       last_name: "",
    #       username: "test",
    #       language_code: "ru",
    #       is_premium: None,
    #       added_to_attachment_menu: None,
    #       allows_write_to_pm: true,
    #       photo_url: None
    #   },
    #  receiver: None,
    #  chat: None,
    #  chat_type: None,
    #  chat_instance: None,
    #  start_param: None,
    #  can_send_after: None,
    #  auth_date: 1726572911,
    #  hash: "f87a5a37a5b487700a35cb1e3d2e92afa67e4b67066c9f1fa2c34986c2350b6e
    # }
    

    return validate_webapp_data(webapp_data, secret_key)  # True if the data is valid, False otherwise

FAQ

1. What is the purpose of this library?

This library simplifies the process of authenticating users in your web application using Telegram WebApp authentication.

2. Can I get the user's profile photo with this library?

The photo_url field in the authentication data contains a URL to the user's profile photo. However, it is only available for Mini Apps launched from the attachment menu. Currently, integration with the attachment menu is only accessible to major advertisers on the Telegram Ad Platform. All bots can still test this feature in the test server environment by contacting Botfather on the test server.

3. Does this library automatically handle sending authentication data to my server?

No, you need to handle the process of sending the authentication data to your server. You can find an example of how to do this in the page.html file within the examples directory.

4. How do I validate the authentication data?

Use the validate_webapp_data function from the library to verify the authenticity of the data. This function compares the data with a secret key derived from your bot's token.

5. Can I use this library with any Python web framework?

Yes, the library is framework-agnostic, though examples are provided with FastAPI and Blacksheep. You can adapt it to other frameworks with minimal effort.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

teleapp_auth-1.0.1.tar.gz (12.9 kB view hashes)

Uploaded Source

Built Distributions

teleapp_auth-1.0.1-cp312-none-win_amd64.whl (157.9 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

teleapp_auth-1.0.1-cp312-cp312-manylinux_2_34_x86_64.whl (251.8 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.34+ x86-64

teleapp_auth-1.0.1-cp312-cp312-macosx_11_0_arm64.whl (219.1 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

teleapp_auth-1.0.1-cp311-none-win_amd64.whl (158.7 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

teleapp_auth-1.0.1-cp311-cp311-manylinux_2_34_x86_64.whl (252.5 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.34+ x86-64

teleapp_auth-1.0.1-cp311-cp311-macosx_11_0_arm64.whl (219.8 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

teleapp_auth-1.0.1-cp310-none-win_amd64.whl (158.8 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

teleapp_auth-1.0.1-cp310-cp310-manylinux_2_34_x86_64.whl (252.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.34+ x86-64

teleapp_auth-1.0.1-cp310-cp310-macosx_11_0_arm64.whl (219.9 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

teleapp_auth-1.0.1-cp39-none-win_amd64.whl (159.4 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

teleapp_auth-1.0.1-cp39-cp39-manylinux_2_34_x86_64.whl (253.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.34+ x86-64

teleapp_auth-1.0.1-cp39-cp39-macosx_11_0_arm64.whl (220.3 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page