Skip to content

feat(messages): edit a sent message with tg messages edit - #187

Open
ertembiyik wants to merge 17 commits into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/messages-edit
Open

ertembiyik wants to merge 17 commits into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/messages-edit

Conversation

@ertembiyik

@ertembiyik ertembiyik commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What and why

tg messages edit --chat <peer> --id <id> --message <text> replaces the text of a message you sent, or the caption of a message with a file. It is the edit part of #23, with the command shape that issue proposes. Refs #23

The need: fixing a sent message meant a raw tg api messages.editMessage call with a hand-built input peer, and the cache kept the old text until a daemon or a --fresh read replaced it.

Before (0.13.1; 0.14.0 is the same here)

$ tg messages edit --chat me --id 1 --message x
USAGE tg messages list|get|search|wait|watch|unread|read
…
Unknown command edit

After (real output of the e2e fake: Alice's chat, message 12 was "See you at nine.")

$ tg messages edit --chat @alice --id 12 --message "See you at ten."
{
  "success": true,
  "data": {
    "chatId": 10,
    "messageId": 12,
    "edited": true,
    "message": {
      "chatId": 10,
      "messageId": 12,
      "fromId": 999,
      "text": "See you at ten.",
      "messageType": "text",
      "hasMedia": false,
      "mediaPath": null,
      "attachment": null,
      "action": null,
      "isOutgoing": true,
      "isEdited": true,
      "isPinned": false,
      "isDeleted": false,
      "replyToId": null,
      "forwardFromId": null,
      "editDate": 1760003600,
      "date": 1760000000,
      "chat": { "id": 10, "title": "Alice", "username": "alice", "type": "private" },
      "sender": { "id": 999, "username": null, "firstName": null, "lastName": null }
    }
  }
}

(chat and sender are folded onto one line here; nothing else is changed.)

A refusal says what is wrong. Message 14 is Alice's:

$ tg messages edit --chat @alice --id 14 --message "Ten then."
{
  "success": false,
  "error": {
    "code": "TELEGRAM_ERROR",
    "message": "You can't edit message 14: it was sent by someone else",
    "details": {
      "chatId": 10,
      "messageId": 14,
      "telegramError": "MESSAGE_AUTHOR_REQUIRED"
    },
    "retryable": false
  }
}
Telegram's error message
MESSAGE_NOT_MODIFIED Message 12 already has this text
MESSAGE_ID_INVALID Telegram has no message 12 in this chat that can be edited
MESSAGE_AUTHOR_REQUIRED You can't edit message 12: it was sent by someone else
MESSAGE_EDIT_TIME_EXPIRED Message 12 is too old: Telegram no longer allows editing it
MESSAGE_TOO_LONG, MEDIA_CAPTION_TOO_LONG The text is too long for a message / … for the caption of a media message
CHAT_WRITE_FORBIDDEN, CHAT_ADMIN_REQUIRED You do not have permission to edit messages in this chat

Any other failure goes through reportHistoryError, as for tg messages read.

How it works

  • One call: messages.editMessage with the peer, the ID and the text. --chat resolves as it does for tg messages get and read.
  • The cache follows at once. The edited message in Telegram's response (updateEditMessage or updateEditChannelMessage) is written to messages_cache, as tg send doesn't add the sent message to the cache #19 does for tg send, and read back as the item tg messages get prints. So tg messages search finds the new text, and no longer the old, without a daemon.
  • It is a write. COMMAND_ACCESS marks it write with chatArgs: ['chat']: read-only mode refuses it before it connects, and allowedChats applies. The access e2e tests pick both up from the table.
  • Checks before the network: --id must be a message ID, and an empty --message is INVALID_ARGS.

Decisions you may want to change:

  • edited is always true and message can be null. If the response holds no message, or the cache write fails, the edit is still done, so the command succeeds and message is null.
  • MESSAGE_NOT_MODIFIED is an error, not a quiet success: this call changed nothing.
  • The text is plain by default. No entities are sent, so formatting the message had is dropped. --markdown and --html make it a rich message (see the update below).
  • details.telegramError is a new detail key, the error's name; tg api already has details.telegramErrorCode for the number.
  • -m is an alias of --message, as in tg send.

Left out, on purpose:

The shared fake client (src/__e2e__/helpers/fake-client-cli.ts) now answers messages.editMessage. This touches src/commands/access.ts, src/commands/messages.ts, README and CHANGELOG, as #183 does; I'll rebase whichever lands later.

Update: main merged in (5443e73)

Update: --markdown and --html (377c39e, 24b005f, e96dfc5)

#175 is on main, so the edit command takes the same two flags as tg send:

$ tg messages edit --chat @alice --id 12 --markdown -m "# Plan"
  • Same method, one more field. messages.editMessage takes rich_message exactly as messages.sendMessage does; there is no separate method for rich edits. With a flag the request carries richMessage (inputRichMessageMarkdown or inputRichMessageHTML) and no message; without one it is the plain request as before.
  • format in the result: markdown, html, or null for plain text, as in tg send.
  • A message with a file is refused. Telegram accepts a rich edit of a message that holds a file, then stores neither the rich text nor the caption the message had: the caption is gone. So a rich edit first reads the message (messages.getMessages or channels.getMessages, one more request, only with a flag) and gives INVALID_ARGS (exit 3) when it has any media other than a link preview, including kinds the cache can't classify (an expired photo or file, a giveaway, media this version doesn't know). This matches the Bot API, where editMessageText takes rich_message and editMessageCaption does not. A text message with a link preview becomes rich; a plain edit of a caption works as before.
  • Other refusals: both flags together is INVALID_ARGS (exit 3) before the network. A RICH_MESSAGE_* refusal is TELEGRAM_ERROR with the name in details.telegramError and the same suggestion tg send gives.
  • One module for both commands: the flag parsing, the request part and the RICH_MESSAGE_* hints moved from src/commands/send.ts to src/commands/send/rich-message.ts; tg send behaves as before.
  • Tests: 7 unit (the request for each format, a rich refusal with its suggestion, a file, an expired photo and unknown media refused with nothing sent, a link preview let through) and 4 e2e (a rich edit stored and found by search, then plain text again; a file refused in Saved Messages and in a supergroup with no edit request; both flags refused offline). The fake client answers a rich edit as Telegram does: empty text, the blocks in richMessage. Each new test fails when its behaviour is broken (checked by breaking the cache fallback, the media rule and the channel lookup in turn). bun run check clean, bun run test 1213 pass, bun run test:e2e 352 pass, 0 fail.
  • Live, by hand, in Saved Messages of a personal account, from an isolated data directory, read back with messages.getMessages each time and deleted after:
    • plain text to Markdown (a heading, a table, a list): richMessage with those blocks, editDate set, the cached text is the blocks as Markdown;
    • that rich message to HTML, then back to plain text;
    • the same Markdown twice: MESSAGE_NOT_MODIFIED; markup with no content: RICH_MESSAGE_EMPTY with its suggestion;
    • a document with a caption, before the fix: success, and Telegram left the file with an empty caption and no rich message. After the fix: INVALID_ARGS, the caption intact; a plain edit of the caption works;
    • a text message with a link preview: becomes rich.
  • Not verified live: a photo or another attachment kind (refused like a document), and a supergroup or channel.

