Skip to content

fix(messages): show and index the text of rich messages - #178

Merged
neoromantic merged 1 commit into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/read-rich-messages
Nov 18, 2026
Merged

neoromantic merged 1 commit into
neoromantic:mainfrom
ertembiyik:ertembiyik/NO-TASK/read-rich-messages

Conversation

@ertembiyik

Copy link
Copy Markdown
Contributor

What and why

A rich message (headings, tables, lists, a whole report) has an empty message and its content in richMessage.blocks, so tg messages list, get and search print it as text: null and search can't find it. An agent reading a chat sees an empty message where a table was sent. This renders the blocks as text, in Telegram's own rich Markdown, wherever a message is printed or indexed. It is the reading side of #175, but stands alone: it affects every rich message received, not only the ones tg sends.

Before (tg messages get --chat @gardenclub --id 2 on a made-up seeded cache):

{
  "messageId": 2,
  "text": null,
  "messageType": "text",
  "hasMedia": false,
  "attachment": null,
  "action": null
}

tg messages search --query zucchini returns 0 results.

After:

{
  "messageId": 2,
  "text": "# Garden report\n\nHarvest is **on track**\n\n| Crop | Kg |\n|---|---:|\n| Zucchini | 12 |\n\n- [x] water beds\n- [ ] order seeds",
  "messageType": "text",
  "hasMedia": false,
  "attachment": null,
  "action": null
}

The same search returns message 2. (Both outputs are real, with the unchanged fields left out.)

Where the text is produced:

  • When a message is cached. writeMessage in src/db/messages-cache.ts is the one place that inserts into messages_cache, so live updates, history, catch-up and tg send all get it. When a message has no text and its raw_json has a richMessage, the rendered text is stored, and the existing FTS triggers index it. A substring check runs before any JSON parse.
  • When an item is printed. messageItemReader reads raw_json for a text row with empty text, the way fix(messages): show what a service message did (calls, pins, group changes) #159 reads attachment and action, so rows cached by 0.13.1 print correctly at once.
  • For search on rows cached earlier: a background migration, messages_rich_text, fills their text. Until it reaches a row, that row prints but isn't found.
  • Live edits: onEditMessage uses the rendered text when mtcute's msg.text is empty, so editing a rich message doesn't blank it.

How blocks are rendered (src/utils/rich-message.ts, a pure function):

  • Telegram's rich Markdown where it has syntax: # headings, **bold**, *italic*, ~~strike~~, `code`, ==marked==, ||spoiler||, links, fenced code with its language, lists and task lists, > quotations, ---, pipe tables with the alignment row, $…$ and $$…$$, footnotes.
  • The HTML tags Telegram uses where Markdown has none: <u>, <sub>, <sup>, <details><summary>, <aside>.
  • Short placeholders for what can't be text: [photo: caption], [document plan.pdf], [map 41.9, 12.5], [button: label (url)].
  • An unknown block or text type never throws: it renders its nested text, else [typeName], so a block Telegram adds later doesn't blank the message again.

Decisions you may want to change:

  • messageType stays "text" and there is no new flag. Changing the type would break caches and scripts that filter on text. An additive isRich is easy to add if you want one.
  • The migration is optional. Reading at print time is enough for output; the migration only makes old rows searchable. It is about 25 lines and adds a row to tg status and tg db migrate. Say the word and I'll drop it.
  • Plain text isn't Markdown-escaped. A literal * in a paragraph comes out as is: better to read, not a guaranteed round trip.

Known limits: a table's first-column alignment can't be told from "no alignment" in Telegram's data; the bordered and striped flags of a table and the collapsed state of a quotation are not shown.

How I checked it

  • Unit tests in src/__tests__/rich-message.test.ts (21): every block and rich-text type the renderer maps, nesting (a list in details, bold in a table cell), unknown types, and both the object and the raw_json form, each fixture taken through toRawJson.

  • E2E in src/__e2e__/messages.e2e.test.ts: messages list (including a row cached the old way), messages get, and messages search on a word that appears only in a table cell. Migration tests cover filling empty text and leaving other rows alone.

  • Against real data, read-only and outside the repo: the renderer over four real rich messages (Markdown and HTML sources, with a table, task list, quotation, code block, details, formula and footnote) gave readable text for each.

  • Not verified: media, map, embed and button blocks against real messages (built from the TL schema), and the live edit path against a real edit.

  • bun run check: clean. bun run test: 2324 pass, 0 fail. bun run test:e2e: 435 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/database-schema.md, the text schema annotation, README, CHANGELOG)

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

🤖 Generated with Claude Code

A rich message has an empty text and its content in page blocks, so tg
messages printed it as text null and search could not find it. Its blocks
are rendered in Telegram's rich Markdown when the message is cached, and
read from raw_json for rows cached earlier; the background migration
messages_rich_text makes those rows searchable. messageType stays text.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
neoromantic added a commit that referenced this pull request Nov 18, 2026
fix(messages): show and index the text of rich messages (#178)
@neoromantic
neoromantic merged commit 301a305 into neoromantic:main Nov 18, 2026
ertembiyik added a commit to ertembiyik/telegram-sync-cli that referenced this pull request Nov 18, 2026
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.

2 participants