Skip to main content

CLI tool to read Telegram messages from the terminal

Project description

✈️ tgcli — Telegram for your terminal and your AI agents.

Give AI agents (Claude Code, Codex, Cursor, etc.) direct access to your Telegram conversations. Structured JSONL output, minimal command surface, fuzzy name resolution. Works equally well for humans with --pretty.

Features

  • JSONL by default — one JSON object per line; agents parse it natively, scripts pipe it freely
  • Minimal surface — a handful of commands; easy for agents to discover and invoke
  • Fuzzy resolution — chat and user names match by display name (no numeric IDs required)
  • --pretty for humans — Rich tables when you want to read output yourself
  • Secure session storage — Telethon session key stored in system keychain via keyring

Installation

One-liner (installs uv if needed):

curl -fsSL https://raw.githubusercontent.com/tksohishi/tgcli/main/install.sh | bash

Homebrew:

brew install tksohishi/tap/tgcli

With uv:

uv tool install pytgcli

From source:

git clone https://github.com/tksohishi/tgcli.git
cd tgcli
uv tool install .

Quick Start

1. Get API Credentials

Create a Telegram API app at my.telegram.org/apps. You'll get an api_id and api_hash.

2. Authenticate

tg auth

This walks you through setup: saves your API credentials to ~/.config/tgcli/config.toml, then logs in with phone number + verification code.

3. Read Messages

tg read "Alice"
tg read "Finance Team" --limit 20
tg read "Finance Team" -q "budget"
tg read "Finance Team" -q "deadline" --from "Alice" --after 2025-01-01

4. View Context

tg context "Finance Team" 12345

Use with AI Agents

Once authenticated, any AI coding agent with shell access can use tgcli directly. A few examples:

Ask Claude Code to summarize a group chat:

"Read the last 30 messages from 'Engineering' and summarize the key decisions."

The agent runs tg read "Engineering" --limit 30, parses the JSONL, and responds.

Find a past conversation:

"What did I discuss with Alice last week about the deployment?"

The agent runs tg read "Alice" -q "deployment" --after 2025-02-14 and surfaces the relevant messages.

Pipe into scripts:

tg read "Alerts" --limit 100 | jq 'select(.text | test("ERROR"))'

No wrapper libraries or API adapters needed. The structured output and simple command surface mean agents can use tgcli out of the box.

Commands

tg auth

Smart entrypoint: creates config if missing, logs in if needed, shows status if already authenticated.

Explicit subcommands:

  • tg auth login - interactive login (phone + code/2FA)
  • tg auth logout - remove session from system keychain
  • tg auth status - show auth state

tg chats

List your Telegram chats. Returns JSONL by default.

Flag Description
--filter Fuzzy filter by chat name
--limit Max chats to list (default 100)
--pretty Rich table output instead of JSONL

tg read <chat>

Read recent messages from a chat. Returns JSONL by default, newest first.

Flag Description
--query/-q Filter messages by text
--from Filter by sender
--limit Max messages (default 50)
--head Oldest messages first
--after Only messages after date (YYYY-MM-DD)
--before Only messages before date (YYYY-MM-DD)
--pretty Rich table output instead of JSONL

tg update

Upgrade tgcli to the latest version. Detects the install method and runs the right command (or tells you what to run for Homebrew installs).

tgcli checks PyPI for new versions once per day and prints a notice to stderr when an update is available. Set TGCLI_NO_UPDATE_CHECK=1 to disable.

tg context <chat> <message_id>

View a message with surrounding context. Returns JSONL by default.

Flag Description
--context Messages before/after (default 5)
--pretty Rich text output instead of JSONL

Configuration

Config lives at ~/.config/tgcli/config.toml:

api_id = 123456
api_hash = "your_api_hash"

Alternatively, set TELEGRAM_API_ID and TELEGRAM_API_HASH environment variables.

Contributing

uv sync --group dev
uv run pytest
uv run ruff check

Tests mock Telethon entirely; no real API calls are made.

License

MIT

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

pytgcli-0.8.2.tar.gz (55.1 kB view details)

Uploaded Source

Built Distribution

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

pytgcli-0.8.2-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file pytgcli-0.8.2.tar.gz.

File metadata

  • Download URL: pytgcli-0.8.2.tar.gz
  • Upload date:
  • Size: 55.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pytgcli-0.8.2.tar.gz
Algorithm Hash digest
SHA256 b4462973844daed9f38d3887ad20f66111bd68026d87a147c7904efda46fd593
MD5 a4edb8f0257fe2fce8c11d719542eefb
BLAKE2b-256 5564299c08eb688a6dea59c3c8a19b44001c2390484ba40a4bc7a1abf121585b

See more details on using hashes here.

File details

Details for the file pytgcli-0.8.2-py3-none-any.whl.

File metadata

  • Download URL: pytgcli-0.8.2-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.7 {"installer":{"name":"uv","version":"0.10.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pytgcli-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 39d8b05323db857314add7aff78f928cde47dfb7fe8b8bc5222bc82189203496
MD5 b829d1a984d2404b3b0cd2440524a665
BLAKE2b-256 211d1d6f5c2bd165d3f3b516c1feac60901df680039608df8bcefc5fffefe777

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