Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,37 @@ $ npx skills add https://github.com/gotd/cli --all
$ npx skills add https://github.com/gotd/cli --skill tg
```

## Inspect and invoke Telegram RPCs

Use the schema compiled into `tg` for local discovery and invocation.

```console
$ tg rpc list -o json
$ tg rpc describe messages.sendMedia -o json
$ tg rpc invoke --yes help.getConfig '{}' -o json
```

Pass `--schema-cache` to use Telegram Desktop's published TL schema at runtime.
The first request after a five-minute cache window checks for an update. A
valid cached schema remains available if that check fails. Newly published
methods use the same CLI commands when Telegram supports their layer.

```console
$ tg rpc --schema-cache ~/.cache/tg/api.tl describe messages.getTranscriptionModels -o json
$ tg rpc --schema-cache ~/.cache/tg/api.tl invoke --yes messages.getTranscriptionModels '{"version":0}' -o json
```

Arguments use TL field names. Use `{"_":"inputPeerSelf"}` to select a concrete
constructor for an abstract field. Pass 64-bit integers as decimal strings
and byte fields as base64. Runtime mode calculates flags from supplied fields.
Omit false presence flags and flag words such as `flags`.

Raw calls run with the existing user session and require `--yes` because
they may change Telegram account data. For a source build reusing a session
created by a release binary, use `tg devices` with the release binary to find
the current device's public app ID. Pass that ID as `--linked-app-id` in
runtime mode. Commands such as `tg upload` handle multi-step operations.

## What you can do

Run `tg --help` for the full, grouped command list, or `tg <command> --help` for details.
Expand Down
25 changes: 20 additions & 5 deletions cmd/tg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ func (a *app) ensureActive() error {

// skipConfig reports whether the command runs without a loaded config/session.
func skipConfig(cmd *cobra.Command) bool {
switch cmd.CommandPath() {
case "tg " + cmdRPC + " describe", "tg " + cmdRPC + " " + cmdList:
return true
}
for c := cmd; c != nil; c = c.Parent() {
switch c.Name() {
case "init", "docs", "completion", "help",
Expand All @@ -171,8 +175,10 @@ func skipConfig(cmd *cobra.Command) bool {

// runParams configures a client session for connect/run.
type runParams struct {
auth authKind
updates bool
auth authKind
updates bool
layer int // Optional schema layer for runtime TL RPCs.
linkedAppID int // Public app ID of a pre-existing session for runtime TL RPCs.
// authorize performs interactive authentication when the session is not yet
// authorized. If nil, user sessions error with errNotAuthorized and bot
// sessions authenticate with the configured token.
Expand All @@ -191,6 +197,7 @@ func (a *app) optionsFor(st *accountState, rp runParams, d tg.UpdateDispatcher)
Device: deviceConfig(),
Middlewares: mw,
SessionStorage: a.sessionStore(st.label, st.acc, rp.auth.String()),
Layer: rp.layer,
}
if rp.updates {
opts.UpdateHandler = d
Expand All @@ -214,14 +221,22 @@ func (a *app) connectWith(
rp runParams,
f func(ctx context.Context, client *telegram.Client, d tg.UpdateDispatcher) error,
) error {
if (rp.layer != 0 || rp.linkedAppID != 0) &&
(rp.layer <= 0 || rp.auth != authUser || rp.updates || rp.authorize != nil) {
return errors.New("runtime schema connections require an existing user session without updates")
}
var d tg.UpdateDispatcher
if rp.updates {
d = tg.NewUpdateDispatcher()
}

appID, appHash, err := effectiveCreds(st.acc)
if err != nil {
return err
appID, appHash := rp.linkedAppID, ""
if appID == 0 {
var err error
appID, appHash, err = effectiveCreds(st.acc)
if err != nil {
return err
}
}
client := telegram.NewClient(appID, appHash, a.optionsFor(st, rp, d))

Expand Down
2 changes: 1 addition & 1 deletion cmd/tg/chats.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (a *app) newChatsListCmd() *cobra.Command {
)

cmd := &cobra.Command{
Use: "list",
Use: cmdList,
Short: "List dialogs with unread counts and flags",
Long: `List dialogs (chats) newest-first, with unread counts, pinned/muted/archived
flags and a last-message preview.`,
Expand Down
1 change: 1 addition & 0 deletions cmd/tg/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ below apply to every command.`,
a.newChatsCmd(),
a.newHistoryCmd(),
a.newSendCmd(),
a.newRPCCmd(),
a.newReplyCmd(),
a.newEditCmd(),
a.newDeleteCmd(),
Expand Down
Loading