@will-17173/telegram-cli 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +85 -449
  2. package/README.zh-CN.md +84 -445
  3. package/dist/cli/app.js +3 -1
  4. package/dist/cli/app.js.map +1 -1
  5. package/dist/commands/telegram.js +16 -1
  6. package/dist/commands/telegram.js.map +1 -1
  7. package/dist/commands/web.js +52 -0
  8. package/dist/commands/web.js.map +1 -0
  9. package/dist/listen-commands/catalog.js +10 -0
  10. package/dist/listen-commands/catalog.js.map +1 -1
  11. package/dist/listen-commands/dispatch.js +5 -0
  12. package/dist/listen-commands/dispatch.js.map +1 -1
  13. package/dist/presenters/ink/listen-command-menu.js +10 -2
  14. package/dist/presenters/ink/listen-command-menu.js.map +1 -1
  15. package/dist/presenters/ink/listen-scroll.js +1 -1
  16. package/dist/presenters/ink/listen-scroll.js.map +1 -1
  17. package/dist/presenters/ink/listen.js +154 -25
  18. package/dist/presenters/ink/listen.js.map +1 -1
  19. package/dist/presenters/listen-message.js +1 -1
  20. package/dist/presenters/listen-message.js.map +1 -1
  21. package/dist/services/sync-service.js +33 -2
  22. package/dist/services/sync-service.js.map +1 -1
  23. package/dist/storage/message-db.js +117 -0
  24. package/dist/storage/message-db.js.map +1 -1
  25. package/dist/telegram/mtcute-client.js +30 -9
  26. package/dist/telegram/mtcute-client.js.map +1 -1
  27. package/dist/telegram/types.js.map +1 -1
  28. package/dist/web/api.js +196 -0
  29. package/dist/web/api.js.map +1 -0
  30. package/dist/web/assets/index-B5v_8ify.css +1 -0
  31. package/dist/web/assets/index-DC74wrJK.js +9 -0
  32. package/dist/web/index.html +13 -0
  33. package/dist/web/query.js +74 -0
  34. package/dist/web/query.js.map +1 -0
  35. package/dist/web/security.js +23 -0
  36. package/dist/web/security.js.map +1 -0
  37. package/dist/web/server.js +133 -0
  38. package/dist/web/server.js.map +1 -0
  39. package/dist/web/static.js +40 -0
  40. package/dist/web/static.js.map +1 -0
  41. package/dist/web/sync-task.js +161 -0
  42. package/dist/web/sync-task.js.map +1 -0
  43. package/dist/web/types.js +2 -0
  44. package/dist/web/types.js.map +1 -0
  45. package/package.json +10 -5
package/README.md CHANGED
@@ -1,547 +1,183 @@
1
1
  # Telegram CLI
2
2
 
