Skip to content

feat(messages): forward messages with tg messages forward - #191

Open
ertembiyik wants to merge 12 commits into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/messages-forward
Open

ertembiyik wants to merge 12 commits into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/messages-forward

Conversation

@ertembiyik

@ertembiyik ertembiyik commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What and why

tg messages forward --from <peer> --to <peer> --id <id>... forwards messages from one chat to another, in the shape #23 proposes. Refs #23

The need: forwarding meant a raw tg api messages.forwardMessages call with two hand-built input peers and a random ID per message, and the copies reached the cache only through a daemon or a --fresh read.

Before (0.14.0)

$ tg messages forward --from me --to me --id 1
USAGE tg messages list|get|search|wait|watch|unread|read
…
Unknown command forward

After (real output of the e2e fake; the second item is cut here, it has the same fields)

$ tg messages forward --from @alice --to @bob --id 13 --id 12
{
  "success": true,
  "data": {
    "fromChatId": 10,
    "toChatId": 20,
    "forwarded": [
      {
        "messageId": 13,
        "newMessageId": 32,
        "message": {
          "chatId": 20, "messageId": 32, "fromId": 999, "text": "Forwarded message 13",
          "messageType": "text", "hasMedia": false, "mediaPath": null, "attachment": null,
          "action": null, "isOutgoing": true, "isEdited": false, "isPinned": false,
          "isDeleted": false, "replyToId": null, "forwardFromId": 10, "editDate": null,
          "date": 1760000000,
          "chat": { "id": 20, "title": "Bob", "username": "bob", "type": "private" },
          "sender": { "id": 999, "username": null, "firstName": null, "lastName": null }
        }
      },
      { "messageId": 12, "newMessageId": 31, "message": { … } }
    ]
  }
}

A refusal says what is wrong (whitespace collapsed):

$ tg messages forward --from @vault --to @bob --id 12
{ "success": false, "error": { "code": "TELEGRAM_ERROR", "message": "The source chat protects its content: Telegram does not allow forwarding from it", "details": { "fromChatId": -1005555555555, "toChatId": 20, "messageIds": [12], "telegramError": "CHAT_FORWARDS_RESTRICTED" }, "retryable": false } }

How it works

  • One call: messages.forwardMessages with one random ID per message. --from and --to resolve as --chat does for tg messages get; a bad one is reported under its own flag name.
  • Old ID to new ID. Each updateMessageID of the response carries the random ID sent with a message and the copy's new ID, so forwarded pairs every source ID with its copy, in the order the IDs were given. Telegram numbers the copies in ascending order of their source IDs, whatever order they are passed in (seen live, below); the docs say so.
  • The cache follows. The copies in the response are written to messages_cache through the mapper tg send uses (fromUpdates, now exported as newMessageInputs), and each pair carries its copy as the item tg messages get prints. get and search show the copies at once; list still asks Telegram, as after tg send, because a forward records no history range.
  • Limits and access. Up to 100 IDs per call, more is INVALID_ARGS before connecting; a repeated ID counts once. It is a write with chatArgs: ['from', 'to']: read-only mode refuses it, and both chats must be in allowedChats (a test covers each side).

Telegram's refusals are TELEGRAM_ERROR with the name in details.telegramError, as in #187: CHAT_FORWARDS_RESTRICTED, a missing source message, no right to post in the target (CHAT_WRITE_FORBIDDEN, CHAT_SEND_<KIND>_FORBIDDEN and the like), a block on either side, slow mode, an unknown chat. The names come from the method's docs page.

Decisions you may want to change:

  • Output shape: { fromChatId, toChatId, forwarded: [{ messageId, newMessageId, message }] }. newMessageId and message are null when the response has no updateMessageID for a message; the command never guesses a pair.
  • Refusal details name both chats and all IDs, because Telegram doesn't say which ID it refused.
  • The 100-ID cap is Telegram's usual per-call limit; the method page doesn't state one.

Left out: --silent, dropping the author or captions, scheduling, forwarding into a forum topic.

This is one of four PRs for #23, one command each: edit (#187), delete, forward, pin. Each is cut from main and stands alone. All four touch src/commands/messages.ts, src/commands/access.ts, the shared fake client (src/__e2e__/helpers/fake-client-cli.ts), the README row for #23 and the CHANGELOG, so each one that lands conflicts with the rest in those spots; I merge main into the others as you land them.

Update: main merged in (5443e73)

How I checked it

  • E2E with the fake client (8 tests): private chat to private chat, into a supergroup, into Saved Messages, several IDs with repeats and their ID pairs, the copies found by get and search afterwards, a refusal with nothing cached, a bad --id and 101 IDs refused offline, a missing --to, no credentials. The access suite adds one test that each of the two chats is checked.

  • Unit tests (13): the request, the pairing (out of order, a missing updateMessageID, a response without updates), each refusal, other errors passed on.

  • Live, by hand, on a personal account: two messages in Saved Messages forwarded to Saved Messages, IDs passed newest first. Both pairs came back with the copies as items; the older source got the lower new ID; messages.getMessages showed both copies with the same text; tg messages get without --fresh read a copy from the cache. Then deleted all four.

  • A step in the live smoke suite (src/__integration__/). Written, not run: I have no test session. Its check follows what the live run showed: a copy of an own message in Saved Messages carries no forward header.

  • Not verified live: a forward to another person or a group, and the refusals.

  • bun run check: clean. bun run test: 2360 pass, 0 fail. bun run test:e2e: 465 pass, 0 fail.

  • Tests pass locally and use made-up data only

  • README, docs and affected demos follow the product content standard (docs/cli-commands.md, docs/configuration.md, docs/api-design.md, README, CHANGELOG)

  • Title follows the repo's commit style: Conventional Commits (feat: …, fix: …) unless AGENTS.md says otherwise

