Skip to main content

python-telegram-bot plugin for TOP.TL — autopost bot stats, gate commands behind votes, handle vote webhooks.

Project description

toptl-ptb

PyPI version Python versions Downloads License python-telegram-bot TOP.TL

Official TOP.TL plugin for python-telegram-bot. One call wires up autoposted bot stats, vote-gated commands, and webhook helpers — all on top of the toptl SDK.

Install

pip install toptl-ptb

Python 3.9+. Pulls in toptl>=0.1.1 and python-telegram-bot>=21.0.

Quick start

from telegram.ext import ApplicationBuilder, CommandHandler
from toptl import AsyncTopTL
from toptl_ptb import setup_toptl, vote_required

app = ApplicationBuilder().token("BOT_TOKEN").build()
client = AsyncTopTL("toptl_xxx")

# One line installs the tracker + autoposter on the Application.
plugin = setup_toptl(app, client, "mybot")

@vote_required(plugin, vote_url="https://top.tl/mybot")
async def premium(update, context):
    await update.message.reply_text("Thanks for voting!")

app.add_handler(CommandHandler("premium", premium))
app.run_polling()

setup_toptl does three things:

  1. Adds a low-priority TypeHandler that records unique user / group / channel IDs from every update.
  2. Schedules a JobQueue task that flushes those counts to TOP.TL every 30 min — only when they changed, so you're not burning API quota on idle minutes.
  3. Returns a TopTLPlugin handle you use for vote checks and manual flushes.

Vote gating

The @vote_required(plugin) decorator short-circuits the handler and replies with a prompt when the user hasn't voted:

@vote_required(plugin, message="Vote to unlock this: https://top.tl/mybot")
async def premium(update, context):
    ...

For checks inside existing logic:

if await plugin.has_voted(update.effective_user.id):
    ...

Both paths fail-open — network errors never block your handler, they just count as "not voted" and log at ERROR level.

Tuning the autoposter

plugin = setup_toptl(
    app, client, "mybot",
    interval_seconds=15 * 60,   # flush every 15 min instead of 30
    first_seconds=5,            # first flush 5s after startup
    handler_group=-1,           # override the default -100 group
)

To flush manually — for example from a graceful-shutdown hook:

async def post_shutdown(application):
    await plugin.post_now()
    await client.aclose()

app.post_shutdown = post_shutdown

Requirements

  • python-telegram-bot[job-queue]>=21.0 — the [job-queue] extra enables the autoposter. Without it you can still call plugin.post_now() from your own schedule (e.g. an existing apscheduler).
  • Python 3.9 or newer.

License

MIT.


Part of the TOP.TL developer ecosystem. Issues and contributions: top-tl/ptb.

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

toptl_ptb-0.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

toptl_ptb-0.1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file toptl_ptb-0.1.0.tar.gz.

File metadata

  • Download URL: toptl_ptb-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for toptl_ptb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 74d5481ffe4b5d3880b6675b35bbf51ecd20cfa755774fb982065b6cd5fd61c9
MD5 67c7b334d213c8608d177d18a62f266b
BLAKE2b-256 7914dd32be8f47571a64ad0c1e58dbd96848016c3634aa0239a2dd8b491f84d5

See more details on using hashes here.

File details

Details for the file toptl_ptb-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: toptl_ptb-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for toptl_ptb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 07b67f72b7ed73bbfafc4da5fb93fdf605c5f724b360f198817d3dd38c579feb
MD5 92326b326126e7304ceb263c8360e18c
BLAKE2b-256 486cca1a7ff2a7d8bcea496faf77fbaf86b5dd60516b4008d8b12847ad6115d4

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