feat: use Telegram native Rich Message for assistant replies

Co-authored-by: MuziIsabel <MuziIsabel@users.noreply.github.com>
This commit is contained in:
MuziIsabel
2026-08-18 18:27:32 +08:00
committed by GitHub
co-authored by MuziIsabel
parent 819010de1c
commit 93f3bec975
15 changed files with 675 additions and 0 deletions
+21
View File
@@ -51,3 +51,24 @@ test('sendMessage can include reply_markup', async () => {
inline_keyboard: [[{ text: 'A', callback_data: 'bind:1' }]],
})
})
test('sendRichMessage posts native rich markdown body', async () => {
const calls: Array<{ url: string; body: Record<string, unknown> }> = []
const fetchImpl: typeof fetch = async (input, init) => {
calls.push({ url: String(input), body: JSON.parse(String(init?.body)) })
return new Response(JSON.stringify({
ok: true,
result: { message_id: 2, date: 0, chat: { id: 1, type: 'private' }, text: '# hi' },
}), { status: 200 })
}
const client = new TelegramClient('TOK', { fetch: fetchImpl })
await client.sendRichMessage(1, '# hi\n\n- a\n- b')
assert.match(calls[0]!.url, /\/botTOK\/sendRichMessage$/)
assert.deepEqual(calls[0]!.body, {
chat_id: 1,
rich_message: {
markdown: '# hi\n\n- a\n- b',
skip_entity_detection: true,
},
})
})
+31
View File
@@ -0,0 +1,31 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import {
RICH_MESSAGE_MAX_CHARS,
splitRichMarkdown,
} from '../src/rich-format.ts'
test('splitRichMarkdown keeps small markdown as one chunk', () => {
const markdown = '# 标题\n\n- a\n- b\n\n```js\nx\n```\n'
assert.deepEqual(splitRichMarkdown(markdown), [markdown])
})
test('splitRichMarkdown splits long plain text without losing content', () => {
const markdown = 'a'.repeat(RICH_MESSAGE_MAX_CHARS + 100)
const chunks = splitRichMarkdown(markdown)
assert.ok(chunks.length > 1)
assert.equal(chunks.join(''), markdown)
})
test('splitRichMarkdown keeps long fenced code blocks balanced', () => {
const opening = '```js\n'
const closing = '\n```'
const markdown = `${opening}${'x'.repeat(RICH_MESSAGE_MAX_CHARS + 1000)}${closing}`
const chunks = splitRichMarkdown(markdown)
assert.ok(chunks.length > 1)
for (const chunk of chunks) {
assert.ok(chunk.trimStart().startsWith('```'), 'chunk should start with a fence')
assert.ok(chunk.trimEnd().endsWith('```'), 'chunk should end with a fence')
}
assert.equal(chunks.join('').replace(/```js\n/g, '').replace(/\n```/g, ''), 'x'.repeat(RICH_MESSAGE_MAX_CHARS + 1000))
})