3
- [简体中文](README.zh-CN.md)
3
+ [Project website](https://will-17173.github.io/telegram-cli/) · [Telegram CLI documentation](https://will-17173.github.io/telegram-cli/docs/) · [简体中文 README](README.zh-CN.md)
4
4
 
5
- A TypeScript command-line client for syncing Telegram chats, listening to live messages, searching locally stored messages, and inspecting groups from the terminal.
5
+ Telegram CLI is a TypeScript command-line interface (CLI) for live Telegram data, local SQLite search, and remote management. It aims to be the most capable Telegram CLI for people and artificial intelligence (AI) agents that need dependable Telegram access from one `tg` command. Account sessions and synced messages stay on your machine.
6
6
 
7
- ## What you can do
7
+ ## Why Telegram CLI stands out
8
8
 
9
- - Manage multiple Telegram accounts with isolated sessions and message databases.
10
- - Inspect unread chats and read or search messages without storing them locally.
11
- - Sync chat history to SQLite for offline search, filtering, analysis, and export.
12
- - Archive selected chats as incremental Markdown files, with optional attachment downloads.
13
- - Listen for new messages and download incoming attachments.
14
- - Send, edit, and delete messages from the command line.
15
- - Inspect contacts, notification settings, chat folders, groups, members, administrators, invites, and forum topics.
16
- - Disable remote Telegram mutations while keeping read-only and local commands available.
17
- - Use human-readable, JSON, YAML, or Markdown output in scripts and agent workflows.
9
+ Telegram CLI combines online reads, local persistence, file archives, live listeners, remote writes, group administration, account isolation, a local web UI, and structured output in one tool.
18
10
 
19
- ## Built for AI agents
11
+ It is designed for AI agents:
20
12
 
21
- Telegram CLI gives AI agents a command-based interface to Telegram and locally synced messages. After you authenticate an account with `tg account add`, an agent can run online and local commands without browser automation.
13
+ - **Stable command contracts**: finite commands support JSON, YAML, Markdown, exit statuses, and stable error codes
14
+ - **Local-first data access**: synced messages stay in SQLite so agents can search and analyze Telegram history without repeated network reads
15
+ - **Explicit account control**: `--account` selects the intended session for one command
16
+ - **Write safety**: a write-access gate separates read-only automation from commands that modify Telegram
17
+ - **Agent skill support**: the `using-telegram-cli` skill teaches supported agents how to authenticate, sync, query, and avoid unsafe writes
22
18
 
23
- The CLI supports agent workflows through these interfaces:
19
+ ## Read the documentation
24
20
 
25
- - JSON and YAML output gives agents structured data instead of terminal-formatted text.
26
- - Nonzero exit codes and structured error codes let agents detect and handle failures.
27
- - `--account <name>` selects an explicit account without changing the current account.
28
- - Local search and analysis commands let agents inspect synced messages without reconnecting to Telegram.
21
+ Read the [complete Telegram CLI documentation](https://will-17173.github.io/telegram-cli/docs/) for installation, workflows, every command, automation, safety, and troubleshooting.
29
22
 
30
- For example, an agent can search one account and parse the result as JSON:
23
+ ## Choose a Telegram workflow
31
24
 
32
- ```sh
33
- tg search "release" --account work --json
34
- ```
25
+ Choose a workflow by the data freshness you need, where the result should go, and whether the command changes Telegram.
35
26
 
36
- ### Agent skill
27
+ ### Read current Telegram data
37
28
 
38
- Install the [`using-telegram-cli`](https://skills.sh/will-17173/telegram-cli/using-telegram-cli) skill to teach a supported AI coding agent how to authenticate accounts, synchronize and query messages, automate structured output, and guard Telegram write operations:
29
+ Use online commands when you need the latest server state. These commands do not add returned messages to SQLite.
39
30
 
40
31
  ```sh
41
- npx skills add https://github.com/will-17173/telegram-cli \
42
- --skill using-telegram-cli
32
+ tg inbox
33
+ tg read @team --since 2h
34
+ tg search-online "incident" --chat @team --json
43
35
  ```
44
36
 
45
- Add `--global` to make the skill available across projects instead of installing it into the current project.
37
+ You can also inspect contacts, notification settings, folders, and group details without importing messages.
46
38
 
47
- ## Installation
39
+ ### Build a searchable local history
48
40
 
49
- Telegram CLI requires Node.js 22 or later.
50
-
51
- Install the package globally from npm:
41
+ Sync one chat or many chats into the selected account’s SQLite database. Search and analyze the stored copy without reconnecting to Telegram.
52
42
 
53
43
  ```sh
54
- npm install -g @will-17173/telegram-cli
44
+ tg sync @team
45
+ tg search "release" --chat @team
46
+ tg recent --chat @team --hours 24
55
47
  ```
56
48
 
57
- ## Quick start
49
+ Local commands can also filter, summarize, and export stored messages.
58
50
 
59
- Authenticate an account, check its status, and list its chats:
51
+ ### Browse local data in a web UI
60
52
 
61
- ```sh
62
- tg account add
63
- tg status
64
- tg chats
65
- tg chats --group
66
- tg chats --channel
67
- tg chats --user
68
- ```
69
-
70
- Choose a chat name, username, or ID from `tg chats`, then sync and search its messages:
53
+ Run a local-only management UI for stored messages:
71
54
 
72
55
  ```sh
73
- tg sync <chat>
74
- tg search "keyword" --chat <chat>
56
+ tg web
75
57
  ```
76
58
 
77
- You can also sync multiple chats, listen for incoming messages, or send a message:
59
+ The server binds to `127.0.0.1`, has no login screen, and is intended for local use only. It can browse local SQLite data and trigger read-only sync for the selected chat.
78
60
 
79
- ```sh
80
- tg sync-all --max-chats 20 --delay 1
81
- tg listen <chat-or-id> --auto-download
82
- tg send <chat> "Hello from tg"
83
- ```
61
+ ### Follow live messages and download files
84
62
 
85
- ## Read and manage Telegram online
86
-
87
- `history`, `sync`, and `sync-all` fetch Telegram messages and store them in the local SQLite database. `read`, `search-online`, and `inbox` query Telegram directly without storing their results. `inbox` lists chats with unread messages without marking them as read.
63
+ The `listen` command streams new messages from one chat or many chats. It can download incoming attachments and run interactive reply or group actions.
88
64
 
89
65
  ```sh
90
- tg inbox --markdown
91
- tg read @team --since 7d --until 2d
92
- tg search-online release --chat @team --json
93
- ```
94
-
95
- Time bounds accept relative durations ending in `s`, `m`, `h`, `d`, or `w`. For example, `7d` means seven days before the command starts. Absolute bounds use ISO 8601 timestamps with a time zone, such as `2026-07-13T00:00:00Z`. The `--since` value must be earlier than `--until`.
96
-
97
- You can also inspect contacts, notification settings, folders, and group dialogs without changing remote state:
98
-
99
- ```sh
100
- tg contact list
101
- tg contact info +8613800000000
102
- tg notification info @team
103
- tg folder list
104
- tg folder info 2
105
- tg group list --admin
106
- ```
107
-
108
- The following commands modify Telegram remotely:
109
-
110
- ```sh
111
- tg notification mute @team 8h
112
- tg notification unmute @team
113
- tg folder chat add Work @team
114
- tg folder chat remove Work @team
115
- ```
116
-
117
- Use `tg config write-access off` to block these mutations. Restore them with `tg config write-access on`.
118
-
119
- Folder commands accept either a title or numeric folder ID. Folder titles are not necessarily unique. Run `tg folder list` first, then use the returned ID with `tg folder info`, `tg folder chat add`, or `tg folder chat remove`.
120
-
121
- ## Configuration
122
-
123
- Personal Telegram application programming interface (API) credentials are optional. To use your own, create them at [my.telegram.org](https://my.telegram.org), then save them with:
124
-
125
- ```sh
126
- tg config set --api-id <id> --api-hash <hash>
127
- ```
128
-
129
- If both `TG_API_ID` and `TG_API_HASH` are unset and the saved configuration file is missing, the CLI uses built-in Telegram API credentials. When a Telegram client is created, it writes this warning to stderr once per process:
130
-
131
- ```text
132
- warning: using default Telegram API credentials, which have stricter flood limits and may trigger FLOOD_WAIT during frequent or large requests. Run tg config set --api-id <id> --api-hash <hash> to configure your own.
66
+ tg listen @team --auto-download
133
67
  ```
134
68
 
135
- Setting only one of `TG_API_ID` or `TG_API_HASH` is an error. A malformed or unreadable saved configuration file is also an error; the CLI does not fall back to the built-in credentials in either case.
136
-
137
- Personal credentials are stored locally as sensitive configuration. Never share them. API credentials are shared by all registered accounts, while each account keeps its own authentication session.
69
+ ### Keep a Markdown archive
138
70
 
139
- To persist a proxy for Telegram connections, run:
71
+ The `archive` command writes incremental Markdown and optional media files. It tracks archive progress separately and does not populate SQLite.
140
72
 
141
73
  ```sh
142
- tg config set --proxy socks5://127.0.0.1:1080
74
+ tg archive @team --download-media
143
75
  ```
144
76
 
145
- For a one-command override, set `TG_PROXY` in the command environment:
77
+ Later runs append new messages and retry referenced media that is still missing.
146
78
 
147
- ```sh
148
- TG_PROXY=http://127.0.0.1:8080 tg status
149
- ```
150
-
151
- Telegram CLI supports SOCKS4, SOCKS5, HTTP, HTTPS, and MTProxy. The proxy applies to account login and every Telegram-backed command. Treat proxy URLs that contain credentials as sensitive.
79
+ ### Send messages and manage groups
152
80
 
153
- To inspect the effective configuration in human-readable, JSON, or YAML format, run:
81
+ Send text, files, or captioned media groups from the terminal. Inspect and manage group members, administrators, invites, forum topics, and messages.
154
82
 
155
83
  ```sh
156
- tg config list
157
- tg config list --json
158
- tg config list --yaml
159
- tg config list --show-secrets
84
+ tg send @team "Release is ready" --file ./report.pdf
85
+ tg group members @team --type admins
86
+ tg group member mute @team @alice 2h --yes
160
87
  ```
161
88
 
162
- This reports the effective configuration rather than the raw contents of `config.json`. The API hash is masked by default; `--show-secrets` reveals only the full API hash.
89
+ Telegram CLI also manages contacts, notification settings, and chat folders. The write-access gate covers commands that change Telegram.
163
90
 
164
- Safe proxy endpoint details remain visible, but proxy usernames, passwords, and credential query parameters are always masked, even with `--show-secrets`.
91
+ ### Automate across isolated accounts
165
92
 
166
- Run `tg account add` to authenticate and create a local session. Other commands never start the interactive login flow.
167
-
168
- You can override the root directory for configuration, account sessions, and message databases:
93
+ Each registered account has a separate session and SQLite database. Select an account for one command without changing the default.
169
94
 
170
95
  ```sh
171
- export DATA_DIR=/path/to/tg-cli-data
96
+ tg stats --account work --json
172
97
  ```
173
98
 
174
- ## Send messages and attachments
175
-
176
- `send` requires `<chat>`. Send text, one or more files, or files with a caption:
177
-
178
- ```sh
179
- # Text only
180
- tg send <chat> "Text only"
181
-
182
- # Files only; repeat --file to preserve this order
183
- tg send <chat> --file ./photo.jpg --file ./clip.mp4
184
-
185
- # Caption and files
186
- tg send <chat> "Group caption" --file ./photo.jpg --file ./clip.mp4
187
- ```
188
-
189
- `--file` is repeatable. Multiple files are sent in the specified order as one Telegram media group. The message is optional only when at least one file is present. When files are present, the message becomes the group caption; the CLI does not send it as a separate text message.
190
-
191
- Telegram determines which file combinations and group sizes it accepts. If Telegram rejects the requested combination or limit, the command returns an error and does not silently split the files into separate messages or groups.
192
-
193
- ## Archive chats as Markdown
99
+ Finite commands support JSON, YAML, and Markdown output. Failures return nonzero exit statuses and stable error codes.
194
100
 
195
- `archive` requires an explicit scope. Pass one or more chat IDs or usernames, or use `--all`, but don't combine them.
101
+ ## Install
196
102
 
197
- The command uses the current account unless you pass `--account <name>`. It writes to that account's `archive` directory by default. Use `--output <path>` to select another directory.
103
+ Install Node.js 22.12.0 or later, then install Telegram CLI from npm:
198
104
 
199
105
  ```sh
200
- # Initial archive with attachments: the preceding seven days
201
- tg archive @team --download-media
202
-
203
- # Custom range (relative durations or ISO timestamps with zones)
204
- tg archive @team --since 30d --until 2026-07-13T00:00:00Z
205
-
206
- # Full available history for every chat, including attachments
207
- tg archive --all --full --download-media
106
+ npm install -g @will-17173/telegram-cli
208
107
  ```
209
108
 
210
- The first run archives the preceding seven days. Use `--since` and `--until` to select another range. Use `--full` to remove the lower bound; you can't combine it with `--since`.
211
-
212
- Later runs append new messages to the existing archive. Use `--rebuild` to replace each selected Markdown file. Without a new range, rebuilding reuses the archive's initial range.
213
-
214
- Use `--download-media` to store attachments in the archive's `media/` directory. Incremental runs retry referenced media that is still missing.
215
-
216
- Chat or attachment failures produce `archive_partial_failure`, preserve successful chat results, and exit with status 1. Use `--json` or `--yaml` in automation and inspect `completed`, `failed`, and `warnings`.
217
-
218
- Large history and media requests can trigger Telegram flood waits or other rate limits. Media downloads can fail independently. Successfully archived messages remain on disk, and warnings identify failed attachments.
109
+ ## Get started
219
110
 
220
- ## Multiple accounts
221
-
222
- Each Telegram account has its own persisted authentication session and local message database. Add and authenticate an account interactively with:
111
+ Authenticate one account, list its chats, sync one chat, and search the local copy:
223
112
 
224
113
  ```sh
225
114
  tg account add
115
+ tg status
116
+ tg chats
117
+ tg sync @team
118
+ tg search "release" --chat @team
226
119
  ```
227
120
 
228
- The first account you add becomes the current account. Adding another account does not switch the current account automatically. Use the account commands to inspect or change the selection:
229
-
230
- ```sh
231
- # List registered accounts
232
- tg account list
233
-
234
- # Show the current account
235
- tg account current
236
-
237
- # Choose the default account from an interactive list
238
- tg account switch
239
-
240
- # Set the default account by name
241
- tg account switch <name>
242
-
243
- # Remove an account and its local session/data
244
- tg account remove <name> --force
245
- ```
246
-
247
- Log out explicitly to end the remote session without deleting the account, its settings, or locally stored messages. The `--yes` option confirms logout without a prompt.
248
-
249
- Logging in again requires an interactive terminal (TTY) and creates or replaces the local Telegram session. Scripts and non-interactive agents receive an `interaction_required` error.
250
-
251
- ```sh
252
- tg account logout work --yes
253
- tg account login work
254
- ```
255
-
256
- In an interactive terminal, `tg account switch` lists registered accounts, marks the current one, and accepts its number. Pass `<name>` when scripting or using `--json`, `--yaml`, or non-interactive input.
257
-
258
- Commands use the current account by default. Commands that support `--account` can target another registered account for one invocation without changing the current account:
259
-
260
- ```sh
261
- tg chats --account <name>
262
- tg sync-all --account <name>
263
- tg search "keyword" --account <name>
264
- ```
265
-
266
- Account names are shown by `tg account list`; they are normally derived from the Telegram username. Sessions and message databases remain isolated under each account's directory inside `DATA_DIR`.
267
-
268
- Telegram API credentials apply to every registered account. You don't need to configure separate API credentials when adding another account.
269
-
270
- ## Group management
271
-
272
- The `group` command supports read-only inspection and management of members, administrators, chat settings, invite links, forum topics, and messages. Use each command group's help to see its actions:
273
-
274
- ```sh
275
- # Group details
276
- tg group info <chat> --account alice --json
277
-
278
- # Member list: type, name/username query, and bounded result count
279
- tg group members <chat> --type admins --query alice --limit 50 --yaml
280
-
281
- # One member's role, administrator rights, and restrictions
282
- tg group member <chat> <user>
283
-
284
- # Administrator audit log; --user and --type can be repeated
285
- tg group audit <chat> --query invite --user <user> --type member_invited --type invite_changed --limit 100 --account alice --json
286
-
287
- # Management examples (the chat argument comes before action arguments)
288
- tg group member ban @team @alice --yes
289
- tg group admin transfer-owner @team @newowner --yes
290
- tg group chat slowmode @team 30s
291
- tg group topic --help
292
- ```
293
-
294
- After confirmation, ownership transfer securely prompts for the Telegram two-factor authentication (2FA) password. The password is never a CLI argument, stdin input, or environment automation source. The secure prompt accepts it only from an interactive terminal, not piped input. Don't automate this prompt.
295
-
296
- `group members` accepts exactly these seven `--type` filters: `recent`, `all`, `admins`, `banned`, `restricted`, `bots`, and `contacts`. It defaults to `recent` and 100 results; `--limit` accepts 1 through 200. Telegram can return fewer members than its reported total, so a page is not guaranteed to enumerate the whole group.
297
-
298
- `group audit` requires group administrator rights. Its `--limit` range is 1 through 500, with a default of 100 events. Its repeatable `--user` filter selects action authors. The repeatable `--type` filter accepts these event groups:
299
-
300
- - **Chat**: `info_changed`, `settings_changed`
301
- - **Members**: `member_joined`, `member_left`, `member_invited`, `member_banned`, `member_unbanned`, `member_restricted`, `member_unrestricted`
302
- - **Administrators**: `admin_promoted`, `admin_demoted`
303
- - **Messages**: `message_deleted`, `message_edited`, `message_pinned`
304
- - **Invites and topics**: `invite_changed`, `topic_changed`
305
- - **Other**: `other`
306
-
307
- Inspection and management actions use human-readable output by default; actions that expose `--json` or `--yaml` provide structured success or error output. Failures set a nonzero exit status. They use the current account unless `--account <name>` selects another registered account for that invocation.
308
-
309
- Management actions are grouped under `member`, `admin`, `chat`, `invite`, `topic`, and `message`. Member targets must be explicit `@username` values or numeric Telegram user IDs. Durations accept `s`, `m`, `h`, and `d` suffixes, or `off` where disabling is supported. For example, `tg group member mute @team @alice 2h --yes` temporarily mutes a member, while `tg group chat slowmode @team off` disables slow mode.
310
-
311
- Potentially destructive CLI actions refuse to connect to Telegram unless `--yes` is present. Permanently deleting a chat additionally requires `--confirm-title` with the exact current title. Interactive listen mode presents these confirmations in an Ink modal. Management requires the relevant administrator permission, and some actions require a supergroup, forum, or creator role.
312
-
313
- Use `tg group member info <chat> <user>` as the canonical member-details route. The legacy `tg group member <chat> <user>` form remains available, but a chat name matching a reserved member action such as `ban`, `mute`, or `info` is ambiguous and requires the canonical route.
314
-
315
- ## Slash commands while listening
316
-
317
- Interactive `tg listen` presents every supported slash command in one menu. This includes `/reply` and the complete group-management catalog; group commands use the same management grammar without repeating the selected chat:
318
-
319
- ```text
320
- /reply <message-id> <content>
321
- /member mute @alice 2h
322
- ```
323
-
324
- Typing `/` opens the unified command menu, with reply first. Matching ranks exact paths, prefixes, then ordered fuzzy matches, so `/rep` and `/rpy` find `/reply`, while `/ban` finds `/member ban`. Use **Up** and **Down** to move through matches. **Tab** completes the selected command. **Enter** completes an incomplete selection or runs a complete command. **Esc** closes the menu, result, or confirmation.
325
-
326
- Group-command availability and permission checks are unchanged: unavailable actions remain disabled, risky actions open a confirmation modal, and chat deletion also asks for the exact title. When listening to more than one chat, set an unambiguous outgoing target with `--send-to <chat>` before using group commands, for example `tg listen @team @ops --send-to @team`.
327
-
328
- ## Online and local commands
121
+ Replace `@team` with a chat name, username, or numeric identifier (ID). Run `tg --help` or a command such as `tg sync --help` to inspect available options.
329
122
 
330
- Online commands connect to Telegram and require a valid session:
123
+ ## Know where data goes
331
124
 
332
- - `read`, `search-online`, and `inbox` return results without storing them locally.
333
- - `history`, `sync`, `sync-all`, and `refresh` persist fetched messages in SQLite.
334
- - `contact`, `notification`, `folder`, `info`, and `group` inspect or manage Telegram data.
335
- - `archive` reads Telegram history and writes Markdown files locally.
336
- - `send`, `edit`, and `delete` modify Telegram messages.
337
- - `listen` keeps a connection open for incoming messages and interactive actions.
125
+ Check a command’s execution scope before you run it:
338
126
 
339
- Global online searches and large archives can trigger Telegram flood waits or other rate limits.
127
+ | Scope | Commands | Effect |
128
+ | --- | --- | --- |
129
+ | Online read | `inbox`, `read`, `search-online` | Queries Telegram without storing returned messages. |
130
+ | Local persistence | `history`, `sync`, `sync-all`, `refresh` | Stores fetched messages in the selected account’s SQLite database. |
131
+ | Local read | `search`, `recent`, `stats`, `export`, `web` | Reads local SQLite data without connecting to Telegram. |
132
+ | File archive | `archive` | Reads Telegram and writes Markdown or media files without writing to SQLite. |
133
+ | Remote write | `send`, `edit`, `delete`, notification, folder, and group actions | Changes Telegram messages or settings. |
340
134
 
341
- Local commands read or modify the selected account's message database without connecting to Telegram. These include `search`, `recent`, `stats`, `top`, `timeline`, `today`, `filter`, `export`, and `purge`.
135
+ Each account has a separate session and SQLite database. Add `--account work` to select an account for one command without changing the default.
342
136
 
343
- ## Review recent messages
137
+ ## Protect remote data
344
138
 
345
- `tg recent` shows messages stored during the last 24 hours, with a default limit of 50. Filter the results by chat or sender, or change the time window and limit:
139
+ Disable remote writes before read-only workflows or automation:
346
140
 
347
141
  ```sh
348
- tg recent --chat <chat> --sender <sender> --hours 6 --limit 100
142
+ tg config write-access off
143
+ tg config write-access status
349
144
  ```
350
145
 
351
- Human-readable output groups each Telegram media group into one row and summarizes its attachments. The `ID` column lists every source message ID in that row. Replies include the original message's time, sender, ID, and text when the target exists locally in the same chat. Otherwise, the output identifies the missing local message ID.
146
+ Run `tg config write-access on` when you intend to modify Telegram again. Enabling the gate does not authorize a specific write.
352
147
 
353
- JSON and YAML output keep the stored-message structure for scripts. `recent` reads local SQLite data and does not connect to Telegram.
148
+ Keep Telegram application programming interface (API) credentials, proxy credentials, session files, SQLite databases, exports, and archives private.
354
149
 
355
- ## Command reference
356
-
357
- Run the built-in help for the complete, current command list:
358
-
359
- ```sh
360
- tg --help
361
- ```
150
+ ## Use with coding agents
362
151
 
363
- Common commands:
364
-
365
- | Command | Purpose |
366
- | --- | --- |
367
- | `tg account add` | Authenticate and register another Telegram account. |
368
- | `tg account list` | List registered accounts and show which one is current. |
369
- | `tg account current` | Show the current account. |
370
- | `tg account switch [name]` | Select the default account interactively or set it by name. |
371
- | `tg account remove <name> --force` | Remove an account and its local session/data. |
372
- | `tg account logout <name> --yes` / `tg account login <name>` | Confirm logout non-interactively, or reauthenticate interactively into a new local session; local messages are retained. |
373
- | `tg status` | Check whether the Telegram account is authenticated. |
374
- | `tg whoami` | Show basic authenticated account information. |
375
- | `tg config set --api-id <id> --api-hash <hash>` | Save Telegram API credentials for persistent use. |
376
- | `tg config set --proxy <url>` | Save an optional proxy for account login and Telegram-backed commands. |
377
- | `tg config list [--show-secrets]` | Show effective configuration values and sources. Proxy credentials remain masked. |
378
- | `tg config write-access [status\|on\|off]` | Inspect or gate remote Telegram mutations. |
379
- | `tg chats [--group\|--channel\|--user]` | List available chats, optionally filtered by type (`--group` includes supergroups). |
380
- | `tg inbox` | List unread dialogs online without marking messages read. |
381
- | `tg read <chat> [--since <time>] [--until <time>]` | Read recent Telegram messages without persisting them locally. |
382
- | `tg search-online <query> [--chat <chat>]` | Search Telegram globally or within one chat without persisting results. |
383
- | `tg contact list` / `tg contact info <user_or_phone>` | List contacts or resolve one by ID, username, or phone. |
384
- | `tg dialog inbox` / `tg dialog read <chat>` | Use the dialog-family routes for `inbox` and `read`. |
385
- | `tg dialog search <query>` / `tg dialog groups` | Search online messages or list group dialogs through the dialog family. |
386
- | `tg notification info <chat>` | Show notification settings for a chat. |
387
- | `tg notification mute <chat> [duration]` | Mute notifications temporarily or indefinitely. |
388
- | `tg notification unmute <chat>` | Restore notifications for a chat. |
389
- | `tg folder list` / `tg folder info <folder>` | List folders or inspect one folder. |
390
- | `tg folder chat add <folder> <chat>` | Add an explicit chat to a folder. |
391
- | `tg folder chat remove <folder> <chat>` | Remove an explicit chat from a folder. |
392
- | `tg history <chat> -n <limit>` | Fetch and store full chat history (default up to 1000 messages). |
393
- | `tg sync <chat>` | Incrementally sync new messages for one chat. |
394
- | `tg sync-all` | Sync messages from all chats, using local last-message IDs for incremental updates. |
395
- | `tg refresh` | Alias-like command for bulk sync with same runtime options as `sync-all`. |
396
- | `tg listen [chat ...]` | Stream incoming messages from selected chats or all chats. |
397
- | `tg listen --no-media` | Hide attachment summary lines while listening. |
398
- | `tg listen <chat-or-id> --auto-download` | Automatically download incoming attachments while listening. |
399
- | `tg search "keyword" --chat <chat>` | Search messages already stored locally. |
400
- | `tg recent`, `tg today`, `tg stats`, `tg top`, `tg timeline` | Explore local message data. |
401
- | `tg filter <keywords>` | Filter local messages by keyword with optional chat/hour filters. |
402
- | `tg export <chat>` | Export local messages from a chat. |
403
- | `tg archive <chat ...>` / `tg archive --all` | Archive selected or all chats as incremental Markdown files. |
404
- | `tg send <chat> [message] [--file <path> ...]` | Send text, files, or a captioned media group. |
405
- | `tg edit <chat> <msgId> <text>` | Edit a message. |
406
- | `tg delete <chat> <msgIds...>` | Delete one or more messages. |
407
- | `tg purge <chat> --yes` | Remove a chat's locally stored messages. |
408
- | `tg info <chat>` | Show metadata for a Telegram chat. |
409
- | `tg group info <chat>` | Show read-only group or supergroup details. |
410
- | `tg group list [--admin]` | List group, supergroup, and channel dialogs; `--admin` keeps only chats you administer or own. |
411
- | `tg group members <chat> [--type <type>] [--query <text>] [--limit <count>]` | List and filter members (default `recent`, limit 100; maximum 200). |
412
- | `tg group member <chat> <user>` | Show one member's role, rights, and restrictions. |
413
- | `tg group audit <chat> [--query <text>] [--user <user>] [--type <type>] [--limit <count>]` | Query the administrator audit log (default 100; maximum 500). |
414
- | `tg group member/admin/chat/invite/topic/message --help` | Discover group management actions by family. |
415
-
416
- All sync-like commands write to local SQLite storage. The `sync-all` and `refresh` commands process multiple chats based on locally stored message IDs.
417
-
418
- Finite commands support explicit `--json`, `--yaml`, and `--markdown` output. Without an explicit format, non-interactive output remains YAML. Interactive terminals use rich human-readable output.
419
-
420
- Successful finite output is written to stdout. JSON/YAML structured failures are written to stdout in the requested format. Output-format conflicts also use stdout and a stable YAML envelope. Human-readable and Markdown failures are written to stderr. Every failure sets a nonzero exit status.
421
-
422
- The `listen` command is an unbounded stream, so these finite-output rules don't apply to it.
423
-
424
- Common options:
425
-
426
- | Option | Purpose |
427
- | --- | --- |
428
- | `--account <name>` | Use a registered account without changing the current account. |
429
- | `--json` / `--yaml` / `--markdown` | Select JSON, YAML, or Markdown output for a finite command. |
430
- | `-v`, `--verbose` | Enable debug logging. |
431
- | `-V`, `--version` | Print the installed version. |
432
-
433
- Use `tg <command> --help` to inspect command-specific options. For example, `listen` supports reconnection and plain-text modes, while `search` supports sender, time, regular-expression, and result-limit filters.
434
-
435
- ### Error codes
436
-
437
- Structured output exposes stable top-level command errors under `error.code`, with operation-specific details where available:
438
-
439
- - **Accounts:** `account_logged_out`, `account_identity_mismatch`, `interaction_required`
440
- - **Contacts:** `contact_not_found`
441
- - **Notifications and folders:** `invalid_notification_duration`, `folder_not_found`, `ambiguous_folder`, `folder_operation_unsupported`
442
- - **Group ownership:** `password_required`, `password_invalid`, `password_too_fresh`, `session_too_fresh`
443
- - **Archive:** `archive_account_mismatch`, `archive_failed`, `archive_partial_failure`. When an attachment fails but the archive retains partial results, the top-level code is `archive_partial_failure`, the command exits nonzero, and each media warning uses `archive_media_failed` under `error.details.warnings[].code`.
444
- - **Write safety and rate limits:** `write_access_disabled`, `flood_wait`
445
-
446
- ### Remote write safety
447
-
448
- Run `tg config write-access status` to inspect the current setting. Use `tg config write-access off` to block remote mutations made by send, edit, delete, notification, folder, and group commands. Local database operations, configuration changes, and Telegram reads remain available.
449
-
450
- Run `tg config write-access on` to restore remote mutations after testing or maintenance.
451
-
452
- ### Sync and listen behavior
453
-
454
- These rules describe how synchronization, listening, replies, and downloads affect local data and terminal output.
455
-
456
- - `sync-all` and `refresh` are batch operations for local persistence; they are not read-only.
457
- - `listen` prints a concise separator for each incoming message and can optionally suppress attachment summaries.
458
- - Telegram media groups appear as one incoming message with a combined attachment summary.
459
- - Reply output includes the original message's local context when available, or identifies the missing message ID.
460
- - In interactive `listen`, reply with `/reply <message-id> <content>`. Add attachments with repeatable `--file <path>` options; quote paths that contain spaces.
461
- - Contact cards show the available name and phone number in attachment summaries. They aren't downloadable attachments and are hidden by `--no-media`.
462
- - `listen --auto-download` works in both interactive and plain-text modes, saves attachments to `~/Downloads/telegram-cli`, and runs at most three downloads concurrently.
463
- - Downloads keep Telegram-provided filenames. Unnamed downloads use a MIME-derived extension, then the media-kind extension, then `.bin`.
464
- - Download failures are reported without stopping the listener. `--no-media` hides attachment summaries only; downloads still run when it is combined with `--auto-download`.
465
-
466
- ## Troubleshooting
467
-
468
- Use these steps to resolve common account, session, and API credential errors.
469
-
470
- ### No active account
471
-
472
- If a command reports `account_required`, add an account or select an existing one:
152
+ Use JSON or YAML when a script or coding agent needs structured output:
473
153
 
474
154
  ```sh
475
- tg account add
476
- tg account switch <name>
477
- ```
478
-
479
- ### Session is no longer valid
480
-
481
- If Telegram returns `AUTH_KEY_UNREGISTERED`, remove the invalid local session and authenticate again:
482
-
483
- ```sh
484
- tg account remove <name> --force
485
- tg account add
155
+ tg search "release" --account work --json
486
156
  ```
487
157
 
488
- ### Default API credentials warning
158
+ Failures return a nonzero exit status and a stable error code. The explicit `--account` option keeps automation on the intended account.
489
159
 
490
- The built-in API credentials remain usable, but the CLI prints a warning when it creates a Telegram client. Configure personal credentials to remove the warning:
160
+ Install the [`using-telegram-cli` agent skill](https://skills.sh/will-17173/telegram-cli/using-telegram-cli) in a supported coding agent:
491
161
 
492
162
  ```sh
493
- tg config set --api-id <id> --api-hash <hash>
494
- ```
495
-
496
- Set both `TG_API_ID` and `TG_API_HASH` when using environment variables. Setting only one causes a configuration error.
497
-
498
- ## Local data and privacy
499
-
500
- Persisted configuration, authentication sessions, and synced messages remain on your machine unless you copy or export them. The relevant files under `DATA_DIR` are:
501
-
502
- ```text
503
- config.json
504
- accounts.json
505
- accounts/<name>/session
506
- accounts/<name>/messages.db
163
+ npx skills add https://github.com/will-17173/telegram-cli \
164
+ --skill using-telegram-cli
507
165
  ```
508
166
 
509
- Treat persisted configuration, `.env`, Telegram credentials, session files, and SQLite data as sensitive. Never share them or commit them to version control.
167
+ The skill covers authentication, synchronization, queries, and write safety.
510
168
 
511
- ## Development
169
+ ## Develop
512
170
 
513
- This project uses pnpm:
171
+ Use pnpm with Node.js 22.12.0 or later:
514
172
 
515
173
  ```sh
516
174
  pnpm install
517
175
  pnpm dev --help
518
176
  pnpm test
519
177
  pnpm typecheck
178
+ pnpm build
520
179
  ```
521
180
 
522
- During development, expose the current checkout as a global `tg` command that runs the TypeScript source directly. From the project root:
523
-
524
- ```sh
525
- mkdir -p ~/.local/bin
526
- cat > ~/.local/bin/tg <<EOF
527
- #!/bin/sh
528
- exec "$(pwd)/node_modules/.bin/tsx" "$(pwd)/src/dev.ts" "\$@"
529
- EOF
530
- chmod +x ~/.local/bin/tg
531
- rehash
532
- ```
533
-
534
- Make sure `~/.local/bin` is in `PATH`. Subsequent `tg` invocations load the latest source changes.
535
-
536
- For local source development, create a `.env` file in the project root:
537
-
538
- ```dotenv
539
- TG_API_ID=your_telegram_api_id
540
- TG_API_HASH=your_telegram_api_hash
541
- ```
542
-
543
- `pnpm dev` loads this file only for local source development. An installed `tg` does not automatically load `.env`; use `tg config set --api-id <id> --api-hash <hash>` for persistent production configuration.
544
-
545
181
  ## License
546
182
 
547
- Licensed under [GPL-3.0](LICENSE).
183
+ Licensed under [GPL-3.0-only](LICENSE).