Skip to main content

Official Python SDK for TOP.TL — post stats, check votes, and manage vote webhooks for your Telegram bot, channel, or group.

Project description

toptl

The official Python SDK for TOP.TL — post stats, check votes, and manage vote webhooks for your Telegram bot, channel, or group listed on top.tl.

Install

pip install toptl

Requires Python 3.9+. Depends on httpx for transport.

Quickstart

Get an API key at https://top.tl/profileAPI Keys. Scope the key to your listing and the operations you need (listing:read, listing:write, votes:read, votes:check).

from toptl import TopTL

client = TopTL("toptl_xxx")

# Fetch a listing
listing = client.get_listing("mybot")
print(listing.title, listing.vote_count)

# Post stats on a listing you own
client.post_stats(
    "mybot",
    member_count=5_000,
    group_count=1_200,
    channel_count=300,
)

# Reward users who voted
check = client.has_voted("mybot", user_id=123456789)
if check.voted:
    grant_premium_access(user_id=123456789)

Async

Same surface, asyncio-native:

import asyncio
from toptl import AsyncTopTL

async def main():
    async with AsyncTopTL("toptl_xxx") as client:
        stats = await client.get_global_stats()
        print(stats.channels, stats.groups, stats.bots)

asyncio.run(main())

Autoposter

For long-running bot processes, the SDK ships with a background autoposter that calls post_stats on an interval and only hits the API when the stats actually changed:

from toptl import TopTL, Autoposter

client = TopTL("toptl_xxx")
poster = Autoposter(
    client,
    "mybot",
    callback=lambda: {"member_count": get_user_count()},
    interval_seconds=30 * 60,   # every 30 min
    only_on_change=True,
)
poster.start()

For cron-style one-shots, skip the autoposter and call client.post_stats(...) directly.

Async version is AsyncAutoposter(async_client, ..., callback=async_fn).

Vote webhooks

Register a URL TOP.TL will POST to whenever someone votes for your listing:

client.set_webhook(
    "mybot",
    url="https://mybot.example.com/toptl-vote",
    reward_title="30-day premium",   # shown to the voter
)

# Send a test request to verify your endpoint
result = client.test_webhook("mybot")
print(result.success, result.status_code)

The webhook payload contains the voting user (userId, firstName, username), the listing, and a timestamp.

Batch stats

Post stats for up to 25 listings in one request:

client.batch_post_stats([
    {"username": "bot1", "member_count": 1200},
    {"username": "bot2", "member_count": 5400},
])

Error handling

Every API error raises a subclass of TopTLError:

from toptl import TopTL, AuthenticationError, NotFoundError, RateLimitError, ValidationError

try:
    client.post_stats("mybot", member_count=5000)
except AuthenticationError:
    ...  # bad key, or missing scope
except NotFoundError:
    ...  # listing does not exist
except RateLimitError:
    ...  # back off and retry
except ValidationError:
    ...  # payload rejected — see exc.response_body

License

MIT — see 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

toptl-0.1.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

toptl-0.1.0-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for toptl-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f861a81eea6b61cd0ecb0c375373cfb6426e1f88e6c6b24c82fd0e2ad1129b8a
MD5 8dbdf1d79402d2c15529578e5a7b0528
BLAKE2b-256 4b03912174ea8e570326cea77458525300cd505d836ba98f7ec9d43549bb57c4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for toptl-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f1d8b520a969076643297c02d951914069dbbf2c811c86a959fc8988634c12f
MD5 c73e27566f4d667aab22ba78650a0f6e
BLAKE2b-256 b1010979cd778a7138f98e77cf611adc1313cf0529b4ff85486223b2b33982b8

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