Skip to main content

Universal asynchronous library for posting Telegram Updates to TGViz API

Project description

TGViz

Downloads

TGViz is a universal asynchronous library for integrating with the TGViz API.
It allows you to send Telegram bot Updates to the TGViz service for logging and processing.

Installation

pip install tgviz

Python >= 3.7

How It Works

You can use tgviz in two ways:

  1. As a Processor (Recommended)
    The easiest way to integrate with TGViz. You wrap your existing Telegram bot update handlers,
    and tgviz automatically logs data and decides whether to process the update or skip it based on the API response (if "mandatory subscription" is on).

  2. Direct API Calls
    If you prefer more control, you can send updates manually using the provided TGVizClient class.

Processor Modes

The TGVizUpdateProcessor supports two modes:

  • Asynchronous mode (default) – The fastest option. The bot continues processing updates without waiting for a response from the API.
    This is suitable for any use cases (analytics, mailing and ad views) except "mandatory subscription" ads type.

  • Synchronous mode – The processor waits for the API response before passing the update to the bot's handler.
    This is required for implementing the upcoming "mandatory subscription" feature.
    In this mode, the API may return skip_update=True, preventing further processing if the user hasn’t subscribed yet.
    (Feature currently in development.)

Usage Examples

1. Using tgviz as a Processor (Recommended)

If you want TGViz to automatically decide whether an update should be processed,
you can use the TGVizUpdateProcessor.

With aiogram

import asyncio
from aiogram import Dispatcher, Bot, types
from aiogram.dispatcher.middlewares import BaseMiddleware
from tgviz.middleware import TGVizUpdateProcessor

TELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"
TGVIZ_BOT_TOKEN = "YOUR_TGVIZ_BOT_TOKEN"

bot = Bot(token=TELEGRAM_BOT_TOKEN)
dp = Dispatcher(bot)

# Use is_async=False for synchronous mode (e.g., mandatory subscription feature)
tgviz_processor = TGVizUpdateProcessor(tgviz_bot_token=TGVIZ_BOT_TOKEN, is_async=True)

class TGVizMiddleware(BaseMiddleware):
    def __init__(self, processor: TGVizUpdateProcessor):
        self.processor = processor
    async def __call__(self, handler: callable, event: types.Update, data: dict):
        update_data = event.model_dump()
        return await self.processor.process_update(
            update=update_data,
            handler=lambda _: handler(event, data)
        )

# Register TGViz as outer middleware
dp.update.outer_middleware.register(TGVizMiddleware(tgviz_processor))

@dp.message_handler()
async def echo_handler(message: types.Message):
    await message.answer(message.text)

async def main():
    await dp.start_polling()

if __name__ == "__main__":
    asyncio.run(main())

With python-telegram-bot

from telegram import Update
from telegram.ext import Application, MessageHandler, ContextTypes
from tgviz.middleware import TGVizUpdateProcessor

TGVIZ_BOT_TOKEN = "YOUR_TGVIZ_BOT_TOKEN"
TELEGRAM_BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"

# Use is_async=False for synchronous mode (e.g., mandatory subscription feature)
processor = TGVizUpdateProcessor(tgviz_bot_token=TGVIZ_BOT_TOKEN, is_async=True)

def tgviz_middleware(handler_func):
    async def wrapper(update: Update, context: ContextTypes.DEFAULT_TYPE):
        update_dict = update.to_dict()
        return await processor.process_update(
            update=update_dict,
            handler=lambda _: handler_func(update, context)
        )
    return wrapper

# handler with tgviz decorator:
@tgviz_middleware
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    """Echo the user message."""
    await update.message.reply_text(update.message.text)

def main() -> None:
    application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
    application.add_handler(MessageHandler(None, echo))
    application.run_polling()


if __name__ == "__main__":
    main()

2. Sending Updates Directly to TGViz API

If you want full control over what is sent to TGViz, you can use TGVizClient directly.
This is useful if you are using a different Telegram library or need custom logic.

With httpx (Async HTTP Client)

import asyncio
from tgviz.client import TGVizClient

async def main():
    client = TGVizClient(
        tgviz_bot_token="YOUR_TGVIZ_BOT_TOKEN"
    )

    update_data = {"update_id": 123456, "message": {"text": "Hello!"}}
    response = await client.send_update(update_data)
    print(response)

if __name__ == "__main__":
    asyncio.run(main())

With requests (Sync HTTP Client)

import requests

TGVIZ_BOT_TOKEN = "YOUR_TGVIZ_BOT_TOKEN"
API_URL = "https://api.tgviz.com/v1/post-update"

update_data = {"update_id": 123456, "message": {"text": "Hello!"}}
headers = {"X-TGViz-Bot-Token": TGVIZ_BOT_TOKEN, "Content-Type": "application/json"}

response = requests.post(API_URL, json=update_data, headers=headers)
print(response.json())

Contributing

Contributions, bug reports, and suggestions are welcome! Feel free to open an issue or submit a pull request.

License

MIT License

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

tgviz-0.1.2.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tgviz-0.1.2-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file tgviz-0.1.2.tar.gz.

File metadata

  • Download URL: tgviz-0.1.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for tgviz-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5e67ed2a8af772135bf93835f7bbe4f2843f7fa723212163919e7850f235cc40
MD5 5932f47e5eb1702fb5bb75148e1a15e7
BLAKE2b-256 5b604bfe97aa592708e4d1e32089990d4b900ee21133078d207600fa9d4a67ee

See more details on using hashes here.

File details

Details for the file tgviz-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: tgviz-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.11

File hashes

Hashes for tgviz-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0388e83bb115c851578237c016fff6c937144915655193e9eff4b830621a1a32
MD5 15b69e85120a1c30cea39e631b072072
BLAKE2b-256 96f88641ff4283f86641000dbeff83e36eedb2e19c7c5e1bf509f56fe7a14ab0

See more details on using hashes here.

Supported by

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