How I checked it

  • E2E with the fake client (8 tests): the text replaced, printed and stored; the new text searchable and the old one not; a supergroup (updateEditChannelMessage) and Saved Messages; a message the cache didn't hold; a refusal with the cached message left as it was; an empty text and a bad --id refused offline; no credentials. The access suite adds read-only and allowedChats refusals for the new command from the table.

  • Unit tests (11): the request, each refusal in the table above, other Telegram errors passed on with the unknown-chat hint, and a response without a message.

  • Live, by hand, on a personal account in Saved Messages: sent one message with a bold entity through tg api, edited it with this command, and read it back with messages.getMessages: the new text, editDate set, the bold entity gone. The same text again gave MESSAGE_NOT_MODIFIED with the message above. tg messages get without --fresh and tg messages search showed the new text. Then deleted the message.

  • A step in the live smoke suite (src/__integration__/): send to Saved Messages, edit, check Telegram and the cache. Written, not run: I have no test session.

  • Not verified live: a supergroup or channel, a caption, and the refusals other than MESSAGE_NOT_MODIFIED.

  • bun run check: clean. bun run test: 2334 pass, 0 fail. bun run test:e2e: 460 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 (b22fbfb)

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

  • The edited message from the response is cached only when it belongs to the edited chat; it was matched by message ID alone.
  • 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 1214 pass, bun run test:e2e 352 pass, 0 fail.

Update: main merged in (649eef9)

main up to #220 merged in, no conflicts. One follow-up commit (84d1019): 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 1225 pass, bun run test:e2e 353 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]), src/commands/send.ts and the fake client of the e2e tests.

After the merge: bun run check clean, bun run test 1237 pass, bun run test:e2e 379 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 1237 pass, bun run test:e2e 379 pass, 0 fail.

Update: main merged in (392c8d8)

No changes beyond the conflicts in CHANGELOG.md, README.md and src/test/fake-client-cli.test-utils.ts. Checks: bun run check clean, unit 1357 pass, e2e 452 pass, 0 fail.

🤖 Generated with Claude Code

ertembiyik and others added 3 commits November 18, 2026 14:09
tg messages edit --chat <peer> --id <id> --message <text> replaces the
text of a message, or the caption of a message with a file, through
messages.editMessage. It prints the edited message as tg messages get
does and stores it in the cache at once, so search follows without a
daemon. Telegram's refusals are TELEGRAM_ERROR with the reason in
details.telegramError. Read-only mode refuses the command and
allowedChats applies.

Refs neoromantic#23

Co-Authored-By: Claude Fable 5.2 <[email protected]>
main is now the 0.14.0 release (neoromantic#189). This PR's changelog entries move
back under Unreleased: a plain merge left them in the released section.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
ertembiyik and others added 14 commits November 18, 2026 20:38
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 and messages.ts conflicted only with main's oxfmt reformat and were resolved by keeping main's code with this branch's edit lines; the edit's e2e, unit and integration tests and its fake-client and access additions moved to main's colocated layout (src/test/*, *.test-utils.ts, edit-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]>
messages.editMessage takes the same rich_message field as messages.sendMessage,
so tg messages edit takes the same two flags as tg send. The flag parsing, the
request part and the hints for RICH_MESSAGE_* refusals move to one module that
both commands use.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Telegram accepts messages.editMessage with rich_message for a message that
holds a file, then stores neither the rich text nor the caption the message
had. A rich edit now reads the message first and refuses with INVALID_ARGS
when it has an attachment other than a link preview.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The check went through the media classifier, which gives nothing for an
expired photo or file, a giveaway or media this version doesn't know, so a
rich edit of such a message still reached Telegram. It now refuses whenever
the message has media other than a link preview.

The fake client answers a rich edit as Telegram does (empty text, the blocks
in richMessage) and answers channels.getMessages, so the tests cover the
cached text of a rich edit, the way back to plain text and a supergroup.

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

The response was matched by message ID alone, so an edit of the same ID in
another chat carried by the same response would have been stored under the
chat that was named.

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

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

# Conflicts:
#	CHANGELOG.md
#	README.md
#	src/test/fake-client-cli.test-utils.ts

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