Skip to main content

Telethon-powered Telegram CLI for local sync, search, and agent-friendly retrieval

Project description

tg-cli

CI PyPI License

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, and tg listen ingest data from Telegram
  • today, recent, search, filter, stats, top, and timeline read 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_ID and TG_API_HASH in your environment or .env

Optional:

  • Custom data dir: DATA_DIR=./data or DB_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 refresh is the recommended daily command. It refreshes all current dialogs and prints a short summary.
  • tg sync-all is the lower-level primitive if you want explicit control in scripts.
  • --sync-first is available on query commands when you want fresh data before reading.
  • tg listen --persist keeps 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

See examples/tg-refresh.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
  • No messages today
    • Run tg refresh first, or use tg today --sync-first.
  • Chat '...' not found in database
    • Run tg refresh first, or use the numeric chat_id from tg chats --yaml.
  • Repeatedly running sync-all
    • Prefer tg refresh for daily use, --sync-first for single queries, or tg listen --persist for a near-live cache.

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:

  1. tg refresh --yaml
  2. tg chats --yaml
  3. tg recent --hours 24 --sync-first --yaml
  4. tg 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kabi_tg_cli-0.4.1.tar.gz (31.0 kB view details)

Uploaded Source

Built Distribution

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

kabi_tg_cli-0.4.1-py3-none-any.whl (30.4 kB view details)

Uploaded Python 3

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

Hashes for kabi_tg_cli-0.4.1.tar.gz
Algorithm Hash digest
SHA256 fee90d6f4b4244b0f854dc8ec9dd759438c7b02513dee1ec2bfc0154902f75d0
MD5 f5bf9e8618ae062a3ddaef484b427639
BLAKE2b-256 5fcebb5af3f97fdc276fc9413871096770a112c8a2621f09d889d67b346beb69

See more details on using hashes here.

Provenance

The following attestation bundles were made for kabi_tg_cli-0.4.1.tar.gz:

Publisher: publish.yml on jackwener/tg-cli

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for kabi_tg_cli-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e12f617c0f18652449a5c7b45590deb34467263cb365e15909c5c5b702ce6227
MD5 46611e965515bb1909e571707675a482
BLAKE2b-256 baca666e98fcf4e0853acd0defb778a0eca2018ba7fea2db961b429d4628f03e

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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