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

PyPI version Python versions Downloads License Typed TOP.TL

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.1.tar.gz (9.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-0.1.1-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: toptl-0.1.1.tar.gz
  • Upload date:
  • Size: 9.2 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.1.tar.gz
Algorithm Hash digest
SHA256 a081bf40cb4d5483b2dc5efced58c58b2aa25dd632bcae6437e46834fbdeb44c
MD5 182a076b9b1ea3f92d955174d1f3af9d
BLAKE2b-256 8fd877ad0561975ddaf0bb79703ec53a8683da72d3706c4f5d8f6fc13f86002f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: toptl-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d28284c4e280d66be8ae1a6b755800034f6eab08bfc616d36537b0db9e2c4df0
MD5 28cbdea3791664e41a99e67b1e725504
BLAKE2b-256 b10a7da254bd1af1eb34f94d38155c3e4200b7d314db088e437e25265b38474d

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