Telethon-powered Telegram CLI for local sync, search, and agent-friendly retrieval
Project description
tg-cli
Telethon-powered Telegram CLI for local-first sync, search, export, and agent-friendly retrieval.
It uses your own Telegram account over MTProto, not the Bot API. Messages are synced into local SQLite,
so humans and AI agents can query the same cache quickly with --json or --yaml.
Features
- Sync Telegram dialogs into a local SQLite cache
- Search by keyword or regex, with chat, sender, and time filters
- Browse recent messages, today's messages, top senders, and timelines
- Export messages as text, JSON, or YAML
- Keep a near-real-time cache with
tg listen --persist - Prefer YAML for AI agents when a strict JSON parser is not required
Installation
# Install from PyPI
uv tool install kabi-tg-cli
# Or: pipx / pip
pipx install kabi-tg-cli
pip install kabi-tg-cli
Install from GitHub:
uv tool install git+https://github.com/jackwener/tg-cli.git
Install from source:
git clone git@github.com:jackwener/tg-cli.git
cd tg-cli
uv sync --extra dev
Quick Start
# Configure credentials first
export TG_API_ID=123456
export TG_API_HASH=your_telegram_app_hash
# Or create a .env file with the same variables
# Login (first run) — enter phone + verification code
tg chats
# Check who you are
tg whoami
# Refresh local cache from all current dialogs
tg refresh
# See today's messages
tg today
# Search
tg search "Rust"
tg search "Rust" --sender "Alice" --hours 48
tg search "Rust|Golang" --regex --hours 72
tg recent --hours 24 --limit 20 --yaml
tg search "Rust" --sync-first --yaml
# Filter by keywords (comma-separated, OR logic)
tg filter "Rust,Golang,Java" --hours 48 --sync-first
# Keep a near-real-time local cache
tg listen --persist
# Send a message
tg send "GroupName" "Hello!"
Why This Exists
tg-cli is intentionally local-first:
tg refresh,tg sync,tg sync-all, andtg listeningest data from Telegramtoday,recent,search,filter,stats,top, andtimelineread from local SQLite
That makes repeated analysis fast, scriptable, and suitable for AI agents. If you need fresh data right
before a query, use --sync-first.
Commands
Telegram (tg ...)
| Command | Description |
|---|---|
tg chats [--type group] [--json|--yaml] |
List joined chats |
tg whoami [--json|--yaml] |
Show current user info |
tg history CHAT -n 1000 |
Fetch historical messages |
tg sync CHAT |
Incremental sync (only new messages) |
tg sync-all [--json|--yaml] |
Low-level sync for all current dialogs |
tg refresh [--json|--yaml] |
Recommended daily refresh entrypoint |
tg listen [CHATS...] [--persist] |
Real-time listener with optional auto-reconnect |
tg info CHAT [--json|--yaml] |
Show detailed chat info |
tg send CHAT "msg" |
Send a message |
Query
| Command | Description |
|---|---|
search KEYWORD [-c NAME] [-s SENDER] [--hours N] [--regex] [--sync-first] [--json|--yaml] |
Search stored messages with chat/sender/time filters or regex |
recent [-c NAME] [-s SENDER] [--hours N] [-n LIMIT] [--sync-first] [--json|--yaml] |
Browse recent messages without a keyword |
filter KEYWORDS [-c NAME] [--hours N] [--sync-first] [--json|--yaml] |
Multi-keyword filter (OR logic, highlighted) |
stats [--sync-first] [--json|--yaml] |
Show message statistics |
top [-c NAME] [--hours 24] [--sync-first] [--json|--yaml] |
Most active senders |
timeline [-c NAME] [--by day|hour] [--sync-first] [--json|--yaml] |
Message activity bar chart |
today [-c NAME] [--sync-first] [--json|--yaml] |
Show today's messages by chat |
| Command | Description |
|---|---|
export CHAT [-f text|json|yaml] [-o FILE] [--hours N] |
Export messages |
purge CHAT [-y] |
Delete stored messages |
Global Options
| Option | Description |
|---|---|
-v, --verbose |
Enable debug logging |
--version |
Show version |
Setup
uv tool install kabi-tg-cli # or: pip install kabi-tg-cli
# Set TG_API_ID and TG_API_HASH in your shell or a .env file
tg chats
After that, tg-cli stores your session locally and reuses it.
Required:
- Set
TG_API_IDandTG_API_HASHin your environment or.env
Optional:
- Custom data dir:
DATA_DIR=./dataorDB_PATH=./data/messages.db - Custom session file name:
TG_SESSION_NAME=my_session
Apply for your own Telegram app credentials at my.telegram.org/apps.
Refresh Modes
tg-cli is local-first: search, recent, today, filter, stats, top, and timeline
read from the local SQLite cache, not directly from Telegram.
tg refreshis the recommended daily command. It refreshes all current dialogs and prints a short summary.tg sync-allis the lower-level primitive if you want explicit control in scripts.--sync-firstis available on query commands when you want fresh data before reading.tg listen --persistkeeps reconnecting automatically and is the closest thing to a live cache.
Examples:
tg refresh
tg today --sync-first --yaml
tg top --hours 24 --sync-first
tg listen --persist --retry-seconds 5
Automation Examples
If you do not want to run tg refresh manually, use a scheduler.
cron
systemd user timer
See:
Typical flow:
mkdir -p ~/.config/systemd/user
cp examples/systemd/tg-refresh.service ~/.config/systemd/user/
cp examples/systemd/tg-refresh.timer ~/.config/systemd/user/
systemctl --user daemon-reload
systemctl --user enable --now tg-refresh.timer
Manual Smoke Test
With valid TG_API_ID, TG_API_HASH, and a working local session:
tg whoami
tg refresh --yaml
tg recent --hours 24 --limit 5 --yaml
tg search "test" --hours 24 --sync-first --yaml
tg today --sync-first
These commands cover auth, sync, local reads, and structured output without sending messages.
Troubleshooting
Missing TG_API_ID / TG_API_HASH- Apply for your own app credentials at my.telegram.org/apps.
No messages today- Run
tg refreshfirst, or usetg today --sync-first.
- Run
Chat '...' not found in database- Run
tg refreshfirst, or use the numericchat_idfromtg chats --yaml.
- Run
- Repeatedly running
sync-all- Prefer
tg refreshfor daily use,--sync-firstfor single queries, ortg listen --persistfor a near-live cache.
- Prefer
Architecture
src/tg_cli/
├── cli/
│ ├── main.py # Click CLI entry point + verbose
│ ├── tg.py # Telegram: chats, sync, refresh, listen, whoami, send
│ ├── query.py # Query: search, regex, recent, filter, stats, today, top, timeline
├── client.py # Telethon client (connection reuse)
├── config.py # Config and required user-provided credentials
├── db.py # SQLite message store
Use as AI Agent Skill
tg-cli ships with a SKILL.md for AI agent integration.
For AI agents, prefer --yaml when a downstream parser does not strictly require JSON.
YAML is usually shorter than pretty-printed JSON and saves tokens while remaining structured.
Recommended agent pattern:
tg refresh --yaml
tg recent --hours 24 --sync-first --yaml
tg filter "招聘,remote" --hours 24 --sync-first --yaml
The recommended agent workflow is:
tg refresh --yamltg chats --yamltg recent --hours 24 --sync-first --yamltg search "keyword" --chat "GroupName" --sync-first --yaml
Claude Code / Antigravity
mkdir -p .agents/skills
git clone git@github.com:jackwener/tg-cli.git .agents/skills/tg-cli
OpenClaw / ClawHub
clawhub install tg-cli
License
Apache-2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kabi_tg_cli-0.4.1.tar.gz.
File metadata
- Download URL: kabi_tg_cli-0.4.1.tar.gz
- Upload date:
- Size: 31.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fee90d6f4b4244b0f854dc8ec9dd759438c7b02513dee1ec2bfc0154902f75d0
|
|
| MD5 |
f5bf9e8618ae062a3ddaef484b427639
|
|
| BLAKE2b-256 |
5fcebb5af3f97fdc276fc9413871096770a112c8a2621f09d889d67b346beb69
|
Provenance
The following attestation bundles were made for kabi_tg_cli-0.4.1.tar.gz:
Publisher:
publish.yml on jackwener/tg-cli
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kabi_tg_cli-0.4.1.tar.gz -
Subject digest:
fee90d6f4b4244b0f854dc8ec9dd759438c7b02513dee1ec2bfc0154902f75d0 - Sigstore transparency entry: 1072198084
- Sigstore integration time:
-
Permalink:
jackwener/tg-cli@5accf54d5e559bbc403c20fb75cf5d2f849991c2 -
Branch / Tag:
refs/tags/v0.4.1 - Owner: https://github.com/jackwener
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5accf54d5e559bbc403c20fb75cf5d2f849991c2 -
Trigger Event:
push
-
Statement type:
File details
Details for the file kabi_tg_cli-0.4.1-py3-none-any.whl.
File metadata
- Download URL: kabi_tg_cli-0.4.1-py3-none-any.whl
- Upload date:
- Size: 30.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e12f617c0f18652449a5c7b45590deb34467263cb365e15909c5c5b702ce6227
|
|
| MD5 |
46611e965515bb1909e571707675a482
|
|
| BLAKE2b-256 |
baca666e98fcf4e0853acd0defb778a0eca2018ba7fea2db961b429d4628f03e
|
Provenance
The following attestation bundles were made for kabi_tg_cli-0.4.1-py3-none-any.whl:
Publisher:
publish.yml on jackwener/tg-cli
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kabi_tg_cli-0.4.1-py3-none-any.whl -
Subject digest:
e12f617c0f18652449a5c7b45590deb34467263cb365e15909c5c5b702ce6227 - Sigstore transparency entry: 1072198128
- Sigstore integration time:
-
Permalink:
jackwener/tg-cli@5accf54d5e559bbc403c20fb75cf5d2f849991c2 -
Branch / Tag:
refs/tags/v0.4.1 - Owner: https://github.com/jackwener
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@5accf54d5e559bbc403c20fb75cf5d2f849991c2 -
Trigger Event:
push
-
Statement type: