Skip to main content

aiogram plugin for TOP.TL — autopost bot stats, gate handlers behind votes, handle vote webhooks.

Project description

toptl-aiogram

PyPI version Python versions Downloads License aiogram TOP.TL

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

Install

pip install toptl-aiogram

Python 3.9+. Pulls in toptl>=0.1.1 and aiogram>=3.0.

Quick start

import asyncio
from aiogram import Bot, Dispatcher
from aiogram.filters import Command
from aiogram.types import Message
from toptl import AsyncTopTL
from toptl_aiogram import setup_toptl, vote_required

async def main():
    bot = Bot("BOT_TOKEN")
    dp = Dispatcher()
    client = AsyncTopTL("toptl_xxx")

    # One call installs the middleware + autoposter, tied to dp's
    # startup/shutdown hooks — no background task bookkeeping for you.
    plugin = setup_toptl(dp, client, "mybot")

    @dp.message(Command("premium"))
    @vote_required(plugin, vote_url="https://top.tl/mybot")
    async def premium(message: Message):
        await message.answer("Thanks for voting!")

    await dp.start_polling(bot)

asyncio.run(main())

setup_toptl does three things:

  1. Registers a middleware on dp.update.middleware that records unique user / group / channel IDs from every update (messages, callbacks, chat_member, …).
  2. Hooks an AsyncAutoposter into the dispatcher's startup/shutdown events — it flushes stats every 30 min, only when counts changed.
  3. Returns a TopTLPlugin handle you use for vote checks and manual flushes.

Vote gating

@dp.message(Command("premium"))
@vote_required(plugin, message="Vote first: https://top.tl/mybot")
async def premium(message: Message):
    ...

Works on message and callback-query handlers. Network or auth failures fall through as "not voted" and log at ERROR level — vote gates never brick your bot.

For checks inside existing logic:

@dp.message(Command("check"))
async def check(message: Message):
    if await plugin.has_voted(message.from_user.id):
        await message.answer("You voted, thanks!")
    else:
        await message.answer("Please vote at https://top.tl/mybot")

The plugin handle is also injected into handler data under data["toptl"]:

async def handler(message: Message, toptl):  # aiogram reads the param name
    if await toptl.has_voted(message.from_user.id):
        ...

Tuning the autoposter

plugin = setup_toptl(
    dp, client, "mybot",
    interval_seconds=15 * 60,  # flush every 15 min instead of 30
)

Manual flush (e.g. before shutdown):

await plugin.post_now()

Requirements

  • aiogram>=3.0
  • Python 3.9+

License

MIT.


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

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_aiogram-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

toptl_aiogram-0.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for toptl_aiogram-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cc8d27ff495f1dc987d70fc453d5806473b18771c9b373f671631f30225a7aba
MD5 6d4277c661310e94521bdb6becac2271
BLAKE2b-256 e3c6b54728f732b463dc5845b66a7bfd5bbb4f167f695f85679c0fcbdb101e12

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for toptl_aiogram-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba50fba9a03cf48a93287b4d75ebe117b3197b5a0edce138c737777d0a7ece99
MD5 fbf7637b8eabea8d6decab5b0c0ec2c2
BLAKE2b-256 0748e4f1751d96ac151295912915ea364d2cd81c5c24a77bbb5e8a2dab068d63

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