Integrating Botality Analytics with Your Pyrogram Telegram Bot


Integrating Botality analytics into your Telegram bot using Pyrogram involves sending message data to Botality's API whenever your bot receives a message. Here's a step-by-step guide to achieve this integration: 


1. Install Pyrogram
 


First, ensure that Pyrogram is installed in your Python environment: 

pip install pyrogram


2. Set Up Your Bot with Pyrogram
 
Initialize your bot using Pyrogram's Client class:
 
from pyrogram import Client, filters

app = Client("my_bot", bot_token="YOUR_BOT_TOKEN")


Replace "YOUR_BOT_TOKEN" with your actual bot token.
 
3. Define a Function to Send Data to Botality
 
Create a function that formats and sends message data to Botality's API:
 
import requests

BOTALITY_API_URL = "https://botality.cc/api/v1/messages"
BOTALITY_TOKEN = "YOUR_BOTALITY_TOKEN"

def send_to_botality(message):
    payload = {
        "token": BOTALITY_TOKEN,
        "data": {
            "message_id": message.message_id,
            "from": {
                "id": message.from_user.id,
                "is_bot": message.from_user.is_bot,
                "username": message.from_user.username
            },
            "date": message.date,
            "text": message.text
        }
    }
    headers = {"Content-Type": "application/json"}
    response = requests.post(BOTALITY_API_URL, json=payload, headers=headers)
    return response.status_code == 200


Ensure you replace "YOUR_BOTALITY_TOKEN" with the token provided by Botality.
 
4. Handle Incoming Messages and Send Data to Botality
 
Set up a message handler in Pyrogram that processes incoming messages and forwards the data to Botality:
 
@app.on_message(filters.text)
def handle_message(client, message):
    if send_to_botality(message):
        print(f"Message {message.message_id} sent to Botality.")
    else:
        print(f"Failed to send message {message.message_id} to Botality.")


5. Run the Bot
 
Start your bot to begin processing messages:
 
app.run()


Complete Example
 
Combining all the steps, your bot's code should look like this:
 
from pyrogram import Client, filters
import requests

# Constants
BOTALITY_API_URL = "https://botality.cc/api/v1/messages"
BOTALITY_TOKEN = "YOUR_BOTALITY_TOKEN"
BOT_TOKEN = "YOUR_BOT_TOKEN"

# Initialize Pyrogram Client
app = Client("my_bot", bot_token=BOT_TOKEN)

# Function to send message data to Botality
def send_to_botality(message):
    payload = {
        "token": BOTALITY_TOKEN,
        "data": {
            "message_id": message.message_id,
            "from": {
                "id": message.from_user.id,
                "is_bot": message.from_user.is_bot,
                "username": message.from_user.username
            },
            "date": message.date,
            "text": message.text
        }
    }
    headers = {"Content-Type": "application/json"}
    response = requests.post(BOTALITY_API_URL, json=payload, headers=headers)
    return response.status_code == 200

# Message handler
@app.on_message(filters.text)
def handle_message(client, message):
    if send_to_botality(message):
        print(f"Message {message.message_id} sent to Botality.")
    else:
        print(f"Failed to send message {message.message_id} to Botality.")

# Run the bot
app.run()


Notes:

Ensure that your server has internet access to communicate with Botality's API.

Handle exceptions and errors appropriately to ensure the stability of your bot.

For more details on Pyrogram, refer to the Pyrogram Documentation.


By following these steps, your Telegram bot will send message data to Botality, enabling you to leverage their analytics services.