Update: second code review (a470814)

A second, independent review of this branch. Each point below was first shown by a failing test, then fixed.

  • Docs: newMessageId: null means Telegram's response did not confirm a copy of that message, which may have been skipped; the text said the forward is done either way.
  • The one-call test fails if the forward request is sent twice.
  • Shared code on main that the review also pointed at, unchanged here: the access guard and the command resolve the account and the chat separately; a cached username can outlive its owner; flags a command does not declare are ignored; the activity log write after a successful request can turn it into a failure.

After these commits: bun run check clean, bun run test 1208 pass, bun run test:e2e 349 pass, 0 fail.

Update: main merged in (649eef9)

main up to #220 merged in, no conflicts. One follow-up commit (216c588), for both --from and --to: the command acts on the chat the allowedChats check resolved (checkedChat), as tg send and tg messages read do since #220. No test of its own: the handoff is tested on main for tg send; here the command's e2e file was run once by hand with allowedChats set, and every case whose chat was in the list passed.

After the merge: bun run check clean, bun run test 1219 pass, bun run test:e2e 350 pass, 0 fail.

Update: main merged in (853a651)

main up to 0.15.1 and #232 merged in. Conflicts: CHANGELOG.md (the entry of this PR now sits under the new ## [Unreleased]) and the fake client of the e2e tests.

  • mtcute now loads only when a command calls Telegram (perf(cli): load mtcute only when a command calls Telegram #226), so forward-message.ts imports its types with import type and reads RpcError through await import(); src/cli.test.ts passes. The random IDs are built as tg send builds its one, instead of randomLong from @mtcute/bun/utils.js, which would load mtcute at start.
  • 191aa48: the command resolves an @username through Telegram, not the cache (freshUsername), as tg send and tg messages read do since fix(send): resolve @username through Telegram for writes #222, so a username that moved never forwards from or to its previous owner. Both --from and --to resolve this way, as the allowedChats check of a write does. The fake client knows the two usernames the e2e tests forward between, and the allowedChats case of tg messages forward runs through it.

After the merge: bun run check clean, bun run test 1231 pass, bun run test:e2e 376 pass, 0 fail.

Update: main merged in (ac85803)

main up to 0.15.2 (#234) merged in. One conflict: CHANGELOG.md (the entry of this PR stays under ## [Unreleased], above the new 0.15.2 section). No code change in this PR.

After the merge: bun run check clean, bun run test 1231 pass, bun run test:e2e 376 pass, 0 fail.

Update: main merged in (392c8d8)

Besides the conflicts in CHANGELOG.md and README.md, flagValues on main now takes the command's flag definitions, so forward.ts passes them; with that, a value given to another string flag is no longer read as an --id, as on main. Checks: bun run check clean, unit 1351 pass, e2e 449 pass, 0 fail.

🤖 Generated with Claude Code

ertembiyik and others added 5 commits November 18, 2026 15:09
main now has neoromantic#175 (neoromantic#194), neoromantic#174 and neoromantic#155. README and CHANGELOG: both sides kept. main dropped MessagesHistory.getMany (neoromantic#174): the item is read with get. Fixture groups take part in the tests: main caches a group's new messages only while the account writes there (neoromantic#155).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
main now has neoromantic#195: Knip in bun run check, and long as a runtime dependency. No conflict; the check is clean.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
main now has neoromantic#196, a test time budget only. No conflict; the check is clean.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
README, access.ts, messages.ts and send/sent-message-cache.ts conflicted only with main's oxfmt reformat and inferred return types and were resolved by keeping main's code with this branch's forward lines (newMessageInputs stays exported for the forward); the forward's e2e, unit and integration tests and its fake-client and access additions moved to main's colocated layout (src/test/*, *.test-utils.ts, forward-message.test.ts beside its module). The merge also brought the code under main's oxlint/landmines rules (no prose comments, inferred return types), reused createAccountCacheDb and raised the integration file's allowed raised-timeout count to 8.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik and others added 7 commits November 19, 2026 19:53
…skipped

The docs said the forward is done either way when newMessageId is null, but a
message Telegram skipped has no copy either. The one-call test now fails if
the forward request is sent twice.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…ssages-forward

# Conflicts:
#	CHANGELOG.md
#	src/test/fake-client-cli.test-utils.ts
…ssages-forward

Pass the forward command's arg definitions to flagValues, which now takes
them to skip a string flag's value that looks like a repeated --id.

# Conflicts:
#	CHANGELOG.md
#	README.md

Co-Authored-By: Claude Fable 5.2 <[email protected]>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant