Skip to content

Rule for when a Telegram method becomes a command and when it stays in tg api #241

Backlog

Description

@ertembiyik

On #192 you asked why pin gets its own command when the rest of the API stays in tg api, and said you don't want a pile of code per method. I agree, and I'd like a rule we can point to before more wrappers get written. This is a proposal for that rule, with what it means for the open PRs from #23.

What the four wrappers cost

PR Command Source Tests Docs
#187 messages edit 308 666 58
#190 messages delete 286 698 105
#191 messages forward 308 599 65
#192 messages pin / unpin 251 580 50
Total (added lines) 1,153 2,543 278

By my count 15 to 80 source lines in each are about the method itself. The rest repeats in all four: the allowedChats check, the cache write, the table of Telegram refusals, the output shape, the argument checks.

The reason #23 gives for them, that the caller has to build TL arguments and input peers, mostly went away with db3cad0: tg api now takes me, @username or an ID wherever a method wants an InputPeer, InputUser or InputChannel.

The rule

Go down the list; the first "yes" decides.

  1. The value is local (the cache, the daemon, files on disk). It is a command. messages search, media download, send --file are here: tg api can't upload or read the cache.
  2. The difficulty belongs to a type, not to a method (a peer, random IDs, formatted text, the Updates in an answer, the name of an error). Fix it once in tg api, for every method that has that type.
  3. A correct result needs a choice of method or several calls, and a wrong raw call does damage that can't be undone. It is a thin command that holds only that logic.
  4. Otherwise it is a recipe: one line in the skill, covered by a test that builds the request against the bundled schema, so a schema bump that breaks a recipe fails CI.

Applied to the open PRs

PR By the rule Why
#192 pin, unpin recipe tg api messages.updatePinnedMessage --peer <chat> --id 42 already builds the right request
#187 edit recipe tg api messages.editMessage --peer <chat> --id 42 --message "fixed" already builds; formatted text is a type-level question (rule 2)
#191 forward rule 2, then recipe needs random IDs (below)
#190 delete thin command two methods by chat type, and messages.deleteMessages names no chat: an ID taken from a channel deletes the private message with the same number

The search, media and send PRs (#216, #238, #239) are rule 1 and unaffected.

What tg api lacks for this

  • Random IDs in a vector. A required random_id: long is filled, a required Vector<long> is not, so messages.forwardMessages without a hand-written randomId is built without the field and never reaches Telegram.
  • The error name. A failure carries telegramErrorCode (the number) but not Telegram's name (MESSAGE_ID_INVALID), which is what a caller acts on.
  • allowedChats. With an allowlist set, tg api is refused as a whole. That was your call and I'd keep it unless you want otherwise: scoping by peer-typed arguments is possible, but peers nested inside objects are easy to miss.
  • The cache. tg api writes nothing to it. The daemon picks up edits and deletes; I found nothing that updates is_pinned after a message is first stored.

If you agree

One PR that fills random-ID vectors, adds the error name and puts the recipes with their tests into the skill; #187, #191 and #192 get closed in its favour; #190 stays, cut down to what is specific to deleting. If you'd rather keep some of the wrappers, the rule still tells us which future ones not to write.

Questions for you:

  • Is the rule right, and is rule 3 the right bar for a command?
  • Should tg api stay off under allowedChats?
  • Should a method called through tg api update the cache at all, or is the daemon the only writer for those?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions