tg-hub
Skill584 starsChanged 59 days agoMITView on GitHub
A skill for reading and writing Telegram data with Python and UV. It depends only on Telethon and uses a local-first architecture: messages are synced to SQLite and then queried offline. On first use, you must log in from the terminal with a phone number verification code. After that, the session is persisted and you do not need to log in again. Supports syncing group and channel messages locally, keyword search, multi-keyword filtering, today's messages, recent messages, speaker rankings, timeline statistics, and more. This skill must be triggered whenever the user mentions "Telegram", "TG", "Telegram", "tg-hub", "sync Telegram messages", "search TG groups", "Telegram keywords", "get TG messages", or any scenario that requires programmatically reading or writing Telegram data.
- Reads credentials
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 205
Control sync frequency: Avoid repeatedly running refresh() at high frequency.
- Very specific: line 217
If you are still using the default api_id=2040, a warning is printed during connection to remind you to switch to your own TG_API_ID / …
≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
tg-hub
Based on: jackwener/tg-cli (Apache-2.0)
This skill simplifies the original repository as follows:
- Removed the
click/rich/python-dotenv/pyyamldependencies- Kept only
telethonas a third-party dependency- Removed the CLI layer and encapsulated all functionality as a synchronous Python API
- Changed the default session/db path to
/var/minis/workspace/tg-hub/- Changed configuration to read environment variables directly, with no
.envfile required
Architecture: Local-First
Telegram MTProto (telethon)
↓ sync / refresh (incremental)
Local SQLite ~/.tg-hub/messages.db
↓ search / today / recent / filter (offline)
Structured data- Read operations (search/today/recent): query the local SQLite database, with no network access, and respond in milliseconds
- Write operations (sync/refresh): connect to Telegram to fetch new messages and write them incrementally to SQLite
- Session file:
~/.tg-hub/tg_hub.session
File Structure
/var/minis/skills/tg-hub/
├── SKILL.md
├── pyproject.toml # telethon only
└── scripts/
├── __init__.py
├── config.py # Configuration (environment variables / default paths)
├── db.py # SQLite message storage
├── exceptions.py # Structured exceptions
└── client.py # TGClient core class (all APIs)First-Time Login (Must Be Done in Terminal)
tg-hub uses the MTProto protocol (not the Bot API), so you need to log in with your Telegram account.
Recommendation: Use your own
TG_API_ID/TG_API_HASHwhenever possible. I have synced the upstream tg-cli anti-risk-control implementation: it uses a Telegram Desktop 5.x fingerprint and prints a warning if you continue using the defaultapi_id=2040. The public app ID is only a fallback. Using your own credentials is still recommended for long-term use.
1. Open Terminal
2. (Recommended) Set your own TG_API_ID / TG_API_HASH first
3. cd /var/minis/skills/tg-hub
4. uv run python -c "
import sys; sys.path.insert(0,'.')
from scripts.client import TGClient
me = TGClient().login()
print('Login successful:', me)
"
5. Enter your phone number when prompted (in +86XXXXXXXXXX format)
6. Enter the verification code received in the Telegram app
7. After login succeeds, the session is saved automatically and future logins are not requiredIf you do not have your own credentials for now, you can log in with the built-in public credentials first. If you encounter login or fetch errors, switch to your own APP ID first.
Open Terminal to log in
Quick Start
Environment Setup
cd /var/minis/skills/tg-hub
uv syncPython Usage
import sys
sys.path.insert(0, "/var/minis/skills/tg-hub")
from scripts.client import TGClient
client = TGClient()
# View the current account
me = client.whoami()
print(me["name"], me["phone"])
# List all conversations (fetched from TG in real time)
chats = client.list_chats()
for c in chats[:10]:
print(f" [{c['type']}] {c['name']} Unread: {c['unread']}")
# Incrementally sync a single group
n = client.sync("Group name or username", limit=1000)
print(f"Added {n} messages")
# Quickly refresh all groups (up to 500 new messages per group)
# Slight throttling is enabled by default; you can also limit this round to only the first 30 chats
result = client.refresh(delay=1.0, max_chats=30)
for name, count in result.items():
if count > 0:
print(f" {name}: +{count}")
# Search by keyword
msgs = client.search("Python", hours=48)
for m in msgs:
print(f"[{m['chat_name']}] {m['sender_name']}: {m['content'][:80]}")
# Multi-keyword filtering (OR logic)
msgs = client.filter("hiring,remote,part-time", hours=24)
# Today's messages
msgs = client.today()
# Messages from the last 12 hours
msgs = client.recent(hours=12, limit=200)
# Speaker rankings
top = client.top_senders(hours=24)
for t in top[:5]:
print(f" {t['sender_name']}: {t['msg_count']} messages")
# Timeline statistics
tl = client.timeline(granularity="hour", hours=48)
# Local database statistics
stats = client.stats()
print(f"{stats['total']} local messages across {len(stats['chats'])} groups")API Quick Reference
Authentication
| Method | Description |
login() | Interactive login (first time, requires terminal) |
whoami() | Get current account information |
Sync (Online)
| Method | Description |
list_chats(chat_type=None) | List all conversations (real time) |
sync(chat, limit=5000) | Sync a single group to local SQLite |
sync_all(limit_per_chat=5000, delay=1.0, max_chats=None) | Sync all groups (with throttling/count limit) |
refresh(limit_per_chat=500, delay=1.0, max_chats=None) | Quick incremental refresh (recommended for daily use) |
Query (Local, Offline)
| Method | Description |
search(keyword, *, chat, sender, hours, regex, limit) | Keyword/regex search |
filter(keywords, *, chat, hours) | Multi-keyword OR filtering |
today(chat=None) | Today's messages |
recent(hours=24, *, chat, sender, limit) | Messages from the last N hours |
top_senders(chat, hours, limit) | Speaker rankings |
timeline(chat, hours, granularity) | Timeline statistics |
stats() | Database statistics |
local_chats() | List of locally synced groups |
delete_chat(chat) | Delete local messages for a group |
Environment Variables
| Variable | Default Value | Description |
TG_API_ID | 2040 (fallback only) | Recommended: replace with your own API ID |
TG_API_HASH | Built in (fallback only) | Recommended: replace with your own API Hash |
TG_SESSION_NAME | tg_hub | Session filename |
TG_DATA_DIR | ~/.tg-hub | Data directory |
TG_DB_PATH | {TG_DATA_DIR}/messages.db | SQLite path |
TG_DEVICE_MODEL | Desktop | Telethon client device model |
TG_SYSTEM_VERSION | macOS 15.3 | Telethon client system version |
TG_APP_VERSION | 5.12.1 | Telethon client version |
TG_LANG_CODE | en | Client language code |
TG_SYSTEM_LANG_CODE | en-US | System language code |
Account Security Recommendations
- Use your own API credentials whenever possible: Go to
https://my.telegram.org, create an application, and then setTG_API_ID/TG_API_HASH. - Control sync frequency: Avoid repeatedly running
refresh()at high frequency. - Use
delayandmax_chats: For daily incremental refreshes, we recommend limiting the number of chats synced per round and keeping an interval between chats. - Do not be too aggressive with the first full sync: tg-hub automatically applies a lower fetch limit for the first sync of each chat.
- Prefer read operations: Local queries such as search and statistics do not use the network, so they are much lower risk than frequent syncs.
Notes
- The first login must be completed in an interactive terminal (a verification code is required).
- Using your own
TG_API_ID/TG_API_HASHis strongly recommended to avoid risk-control issues caused by abuse of the public APP ID. - tg-hub is aligned with the upstream tg-cli Telegram Desktop 5.x client fingerprint and retains environment variable overrides to reduce the risk of abnormal fingerprints.
- If you are still using the default
api_id=2040, a warning is printed during connection to remind you to switch to your ownTG_API_ID/TG_API_HASH. - The session file is stored at
/var/minis/workspace/tg-hub/tg_hub.session. Keep it safe. - The first run of
sync_allmay take a long time, depending on the number of groups and the amount of historical messages. - We recommend using
refresh()for daily incremental updates andsync(chat, limit=10000)for the initial full sync. - Telegram applies rate limits to API requests. During large syncs, Telethon automatically handles flood waits.
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 1
- lsongdev/skills★ 097% same
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository56 other files it gives its agents.
- android-devSkill
- android-ui-automationSkill
- apple-remindersSkill
- appstoreprice-hubSkill
- asc-cliSkill
- bilibili-hubSkill
- Clash 控制器Skill
- cloudflare-dnsSkill
- codex-imageSkill
- codex-on-ishSkill
- contact-vcard-extractorSkill
- coredevice-toolkitSkill
- deepseek-usageSkill
- doubao-ttsSkill
- douyin-downloaderSkill
- evidence-chain-builderSkill
- exa-searchSkill
- free-ttsSkill
- fucai3d-latestSkill
- generative-ui-minisSkill
- github-sync-helperSkill
- health-sleep-analysisSkill
- health-training-checkinSkill
- hyperframes-cliSkill
- hyperframesSkill
- ios-devSkill
- korea-stockSkill
- maimai-hubSkill
- memos-apiSkill
- nano-bananaSkill
- notion-hubSkill
- openstreetmap-markerSkill
- pdf-converterSkill
- production-agent-publicSkill
- qbt-hubSkill
- quark-hubSkill
- reverse-dcfSkill
- self-improving-agentSkill
- shortcuts-builderSkill
- shortcut-share-fileSkill
- spotify-hubSkill
- stock-fund-flowSkill
- stock-priceSkill
- tavily-searchSkill
- todoocardSkill
- twitter-downloaderSkill
- twitter-x-hubSkill
- us-stockSkill
- web-content-extractorSkill
- web-searchSkill
- weibo-hubSkill
- whenpeakSkill
- xianyu-hubSkill
- xiaohongshu-hubSkill
- youtube-downloaderSkill
- ytmusic-hubSkill
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

