Python SDK

📘

Python resources

Github | PyPI

Introduction

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

Key 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

Getting Started

System Requirements:

  • pip package manager
  • Python 3.7+
  • A Telegram bot project set up with Python

Installation

You can install the Telemetree Python SDK using pip:

pip install telemetree

Usage

  1. Import the Telemetree SDK:
from telemetree import TelemetreeClient
  1. Initialise the client with your API key and project ID (How to get credentials):
api_key = "YOUR_API_KEY"
project_id = "YOUR_PROJECT_ID"

client = TelemetreeClient(api_key, project_id)
  1. Connect the client to your webhook.
async def webhook(request: Request):
    """Telegram webhook endpoint."""
		response_status_code = client.track(event)
		print(response_status_code)
    de_json_object = Update.de_json(data=await request.json(), bot=application.bot)
    await application.update_queue.put(de_json_object)
  1. Alternatively, you can pass on the Telegram Update dictionary directly:
event = {
    "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(event)
print(response_status_code)

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.