Skip to main content

tg-toolkit: read Telegram as yourself, paced and incremental; write Telegram Desktop export JSON; push to your own bot. Beside it, a message with an emoji and a code entity at UTF-16 offset 7, length 22, and the text_entities that to_desktop_message produces for it.

CI PyPI Python versions License: MIT

Read Telegram through your own account: paced, incremental, safe on FloodWait. Convert messages to and from Telegram Desktop export JSON.

tg-toolkit is a small Python library on top of Telethon. It reads channels and chats with a pause between requests, continues where the last run stopped, and sleeps through FloodWait instead of crashing. It also sends plain-text pushes to your own bot.

It has three modules. Use any one without the others:

module needs telethon what it does
tg_toolkit.reader only when called paced, incremental, bounded message reads
tg_toolkit.export no Telegram Desktop export, both directions
tg_toolkit.push no (stdlib urllib) Bot API message and desktop notify-send

Install

pip install tg-toolkit
# or
uv add tg-toolkit

Python 3.12 or newer. Reading needs an api_id / api_hash from my.telegram.org and a Telethon session. export and push need neither.

First use

Collect new posts from a channel, only those since the last run:

from tg_toolkit import last_seen_msg_id, read_messages, window_cutoff

def to_post(msg):
    text = (msg.message or "").strip()
    if not text:
        return None                       # returning None skips the message
    return RawPost(post_id=f"telegram:{handle}:{msg.id}", text=text, ...)

posts = read_messages(
    api_id=api_id, api_hash=api_hash, session=session, target=f"@{handle}",
    mapper=to_post,
    min_id=last_seen_msg_id(cursor),      # incremental
    since=window_cutoff(60),              # recency window, days
    max_scan=500,                         # backstop
    label=f"@{handle}",
    sort_key=lambda p: int(p.post_id.rsplit(":", 1)[-1]),   # last row = new cursor
)

More examples (several chats, live export, offline parse, push): docs/usage.md.

What you get back

A message with an emoji and a code entity, run through to_desktop_message (output from the test fakes):

>>> to_desktop_message(msg)["text_entities"]
['🎉 run ', {'type': 'code', 'text': 'pip install tg-toolkit'}]

Telegram counts entity offsets in UTF-16 code units, and the party popper takes two of them. Slicing the Python string at offset 7 would cut one character too far. export slices through utf-16-le, so the code span comes out whole.

How the reader works

How scan_messages reads a chat: the cursor becomes min_id; iter_messages walks newest-first with 1 second between requests; a FloodWaitError sleeps the seconds the server asks for and resumes the same iterator; scan_decision returns TAKE (message goes to your mapper), SKIP (date at or after until) or STOP (older than since, or max_scan reached).

  • Uses your user session, never a bot token. A bot can't read arbitrary channels or your own dialogs.
  • Pauses 1 second between requests (wait_time=1.0), under Telegram's limit.
  • On a long FloodWaitError, iter_flood_safe sleeps the asked time and resumes the same iterator.
  • Scans newest-first and stops at since or max_scan, so a first run is not a full backfill.
  • Resumes by min_id. A broken cursor becomes 0 (a windowed scan) instead of an error.

Full list of reader, export and push rules: docs/behavior.md. Step-by-step flows: docs/logic-map.md.

Limits

  • Export entities that overlap or nest are skipped, not merged.
  • Push is best-effort: failures are logged, never raised. Send only to your own private chat with the bot.
  • export matches Telethon objects by class name. Exotic subclasses take the user / group_chat branch.

More

Tests

uv sync --extra dev && uv run pytest -q

No network, no session file, no real TelegramClient.

License

MIT

Metadata

Release files for tg-toolkit 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tg-toolkit 0.1.4
File Size Uploaded Provenance Actions
tg_toolkit-0.1.4.tar.gz 92.6 kB With provenance Details for tg_toolkit-0.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tg-toolkit 0.1.4
File Interpreter ABI Platform Actions
tg_toolkit-0.1.4-py3-none-any.whl Python 3 none any Details for tg_toolkit-0.1.4-py3-none-any.whl

Total release size: 107.9 kB

Release files / tg_toolkit-0.1.4.tar.gz

Download URL tg_toolkit-0.1.4.tar.gz
Size 92.6 kB
Tags Source
SHA-256 checksum
How to use checksums
b77ee52b29e58b190726dced54431d4b48623697bdf687b43b238279a401fc8e
BLAKE2b-256 checksum
How to use checksums
1a20fa8f58a06a21f2494077f3c07e1e18e138b228e4055e6c6a88ba86f08c37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

Release files / tg_toolkit-0.1.4-py3-none-any.whl

Download URL tg_toolkit-0.1.4-py3-none-any.whl
Size 15.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bba1c501d30b938a7d71ea4314f19a33107bf60f123a4299fd82f644822533fd
BLAKE2b-256 checksum
How to use checksums
bde9b6f4dafe03d22c23d0e27c05a0a22d0b2a250243c33c88ad3f6afc823929
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

Security for tg-toolkit 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tg_toolkit-0.1.4.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 5, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page