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.1.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.1-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tgviz-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 8c2777333382af592155ba51d69ae89bdc136f4a677967691aaca9e3b3cf580d
MD5 a6a87396462b70c23775ac381ec89ed4
BLAKE2b-256 c12d76c0ba932ba044f7371080d551eec85c172caf8b391208f9015866332c16

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tgviz-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b52c8e5555e2954ed4654f7e188b301e736b00149b551c24981973ead92b335d
MD5 f5e8564b796bc98156de6f37e819bd36
BLAKE2b-256 c972df0fc922ba62837e9f7b4857d934a38323b3aa84a21969ad87708718e1f4

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