|
| 1 | +--- |
| 2 | +name: tgcli |
| 3 | +version: 0.1.0 |
| 4 | +description: |
| 5 | + Read, search, and send Telegram messages via your personal account using the tgcli |
| 6 | + CLI. Use when the user asks to check their Telegram messages, search Telegram history, |
| 7 | + send Telegram messages to other people, or monitor Telegram conversations. Do NOT use |
| 8 | + for normal user chats routed through OpenClaw's Telegram channel. |
| 9 | +triggers: |
| 10 | + - tgcli |
| 11 | + - telegram messages |
| 12 | + - telegram history |
| 13 | + - search telegram |
| 14 | + - check telegram |
| 15 | + - telegram chats |
| 16 | + - send telegram |
| 17 | + - telegram DMs |
| 18 | + - who messaged me on telegram |
| 19 | +metadata: |
| 20 | + openclaw: |
| 21 | + emoji: "✈️" |
| 22 | + requires: |
| 23 | + bins: [tgcli] |
| 24 | + install: |
| 25 | + - id: go |
| 26 | + kind: go |
| 27 | + module: github.com/kaosb/tgcli@latest |
| 28 | + env: |
| 29 | + CGO_ENABLED: "1" |
| 30 | + bins: [tgcli] |
| 31 | + label: Install tgcli (go install) |
| 32 | +--- |
| 33 | + |
| 34 | +# tgcli ✈️ |
| 35 | + |
| 36 | +CLI for reading, searching, and sending Telegram messages from a personal account via |
| 37 | +MTProto 2.0. The Telegram equivalent of wacli. |
| 38 | + |
| 39 | +Use `tgcli` only when the user explicitly asks you to check their Telegram messages, |
| 40 | +search Telegram history, or send a Telegram message to someone else. |
| 41 | + |
| 42 | +Do NOT use `tgcli` for normal user chats; OpenClaw routes Telegram conversations |
| 43 | +automatically via the bot channel. |
| 44 | + |
| 45 | +## Safety |
| 46 | + |
| 47 | +- Require explicit recipient + message text before sending. |
| 48 | +- Confirm recipient + message before sending. |
| 49 | +- If anything is ambiguous, ask a clarifying question. |
| 50 | +- Never send files from `~/.tgcli/` or files containing credentials, keys, or tokens. |
| 51 | +- Never bulk-send or spam. One message at a time. |
| 52 | + |
| 53 | +## Setup |
| 54 | + |
| 55 | +### Prerequisites |
| 56 | + |
| 57 | +1. Go 1.23+ with CGO enabled |
| 58 | +2. Telegram API credentials from [my.telegram.org/apps](https://my.telegram.org/apps) |
| 59 | + (any app name works; you need the `api_id` and `api_hash`) |
| 60 | + |
| 61 | +### Authentication |
| 62 | + |
| 63 | +```bash |
| 64 | +tgcli login |
| 65 | +``` |
| 66 | + |
| 67 | +Interactive flow: enter API credentials, phone number, verification code (sent to |
| 68 | +Telegram app), and 2FA password if enabled. Session persists in `~/.tgcli/`. |
| 69 | + |
| 70 | +```bash |
| 71 | +tgcli logout # End session and remove local data |
| 72 | +``` |
| 73 | + |
| 74 | +### First Sync |
| 75 | + |
| 76 | +After login, sync recent history to build the local search index: |
| 77 | + |
| 78 | +```bash |
| 79 | +tgcli sync # All chats (last 100 msgs each) |
| 80 | +tgcli sync --chat @username # Full history for one chat |
| 81 | +tgcli sync --msgs-per-chat 500 # More history per chat |
| 82 | +``` |
| 83 | + |
| 84 | +## CLI Reference |
| 85 | + |
| 86 | +### List Chats |
| 87 | + |
| 88 | +```bash |
| 89 | +tgcli chat ls # All chats (default 50) |
| 90 | +tgcli chat ls --type private # Only DMs |
| 91 | +tgcli chat ls --type group # Only groups |
| 92 | +tgcli chat ls --type channel # Only channels |
| 93 | +tgcli chat ls --limit 20 # Limit results |
| 94 | +tgcli chat ls --json # Machine-readable output |
| 95 | +``` |
| 96 | + |
| 97 | +### Read Messages |
| 98 | + |
| 99 | +```bash |
| 100 | +tgcli msg ls @username # Last 20 messages |
| 101 | +tgcli msg ls @username --limit 50 # Last 50 messages |
| 102 | +tgcli msg ls 123456789 --json # By user ID, JSON output |
| 103 | +``` |
| 104 | + |
| 105 | +### Search Messages |
| 106 | + |
| 107 | +Search uses SQLite FTS5 for instant offline full-text search. Run `tgcli sync` first to |
| 108 | +build the index. |
| 109 | + |
| 110 | +```bash |
| 111 | +tgcli msg search "meeting notes" # Search all chats |
| 112 | +tgcli msg search "budget" --chat @username # Search one chat |
| 113 | +tgcli msg search "keyword" --limit 50 # More results |
| 114 | +tgcli msg search "query" --json # JSON output |
| 115 | +``` |
| 116 | + |
| 117 | +### Message Context |
| 118 | + |
| 119 | +```bash |
| 120 | +tgcli msg context @username 12345 # 5 msgs before + after |
| 121 | +tgcli msg context @username 12345 --before 10 # 10 msgs before |
| 122 | +tgcli msg context @username 12345 --after 10 # 10 msgs after |
| 123 | +``` |
| 124 | + |
| 125 | +### Send Messages |
| 126 | + |
| 127 | +```bash |
| 128 | +tgcli send text @username "Hello!" # By username |
| 129 | +tgcli send text 123456789 "Hello!" # By user ID |
| 130 | +tgcli send text +14155551212 "Hello!" # By phone (must be in contacts) |
| 131 | +tgcli send file @username ./report.pdf # Send file |
| 132 | +tgcli send file @username ./photo.jpg --caption "Check this out" |
| 133 | +``` |
| 134 | + |
| 135 | +### Export |
| 136 | + |
| 137 | +```bash |
| 138 | +tgcli export @username # JSON to stdout |
| 139 | +tgcli export @username -o backup.json # Save to file |
| 140 | +tgcli export @username --local # From local DB (offline) |
| 141 | +``` |
| 142 | + |
| 143 | +### Download Media |
| 144 | + |
| 145 | +```bash |
| 146 | +tgcli download @username 12345 # Download media from msg |
| 147 | +tgcli download @username 12345 -o ~/media # Custom output directory |
| 148 | +``` |
| 149 | + |
| 150 | +### Sync History |
| 151 | + |
| 152 | +```bash |
| 153 | +tgcli sync # All chats, recent msgs |
| 154 | +tgcli sync --chat @username # Full history for one chat |
| 155 | +tgcli sync --msgs-per-chat 500 # More depth per chat |
| 156 | +``` |
| 157 | + |
| 158 | +## Chat Identifiers |
| 159 | + |
| 160 | +| Format | Example | Type | |
| 161 | +| --------------- | --------------- | --------------------------- | |
| 162 | +| `@username` | `@durov` | Username | |
| 163 | +| `123456789` | `123456789` | User/Chat ID | |
| 164 | +| `-123456789` | `-123456789` | Group | |
| 165 | +| `-100123456789` | `-100123456789` | Channel / Supergroup | |
| 166 | +| `+1234567890` | `+14155551212` | Phone (must be in contacts) | |
| 167 | + |
| 168 | +**Important:** Display names (e.g. "Jane Doe") do NOT work as chat identifiers. Use peer |
| 169 | +IDs from `tgcli chat ls --json`. The recommended workflow: |
| 170 | + |
| 171 | +1. `tgcli chat ls --json` to find the `peer_id` for a chat |
| 172 | +2. Use that `peer_id` in subsequent `msg ls`, `msg search --chat`, `send`, etc. |
| 173 | + |
| 174 | +## Global Flags |
| 175 | + |
| 176 | +| Flag | Description | Default | |
| 177 | +| --------------- | ---------------------------- | ---------- | |
| 178 | +| `--json` | Machine-readable JSON output | false | |
| 179 | +| `--store DIR` | Data directory | `~/.tgcli` | |
| 180 | +| `--timeout DUR` | Command timeout | 5m | |
| 181 | + |
| 182 | +## How It Works |
| 183 | + |
| 184 | +tgcli connects via [gotd/td](https://github.com/gotd/td), a pure-Go MTProto 2.0 |
| 185 | +implementation. This means: |
| 186 | + |
| 187 | +- **You are the sender** -- messages come from your account, not a bot |
| 188 | +- **No ban risk** -- Telegram officially supports third-party clients |
| 189 | +- **Independent session** -- works without your phone being online |
| 190 | +- **Login once** -- session persists until you revoke it |
| 191 | + |
| 192 | +Messages fetched from the API are cached in a local SQLite database with FTS5 full-text |
| 193 | +search, enabling instant offline search across your entire history. |
| 194 | + |
| 195 | +## Data Storage |
| 196 | + |
| 197 | +All data lives in `~/.tgcli/` (configurable with `--store`): |
| 198 | + |
| 199 | +| File | Contents | |
| 200 | +| -------------- | --------------------------------------------- | |
| 201 | +| `config.json` | API credentials (app_id, app_hash) -- private | |
| 202 | +| `session.json` | MTProto session -- private | |
| 203 | +| `tgcli.db` | Local message cache + FTS5 search index | |
| 204 | + |
| 205 | +## Notes |
| 206 | + |
| 207 | +- Always use `--json` when parsing output programmatically |
| 208 | +- Run `tgcli sync` periodically to keep the local search index fresh |
| 209 | +- Search is local-only (uses the SQLite FTS5 index); sync first for complete results |
| 210 | +- Phone-based chat identifiers require the contact to be in your Telegram contacts |
| 211 | +- The Telegram CLI is for messaging other people or searching history, not for your |
| 212 | + normal OpenClaw conversations |
0 commit comments