telethon 0.4.0 → 1.0.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.
@@ -0,0 +1,805 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Telethon
4
+ module CLI
5
+ # Single source of truth for tg's commands: drives option parsing, grouped `tg --help`,
6
+ # `tg help COMMAND`, shell completion, and the generated `docs/cli.md` reference.
7
+ # Plain frozen Hashes keep Spinel's class count flat.
8
+ module Table
9
+ GROUPS = %w[Account Reading Writing Chats Bots Other].freeze
10
+
11
+ EXIT_CODES = [
12
+ [0, "ok"],
13
+ [1, "failure"],
14
+ [2, "usage error"],
15
+ [3, "not logged in"],
16
+ [4, "Telegram refused (RPC error)"],
17
+ [5, "connection failed"]
18
+ ].freeze
19
+
20
+ COMMANDS = [
21
+ {
22
+ name: "login",
23
+ group: "Account",
24
+ usage: "login [--qr | --bot-token] [--json]",
25
+ summary: "log in interactively (or as a bot via TG_BOT_TOKEN/stdin)",
26
+ positionals: [],
27
+ spec: { "--qr" => :flag, "--bot-token" => :flag, "--json" => :flag },
28
+ json: true,
29
+ options: {
30
+ "--qr" => "log in by scanning a QR code from another device",
31
+ "--bot-token" => "log in as a bot (reads TG_BOT_TOKEN or stdin, never argv)",
32
+ "--json" => "print result as JSON"
33
+ },
34
+ example: "tg login --qr",
35
+ json_shape: '{"ok": true, "user": {"id": 1, "kind": "user", "username": "example_user", "name": "..."}, "session_path": "..."}'
36
+ },
37
+ {
38
+ name: "logout",
39
+ group: "Account",
40
+ usage: "logout [--json]",
41
+ summary: "log out and remove the saved session",
42
+ positionals: [],
43
+ spec: { "--json" => :flag },
44
+ json: true,
45
+ options: {
46
+ "--json" => "print result as JSON"
47
+ },
48
+ example: "tg logout",
49
+ json_shape: '{"ok": true}'
50
+ },
51
+ {
52
+ name: "2fa",
53
+ group: "Account",
54
+ usage: "2fa set|remove [--hint HINT] [--email EMAIL] [--json]",
55
+ summary: "set, change, or remove the account's 2FA password",
56
+ positionals: ["ACTION"],
57
+ spec: { "--hint" => :value, "--email" => :value, "--json" => :flag },
58
+ json: true,
59
+ options: {
60
+ "--hint" => "password hint (for set)",
61
+ "--email" => "recovery email address (for set)",
62
+ "--json" => "print result as JSON"
63
+ },
64
+ example: "tg 2fa set --hint \"favorite book\"",
65
+ json_shape: '{"ok": true, "action": "set|remove"}'
66
+ },
67
+ {
68
+ name: "import-session",
69
+ group: "Account",
70
+ usage: "import-session [FILE | --string] [--json]",
71
+ summary: "take over a Telethon .session or StringSession (via TG_SESSION_STRING/stdin)",
72
+ positionals: [],
73
+ spec: { "--string" => :flag, "--json" => :flag },
74
+ json: true,
75
+ options: {
76
+ "--string" => "import a Telethon StringSession from TG_SESSION_STRING or stdin",
77
+ "--json" => "print result as JSON"
78
+ },
79
+ example: "tg import-session ./account.session",
80
+ json_shape: '{"ok": true, "dc_id": 2, "entities": 0, "home": "..."}'
81
+ },
82
+ {
83
+ name: "export-session",
84
+ group: "Account",
85
+ usage: "export-session [--json]",
86
+ summary: "print the current session as a Telethon StringSession (stdout only)",
87
+ positionals: [],
88
+ spec: { "--json" => :flag },
89
+ json: true,
90
+ options: {
91
+ "--json" => "print result as JSON"
92
+ },
93
+ example: "tg export-session",
94
+ json_shape: '{"session": "1..."}'
95
+ },
96
+ {
97
+ name: "whoami",
98
+ group: "Account",
99
+ usage: "whoami [--json]",
100
+ summary: "show the logged-in account",
101
+ positionals: [],
102
+ spec: { "--json" => :flag },
103
+ json: true,
104
+ options: {
105
+ "--json" => "print account info as JSON"
106
+ },
107
+ example: "tg whoami",
108
+ json_shape: '{"id": 1, "kind": "user", "username": "example_user", "name": "...", "first_name": "...", "last_name": null, "title": null, "bot": false, "forum": false}'
109
+ },
110
+ {
111
+ name: "accounts",
112
+ group: "Account",
113
+ usage: "accounts [rename OLD NEW] [--json]",
114
+ summary: "list the saved accounts, or rename one (pick one with --account NAME|@username|id)",
115
+ positionals: [],
116
+ spec: { "--json" => :flag },
117
+ json: true,
118
+ options: {
119
+ "--json" => "print accounts as JSON"
120
+ },
121
+ example: "tg accounts rename second work",
122
+ json_shape: '[{"name": "default", "username": "example_user", "id": 1, "logged_in": true, "current": true}]'
123
+ },
124
+ {
125
+ name: "sessions",
126
+ group: "Account",
127
+ usage: "sessions [--terminate HASH [--yes]] [--json]",
128
+ summary: "list active Telegram sessions or terminate one by hash",
129
+ positionals: [],
130
+ spec: { "--terminate" => :value, "--yes" => :flag, "--json" => :flag },
131
+ json: true,
132
+ options: {
133
+ "--terminate" => "terminate the authorization with the given numeric hash (asks first)",
134
+ "--yes" => "terminate without asking",
135
+ "--json" => "print sessions or result as JSON"
136
+ },
137
+ example: "tg sessions",
138
+ json_shape: '[{"hash": 0, "current": true, "device_model": "...", "platform": "...", "app_name": "...", "app_version": "...", "country": "...", "date_created": "...", "date_active": "..."}]'
139
+ },
140
+ {
141
+ name: "profile",
142
+ group: "Account",
143
+ usage: "profile [--first-name S] [--last-name S] [--bio S] [--username S] [--photo FILE] [--json]",
144
+ summary: "show or update your name, bio, username, or profile photo",
145
+ positionals: [],
146
+ spec: { "--first-name" => :value, "--last-name" => :value, "--bio" => :value,
147
+ "--username" => :value, "--photo" => :value, "--json" => :flag },
148
+ json: true,
149
+ options: {
150
+ "--first-name" => "set first name",
151
+ "--last-name" => "set last name",
152
+ "--bio" => "set bio (about)",
153
+ "--username" => "set username (without @)",
154
+ "--photo" => "upload a new profile photo from FILE",
155
+ "--json" => "print profile or update result as JSON"
156
+ },
157
+ example: "tg profile --bio \"Available via Telegram\"",
158
+ json_shape: '{"ok": true, "updated": ["bio"]}'
159
+ },
160
+ {
161
+ name: "chats",
162
+ group: "Reading",
163
+ usage: "chats [--archived] [--folder N] [--limit N] [--json]",
164
+ summary: "list chats (dialogs)",
165
+ positionals: [],
166
+ spec: { "--archived" => :flag, "--folder" => :value, "--limit" => :value, "--json" => :flag },
167
+ json: true,
168
+ options: {
169
+ "--archived" => "list archived chats (folder 1)",
170
+ "--folder" => "list chats in folder N (0 main, 1 archive)",
171
+ "--limit" => "maximum number of chats to list",
172
+ "--json" => "print chats as JSON"
173
+ },
174
+ example: "tg chats --limit 20",
175
+ json_shape: '[{"id": 1, "kind": "user", "name": "...", "username": "...", "unread_count": 0, "pinned": false, "archived": false, "folder_id": null, "date": "...", "last_message": "..."}]'
176
+ },
177
+ {
178
+ name: "info",
179
+ group: "Reading",
180
+ usage: "info CHAT [--json]",
181
+ summary: "show details about a user, group, or channel",
182
+ positionals: ["CHAT"],
183
+ spec: { "--json" => :flag },
184
+ json: true,
185
+ options: {
186
+ "--json" => "print entity info as JSON"
187
+ },
188
+ example: "tg info me",
189
+ json_shape: '{"id": 1, "kind": "user", "username": "example_user", "name": "...", "first_name": "...", "last_name": null, "title": null, "bot": false, "forum": false}'
190
+ },
191
+ {
192
+ name: "listen",
193
+ group: "Reading",
194
+ usage: "listen [CHAT...] [--json]",
195
+ summary: "stream new, edited, and deleted messages until Ctrl-C",
196
+ positionals: [],
197
+ spec: { "--json" => :flag },
198
+ json: true,
199
+ options: {
200
+ "--json" => "print one JSON object per line (NDJSON)"
201
+ },
202
+ example: "tg listen @example_group --json",
203
+ json_shape: '{"event": "new|edit|delete", "id": 1, "chat_id": 1, ...}'
204
+ },
205
+ {
206
+ name: "topics",
207
+ group: "Reading",
208
+ usage: "topics CHAT [--json]",
209
+ summary: "list a forum's topics",
210
+ positionals: ["CHAT"],
211
+ spec: { "--json" => :flag },
212
+ json: true,
213
+ options: {
214
+ "--json" => "print topics as JSON"
215
+ },
216
+ example: "tg topics @example_forum",
217
+ json_shape: '[{"id": 1, "title": "General"}]'
218
+ },
219
+ {
220
+ name: "read",
221
+ group: "Reading",
222
+ usage: "read CHAT [--limit N] [--topic ID] [--json]",
223
+ summary: "human-readable message history",
224
+ positionals: ["CHAT"],
225
+ spec: { "--limit" => :value, "--topic" => :value, "--json" => :flag },
226
+ json: true,
227
+ options: {
228
+ "--limit" => "maximum number of messages to fetch (default 20)",
229
+ "--topic" => "forum topic ID to read",
230
+ "--json" => "print messages as JSON"
231
+ },
232
+ example: "tg read me --limit 10",
233
+ json_shape: '[{"id": 1, "chat_id": 1, "date": "...", "from_id": 1, "from_username": "...", "from_name": "...", "text": "...", "media": false, "file_name": null}]'
234
+ },
235
+ {
236
+ name: "search",
237
+ group: "Reading",
238
+ usage: "search [CHAT] QUERY [--from USER] [--filter KIND] [--limit N] [--json]",
239
+ summary: "search messages in a chat or globally",
240
+ positionals: [],
241
+ spec: { "--from" => :value, "--filter" => :value, "--limit" => :value, "--json" => :flag },
242
+ json: true,
243
+ options: {
244
+ "--from" => "filter messages by sender",
245
+ "--filter" => "filter by media kind (photos, videos, documents, urls, voice, music, gifs, pinned)",
246
+ "--limit" => "maximum number of results (default 20)",
247
+ "--json" => "print messages as JSON"
248
+ },
249
+ example: "tg search @example_group release --limit 10",
250
+ json_shape: '[{"id": 1, "chat_id": 1, "date": "...", "from_id": 1, "from_username": "...", "from_name": "...", "text": "...", "media": false, "file_name": null}]'
251
+ },
252
+ {
253
+ name: "export",
254
+ group: "Reading",
255
+ usage: "export CHAT [--topic ID] [--limit N | --all] [--before ID] [--out FILE]",
256
+ summary: "newest messages as JSON",
257
+ positionals: ["CHAT"],
258
+ spec: { "--topic" => :value, "--limit" => :value, "--all" => :flag, "--before" => :value, "--out" => :value },
259
+ json: true,
260
+ options: {
261
+ "--topic" => "forum topic ID to export",
262
+ "--limit" => "maximum number of messages to fetch (default 100)",
263
+ "--all" => "fetch the entire history",
264
+ "--before" => "only messages older than this message ID",
265
+ "--out" => "write JSON to FILE (owner-only 0600)"
266
+ },
267
+ example: "tg export @example_group --limit 100 --out history.json",
268
+ json_shape: '{"chat_id": -1001, "chat_title": "...", "topic_id": null, "messages": [...], "next_before": 1, "fetched": 1, "complete": true}'
269
+ },
270
+ {
271
+ name: "download",
272
+ group: "Reading",
273
+ usage: "download CHAT ID... [--out DIR] [--json]",
274
+ summary: "save messages' attachments (owner-only files)",
275
+ positionals: %w[CHAT ID],
276
+ spec: { "--out" => :value, "--json" => :flag },
277
+ json: true,
278
+ options: {
279
+ "--out" => "destination directory (default .)",
280
+ "--json" => "print downloaded file paths as JSON"
281
+ },
282
+ example: "tg download me 42 --out ./downloads",
283
+ json_shape: '[{"id": 42, "path": "./downloads/a.txt"}]'
284
+ },
285
+ {
286
+ name: "delete",
287
+ group: "Writing",
288
+ usage: "delete CHAT ID... [--yes] [--json]",
289
+ summary: "delete messages for everyone",
290
+ positionals: %w[CHAT ID],
291
+ spec: { "--yes" => :flag, "--json" => :flag },
292
+ json: true,
293
+ options: {
294
+ "--yes" => "skip interactive confirmation prompt",
295
+ "--json" => "print result as JSON"
296
+ },
297
+ example: "tg delete me 42 --yes",
298
+ json_shape: '{"ok": true, "deleted": [42], "count": 1}'
299
+ },
300
+ {
301
+ name: "send",
302
+ group: "Writing",
303
+ usage: "send CHAT TEXT [--topic ID] [--at TIME] [--file PATH]... [--voice] [--video-note] [--album] [--media] [--json]",
304
+ summary: "send a message or media (supports scheduling via --at)",
305
+ positionals: %w[CHAT TEXT],
306
+ spec: { "--topic" => :value, "--at" => :value, "--file" => :list, "--voice" => :flag,
307
+ "--video-note" => :flag, "--album" => :flag, "--media" => :flag, "--json" => :flag },
308
+ json: true,
309
+ options: {
310
+ "--topic" => "forum topic ID to post in",
311
+ "--at" => "schedule delivery (ISO-8601, UTC unless it names a zone; Unix timestamp; or +10m/+2h/+1d)",
312
+ "--file" => "attach a file (repeatable; use - to read from stdin)",
313
+ "--voice" => "send audio file as a voice note",
314
+ "--video-note" => "send video file as a round video note",
315
+ "--album" => "group attached files as an album",
316
+ "--media" => "send photos and videos as media (default: as documents, uncompressed)",
317
+ "--json" => "print sent messages as JSON"
318
+ },
319
+ example: "tg send me \"hello world\" --at +10m",
320
+ json_shape: '{"ok": true, "ids": [42], "messages": [...]}'
321
+ },
322
+ {
323
+ name: "reply",
324
+ group: "Writing",
325
+ usage: "reply CHAT ID TEXT [--json]",
326
+ summary: "reply to a specific message",
327
+ positionals: %w[CHAT ID TEXT],
328
+ spec: { "--json" => :flag },
329
+ json: true,
330
+ options: {
331
+ "--json" => "print sent message as JSON"
332
+ },
333
+ example: "tg reply me 42 \"got it\"",
334
+ json_shape: '{"ok": true, "id": 43, "message": {...}}'
335
+ },
336
+ {
337
+ name: "pin",
338
+ group: "Writing",
339
+ usage: "pin CHAT ID [--notify] [--pm-oneside] [--json]",
340
+ summary: "pin a message in a chat",
341
+ positionals: %w[CHAT ID],
342
+ spec: { "--notify" => :flag, "--pm-oneside" => :flag, "--json" => :flag },
343
+ json: true,
344
+ options: {
345
+ "--notify" => "notify chat members of the pin",
346
+ "--pm-oneside" => "pin only on your side in a private chat",
347
+ "--json" => "print result as JSON"
348
+ },
349
+ example: "tg pin @example_group 42",
350
+ json_shape: '{"ok": true, "id": 42}'
351
+ },
352
+ {
353
+ name: "unpin",
354
+ group: "Writing",
355
+ usage: "unpin CHAT [ID] [--notify] [--json]",
356
+ summary: "unpin one message, or all messages when ID is omitted",
357
+ positionals: ["CHAT"],
358
+ spec: { "--notify" => :flag, "--json" => :flag },
359
+ json: true,
360
+ options: {
361
+ "--notify" => "notify chat members",
362
+ "--json" => "print result as JSON"
363
+ },
364
+ example: "tg unpin @example_group 42",
365
+ json_shape: '{"ok": true, "id": 42}'
366
+ },
367
+ {
368
+ name: "mark-read",
369
+ group: "Writing",
370
+ usage: "mark-read CHAT [MAX_ID] [--clear-mentions] [--clear-reactions] [--json]",
371
+ summary: "mark messages read up to MAX_ID (default all)",
372
+ positionals: ["CHAT"],
373
+ spec: { "--clear-mentions" => :flag, "--clear-reactions" => :flag, "--json" => :flag },
374
+ json: true,
375
+ options: {
376
+ "--clear-mentions" => "also mark unread mentions as read",
377
+ "--clear-reactions" => "also mark unread reactions as read",
378
+ "--json" => "print result as JSON"
379
+ },
380
+ example: "tg mark-read @example_group",
381
+ json_shape: '{"ok": true, "max_id": 0}'
382
+ },
383
+ {
384
+ name: "react",
385
+ group: "Writing",
386
+ usage: "react CHAT ID [EMOJI] [--big] [--json]",
387
+ summary: "add or clear a reaction on a message",
388
+ positionals: %w[CHAT ID],
389
+ spec: { "--big" => :flag, "--json" => :flag },
390
+ json: true,
391
+ options: {
392
+ "--big" => "play a larger reaction animation",
393
+ "--json" => "print result as JSON"
394
+ },
395
+ example: "tg react me 42 👍",
396
+ json_shape: '{"ok": true, "id": 42, "reaction": "👍"}'
397
+ },
398
+ {
399
+ name: "draft",
400
+ group: "Writing",
401
+ usage: "draft [CHAT] [TEXT] [--clear] [--send] [--reply-to ID] [--json]",
402
+ summary: "list, inspect, set, send, or clear message drafts",
403
+ positionals: [],
404
+ spec: { "--clear" => :flag, "--send" => :flag, "--reply-to" => :value, "--json" => :flag },
405
+ json: true,
406
+ options: {
407
+ "--clear" => "clear the draft for CHAT",
408
+ "--send" => "send the saved draft for CHAT",
409
+ "--reply-to" => "message ID the draft replies to",
410
+ "--json" => "print draft(s) as JSON"
411
+ },
412
+ example: "tg draft me \"remember to call\"",
413
+ json_shape: '{"chat_id": 1, "text": "...", "reply_to_msg_id": null, "date": "..."}'
414
+ },
415
+ {
416
+ name: "typing",
417
+ group: "Writing",
418
+ usage: "typing CHAT [--action KIND] [--seconds N] [--json]",
419
+ summary: "send a chat action (typing, photo, record-voice, cancel, ...)",
420
+ positionals: ["CHAT"],
421
+ spec: { "--action" => :value, "--seconds" => :value, "--json" => :flag },
422
+ json: true,
423
+ options: {
424
+ "--action" => "action name (default typing; see ChatAction::NAMES)",
425
+ "--seconds" => "hold action for N seconds before cancelling (default 0: single signal)",
426
+ "--json" => "print result as JSON"
427
+ },
428
+ example: "tg typing @example_user --action typing",
429
+ json_shape: '{"ok": true, "action": "typing"}'
430
+ },
431
+ {
432
+ name: "edit",
433
+ group: "Writing",
434
+ usage: "edit CHAT ID TEXT [--json]",
435
+ summary: "replace the text (or caption) of a message you sent",
436
+ positionals: %w[CHAT ID TEXT],
437
+ spec: { "--json" => :flag },
438
+ json: true,
439
+ options: {
440
+ "--json" => "print edited message as JSON"
441
+ },
442
+ example: "tg edit me 42 \"updated text\"",
443
+ json_shape: '{"ok": true, "id": 42, "message": {...}}'
444
+ },
445
+ {
446
+ name: "forward",
447
+ group: "Writing",
448
+ usage: "forward CHAT ID... [--from CHAT] [--delete-source] [--json]",
449
+ summary: "forward without the \"Forwarded from\" header (default from: me)",
450
+ positionals: %w[CHAT ID],
451
+ spec: { "--from" => :value, "--delete-source" => :flag, "--json" => :flag },
452
+ json: true,
453
+ options: {
454
+ "--from" => "source chat (default me)",
455
+ "--delete-source" => "delete source messages after forwarding",
456
+ "--json" => "print forwarded message IDs as JSON"
457
+ },
458
+ example: "tg forward @example_group 42 --from me",
459
+ json_shape: '{"ok": true, "ids": [900], "deleted_source": []}'
460
+ },
461
+ {
462
+ name: "members",
463
+ group: "Chats",
464
+ usage: "members CHAT [--admins] [--search Q] [--limit N] [--json]",
465
+ summary: "list participants of a group or channel",
466
+ positionals: ["CHAT"],
467
+ spec: { "--admins" => :flag, "--search" => :value, "--limit" => :value, "--json" => :flag },
468
+ json: true,
469
+ options: {
470
+ "--admins" => "list administrators only",
471
+ "--search" => "filter members by name or username",
472
+ "--limit" => "maximum number of members to list",
473
+ "--json" => "print members as JSON"
474
+ },
475
+ example: "tg members @example_group --admins",
476
+ json_shape: '[{"id": 7, "kind": "user", "username": "example_user", "name": "...", "role": "channelParticipantAdmin"}]'
477
+ },
478
+ {
479
+ name: "admin",
480
+ group: "Chats",
481
+ usage: "admin CHAT USER [--title T] [--revoke] [--change-info] [--post-messages] [--edit-messages] [--delete-messages] [--ban-users] [--invite-users] [--pin-messages] [--add-admins] [--manage-call] [--anonymous] [--json]",
482
+ summary: "promote, update, or demote an administrator",
483
+ positionals: %w[CHAT USER],
484
+ spec: { "--title" => :value, "--revoke" => :flag, "--change-info" => :flag, "--post-messages" => :flag,
485
+ "--edit-messages" => :flag, "--delete-messages" => :flag, "--ban-users" => :flag,
486
+ "--invite-users" => :flag, "--pin-messages" => :flag, "--add-admins" => :flag,
487
+ "--manage-call" => :flag, "--anonymous" => :flag, "--json" => :flag },
488
+ json: true,
489
+ options: {
490
+ "--title" => "custom admin title (rank)",
491
+ "--revoke" => "demote user and remove admin rights",
492
+ "--change-info" => "allow editing chat info",
493
+ "--post-messages" => "allow posting channel messages",
494
+ "--edit-messages" => "allow editing channel messages",
495
+ "--delete-messages" => "allow deleting messages",
496
+ "--ban-users" => "allow banning users",
497
+ "--invite-users" => "allow inviting users",
498
+ "--pin-messages" => "allow pinning messages",
499
+ "--add-admins" => "allow promoting other admins",
500
+ "--manage-call" => "allow managing voice/video chats",
501
+ "--anonymous" => "remain anonymous in the group",
502
+ "--json" => "print result as JSON"
503
+ },
504
+ example: "tg admin @example_group @example_user --delete-messages --pin-messages",
505
+ json_shape: '{"ok": true, "admin": true}'
506
+ },
507
+ {
508
+ name: "ban",
509
+ group: "Chats",
510
+ usage: "ban CHAT USER [--until TIME] [--json]",
511
+ summary: "ban a user from a group or channel",
512
+ positionals: %w[CHAT USER],
513
+ spec: { "--until" => :value, "--json" => :flag },
514
+ json: true,
515
+ options: {
516
+ "--until" => "ban expiry (ISO-8601, UTC unless it names a zone; Unix timestamp; or +10m/+2h/+1d; default forever)",
517
+ "--json" => "print result as JSON"
518
+ },
519
+ example: "tg ban @example_group @example_user --until +24h",
520
+ json_shape: '{"ok": true, "until_date": null}'
521
+ },
522
+ {
523
+ name: "kick",
524
+ group: "Chats",
525
+ usage: "kick CHAT USER [--json]",
526
+ summary: "remove a user from a group or channel without banning them",
527
+ positionals: %w[CHAT USER],
528
+ spec: { "--json" => :flag },
529
+ json: true,
530
+ options: {
531
+ "--json" => "print result as JSON"
532
+ },
533
+ example: "tg kick @example_group @example_user",
534
+ json_shape: '{"ok": true}'
535
+ },
536
+ {
537
+ name: "unban",
538
+ group: "Chats",
539
+ usage: "unban CHAT USER [--json]",
540
+ summary: "unban a user in a group or channel",
541
+ positionals: %w[CHAT USER],
542
+ spec: { "--json" => :flag },
543
+ json: true,
544
+ options: {
545
+ "--json" => "print result as JSON"
546
+ },
547
+ example: "tg unban @example_group @example_user",
548
+ json_shape: '{"ok": true}'
549
+ },
550
+ {
551
+ name: "permissions",
552
+ group: "Chats",
553
+ usage: "permissions CHAT [USER] [--json]",
554
+ summary: "show a user's permissions or the chat's default restrictions",
555
+ positionals: ["CHAT"],
556
+ spec: { "--json" => :flag },
557
+ json: true,
558
+ options: {
559
+ "--json" => "print permissions as JSON"
560
+ },
561
+ example: "tg permissions @example_group @example_user",
562
+ json_shape: '{"admin": true, "creator": false, "banned": false, "left": false, ...}'
563
+ },
564
+ {
565
+ name: "admin-log",
566
+ group: "Chats",
567
+ usage: "admin-log CHAT [--limit N] [--search Q] [--json]",
568
+ summary: "list recent admin log events for a channel or supergroup",
569
+ positionals: ["CHAT"],
570
+ spec: { "--limit" => :value, "--search" => :value, "--json" => :flag },
571
+ json: true,
572
+ options: {
573
+ "--limit" => "maximum number of events (default 20)",
574
+ "--search" => "filter events by query",
575
+ "--json" => "print events as JSON"
576
+ },
577
+ example: "tg admin-log @example_group --limit 10",
578
+ json_shape: '[{"id": 1, "date": "...", "user_id": 7, "action": "channelAdminLogEventActionChangeTitle"}]'
579
+ },
580
+ {
581
+ name: "stats",
582
+ group: "Chats",
583
+ usage: "stats CHAT [MSG_ID] [--json]",
584
+ summary: "show broadcast, supergroup, or message statistics",
585
+ positionals: ["CHAT"],
586
+ spec: { "--json" => :flag },
587
+ json: true,
588
+ options: {
589
+ "--json" => "print raw TL stats object as JSON"
590
+ },
591
+ example: "tg stats @example_channel --json",
592
+ json_shape: '{"_": "stats.broadcastStats", ...}'
593
+ },
594
+ {
595
+ name: "join",
596
+ group: "Chats",
597
+ usage: "join CHAT_OR_INVITE [--json]",
598
+ summary: "join a public channel/group or accept an invite link",
599
+ positionals: ["CHAT_OR_INVITE"],
600
+ spec: { "--json" => :flag },
601
+ json: true,
602
+ options: {
603
+ "--json" => "print result as JSON"
604
+ },
605
+ example: "tg join @example_channel",
606
+ json_shape: '{"ok": true, "target": "@example_channel"}'
607
+ },
608
+ {
609
+ name: "leave",
610
+ group: "Chats",
611
+ usage: "leave CHAT [--json]",
612
+ summary: "leave a channel or group",
613
+ positionals: ["CHAT"],
614
+ spec: { "--json" => :flag },
615
+ json: true,
616
+ options: {
617
+ "--json" => "print result as JSON"
618
+ },
619
+ example: "tg leave @example_channel",
620
+ json_shape: '{"ok": true, "chat": "@example_channel"}'
621
+ },
622
+ {
623
+ name: "archive",
624
+ group: "Chats",
625
+ usage: "archive CHAT... [--json]",
626
+ summary: "move one or more chats to the archive folder",
627
+ positionals: ["CHAT"],
628
+ spec: { "--json" => :flag },
629
+ json: true,
630
+ options: {
631
+ "--json" => "print result as JSON"
632
+ },
633
+ example: "tg archive @example_group",
634
+ json_shape: '{"ok": true, "folder": 1, "count": 1}'
635
+ },
636
+ {
637
+ name: "unarchive",
638
+ group: "Chats",
639
+ usage: "unarchive CHAT... [--json]",
640
+ summary: "move one or more chats back to the main list",
641
+ positionals: ["CHAT"],
642
+ spec: { "--json" => :flag },
643
+ json: true,
644
+ options: {
645
+ "--json" => "print result as JSON"
646
+ },
647
+ example: "tg unarchive @example_group",
648
+ json_shape: '{"ok": true, "folder": 0, "count": 1}'
649
+ },
650
+ {
651
+ name: "delete-chat",
652
+ group: "Chats",
653
+ usage: "delete-chat CHAT [--revoke] [--yes] [--json]",
654
+ summary: "leave a group/channel and delete its dialog/history",
655
+ positionals: ["CHAT"],
656
+ spec: { "--revoke" => :flag, "--yes" => :flag, "--json" => :flag },
657
+ json: true,
658
+ options: {
659
+ "--revoke" => "also delete history for the other side where supported",
660
+ "--yes" => "skip interactive confirmation prompt",
661
+ "--json" => "print result as JSON"
662
+ },
663
+ example: "tg delete-chat @example_group --yes",
664
+ json_shape: '{"ok": true, "chat": "@example_group", "revoke": false}'
665
+ },
666
+ {
667
+ name: "profile-photos",
668
+ group: "Chats",
669
+ usage: "profile-photos CHAT [--limit N] [--out DIR] [--json]",
670
+ summary: "list (and optionally download) profile photos of a user or chat",
671
+ positionals: ["CHAT"],
672
+ spec: { "--limit" => :value, "--out" => :value, "--json" => :flag },
673
+ json: true,
674
+ options: {
675
+ "--limit" => "maximum number of photos to list",
676
+ "--out" => "download photos into DIR (owner-only)",
677
+ "--json" => "print photo info as JSON"
678
+ },
679
+ example: "tg profile-photos me --limit 5",
680
+ json_shape: '[{"id": 1, "date": "...", "path": null}]'
681
+ },
682
+ {
683
+ name: "inline",
684
+ group: "Bots",
685
+ usage: "inline BOT QUERY [--pick N] [--to CHAT] [--json]",
686
+ summary: "query an inline bot, optionally sending result N",
687
+ positionals: %w[BOT QUERY],
688
+ spec: { "--pick" => :value, "--to" => :value, "--json" => :flag },
689
+ json: true,
690
+ options: {
691
+ "--pick" => "send result at 0-based index N",
692
+ "--to" => "destination chat for --pick (default me)",
693
+ "--json" => "print results as JSON"
694
+ },
695
+ example: "tg inline @gif cats --json",
696
+ json_shape: '[{"index": 0, "id": "...", "type": "article", "title": "...", "description": "...", "url": null}]'
697
+ },
698
+ {
699
+ name: "click",
700
+ group: "Bots",
701
+ usage: "click CHAT ID BUTTON [--json]",
702
+ summary: "click a message's button by index or text",
703
+ positionals: %w[CHAT ID BUTTON],
704
+ spec: { "--json" => :flag },
705
+ json: true,
706
+ options: {
707
+ "--json" => "print callback response as JSON"
708
+ },
709
+ example: "tg click @example_bot 42 0",
710
+ json_shape: '{"ok": true, "message": "...", "url": null, "alert": false}'
711
+ },
712
+ {
713
+ name: "call",
714
+ group: "Other",
715
+ usage: "call METHOD [JSON]",
716
+ summary: "call any raw TL layer function with JSON arguments and print JSON",
717
+ positionals: ["METHOD"],
718
+ spec: {},
719
+ json: true,
720
+ options: {},
721
+ example: "tg call messages.getHistory '{\"peer\": \"me\", \"limit\": 5}'",
722
+ json_shape: '{"_": "messages.messages", ...}'
723
+ },
724
+ {
725
+ name: "completion",
726
+ group: "Other",
727
+ usage: "completion bash|zsh|fish",
728
+ summary: "print shell completion script for bash, zsh, or fish",
729
+ positionals: ["SHELL"],
730
+ spec: {},
731
+ json: false,
732
+ options: {},
733
+ example: "tg completion zsh > ~/.zfunc/_tg",
734
+ json_shape: nil
735
+ }
736
+ ].freeze
737
+
738
+ module_function
739
+
740
+ def find(name)
741
+ COMMANDS.each { |entry| return entry if entry[:name] == name }
742
+ nil
743
+ end
744
+
745
+ def names
746
+ COMMANDS.map { |entry| entry[:name] }
747
+ end
748
+
749
+ def grouped
750
+ out = []
751
+ GROUPS.each do |group|
752
+ entries = COMMANDS.select { |entry| entry[:group] == group }
753
+ out << [group, entries] unless entries.empty?
754
+ end
755
+ out
756
+ end
757
+
758
+ def format_help
759
+ lines = ["usage: tg COMMAND [ARGS]", ""]
760
+ grouped.each do |group, entries|
761
+ lines << "#{group}:"
762
+ entries.each do |entry|
763
+ usage = entry[:usage]
764
+ if usage.size <= 32
765
+ lines << format(" %-32s %s", usage, entry[:summary])
766
+ else
767
+ lines << " #{usage}"
768
+ lines << format(" %-32s %s", "", entry[:summary])
769
+ end
770
+ end
771
+ lines << ""
772
+ end
773
+ lines << "Other flags:"
774
+ lines << " help [COMMAND] show general help or help for COMMAND"
775
+ lines << " --version show version and Telegram API layer"
776
+ lines << ""
777
+ lines << "exit codes:"
778
+ EXIT_CODES.each { |code, desc| lines << " #{code} #{desc}" }
779
+ "#{lines.join("\n")}\n"
780
+ end
781
+
782
+ def format_command_help(entry)
783
+ lines = [
784
+ "usage: tg #{entry[:usage]}",
785
+ "",
786
+ entry[:summary]
787
+ ]
788
+ opts = entry[:options]
789
+ if opts && !opts.empty?
790
+ lines << ""
791
+ lines << "options:"
792
+ opts.each do |flag, desc|
793
+ lines << format(" %-20s %s", flag, desc)
794
+ end
795
+ end
796
+ if entry[:example]
797
+ lines << ""
798
+ lines << "example:"
799
+ lines << " #{entry[:example]}"
800
+ end
801
+ "#{lines.join("\n")}\n"
802
+ end
803
+ end
804
+ end
805
+ end