Skip to main content

Telegram Mini App analytics SDK for Python

PyPi license PyPI pyversions

The Telemetree Python SDK provides a convenient way to track and analyze Telegram events using the Telemetree service. With this SDK, you can easily capture and send Telegram events to the Telemetree platform for further analysis and insights.

Alt

Features

  • Automatically capture Telegram events and send them to Telemetree
  • Encrypt event data using a hybrid approach with RSA and AES encryption
  • Customize the events and commands to track
  • Simple and intuitive API for easy integration

Installation

You can install the Telemetree Python SDK using pip:

pip install telemetree

Usage

  1. Import the Telemetree SDK:
from telemetree import TelemetreeClient
  1. Initialize the client with your API key and project ID:
api_key = "YOUR_API_KEY"
project_id = "YOUR_PROJECT_ID"

client = TelemetreeClient(api_key, project_id)
  1. Connect the client to your webhook, or pass the event data directly:
# Option 1: Pass raw Telegram webhook data directly
# The SDK will automatically detect and transform Telegram webhook data
webhook_data = {
    "update_id": 123456789,
    "message": {
        "message_id": 1,
        "from": {
            "id": 987654321,
            "is_bot": False,
            "first_name": "John",
            "last_name": "Doe",
            "username": "johndoe",
            "language_code": "en"
        },
        "chat": {
            "id": 987654321,
            "first_name": "John",
            "last_name": "Doe",
            "username": "johndoe",
            "type": "private"
        },
        "date": 1621234567,
        "text": "Hello, world!"
    }
}

response_status_code = client.track(webhook_data)
print(response_status_code)

# Option 2: Pass a pre-formatted event
custom_event = {
    "event_type": "custom_event",
    "telegram_id": 987654321,
    "username": "johndoe",
    "firstname": "John",
    "lastname": "Doe"
}

response_status_code = client.track(custom_event)
print(response_status_code)

Automatic Telegram Webhook Handling

The Telemetree SDK automatically detects and transforms Telegram webhook data into the required format. When you pass a raw Telegram webhook payload to the track method, the SDK will:

  1. Detect that it's a Telegram webhook based on the presence of the update_id field and other Telegram-specific fields
  2. Extract the user ID and other user information from the appropriate location in the webhook data
  3. Transform the data into the format required by Telemetree
  4. Track the event with the appropriate event type (e.g., telegram_message, telegram_callback_query, etc.)

This means you can directly pass the webhook data from your Telegram bot to Telemetree without any manual transformation:

@app.route("/webhook", methods=["POST"])
async def telegram_webhook():
    data = await request.json()

    # Pass the raw webhook data directly to Telemetree
    telemetree_client.track(data)

    # Process the webhook data for your bot
    # ...

    return {"status": "ok"}

Configuration

The Telemetree Python SDK provides some configuration options that you can customize:

  • auto_capture_telegram: Enables or disables automatic capturing of Telegram events (default: True)
  • auto_capture_telegram_events: Specifies the types of Telegram events to capture automatically (default: ["message"])
  • auto_capture_commands: Specifies the Telegram commands to capture automatically (default: ["/start", "/help"])

Other configuration options include the Telemetree API endpoint, encryption keys, and logging settings. You can modify these options either within the Telemetree dashboard or by updating the config.py file in the SDK.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Support

If you have any questions or need assistance, please contact our support team at support@ton.solutions.

Metadata

Release files for telemetree 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for telemetree 0.4.0
File Size Uploaded
telemetree-0.4.0.tar.gz 13.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for telemetree 0.4.0
File Interpreter ABI Platform
telemetree-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.1 kB

Release files / telemetree-0.4.0.tar.gz

Download URL telemetree-0.4.0.tar.gz
Size 13.9 kB
Tags Source
SHA-256 checksum
How to use checksums
af79f8e71bfb7bf1468d980049a2b11d0568c23418a57851c97565e778eaf702
BLAKE2b-256 checksum
How to use checksums
e06b88572a18c6f3a24b9c1f3ae35ff82c28d31e1fc68ed5a08dde2fd6870888
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.4

Release files / telemetree-0.4.0-py3-none-any.whl

Download URL telemetree-0.4.0-py3-none-any.whl
Size 15.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd24da82977369281819250b2e2384a3431be2627d89b43868fc9d755754f61f
BLAKE2b-256 checksum
How to use checksums
94e3100e1aef163bfcd6667986bae0b5bf567eef6d7ed8e36dc8c33fbb86aa70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.2

2 release files

0.3.1

1 release file

0.3.0

2 release files

0.2.1

1 release file

0.2.0

1 release file

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page