Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(telegram): 按官方结构保留富文本行列和可见字段

Dashdead7f65828b
diff --git a/packages/lizi-im/src/telegram/__tests__/inbound.test.ts b/packages/lizi-im/src/telegram/__tests__/inbound.test.ts
index d08f54623b..f7aef6081e 100644
--- a/packages/lizi-im/src/telegram/__tests__/inbound.test.ts
+++ b/packages/lizi-im/src/telegram/__tests__/inbound.test.ts
@@ -165,7 +165,7 @@ describe('detectGroupTrigger', () => {
describe('replyContextOf', () => {
// Regression: rich bot replies have no text/caption. Exercise the production
// reply/window projections; prior tests only used ordinary messages (unit).
- it('preserves rich reply paragraphs, links and tables in references and history', () => {
+ it('preserves canonical rich blocks in references and history', () => {
const rich = msg({
text: undefined,
rich_message: {
@@ -180,14 +180,40 @@ describe('replyContextOf', () => {
},
{
type: 'table',
- rows: [{ cells: [{ text: '城市' }, { text: '税率' }] }],
+ cells: [
+ [
+ { text: '城市', align: 'left', valign: 'top' },
+ { text: '税率', align: 'left', valign: 'top' },
+ ],
+ [
+ { align: 'left', valign: 'top' },
+ { text: '7%', align: 'left', valign: 'top' },
+ ],
+ ],
+ caption: '表格说明',
+ },
+ {
+ type: 'details',
+ summary: ['折叠', { type: 'bold', text: '标题' }],
+ blocks: [{ type: 'paragraph', text: '详情' }],
+ },
+ { type: 'list', items: [{ label: '1.', blocks: [{ type: 'paragraph', text: '注释' }] }] },
+ {
+ type: 'photo',
+ caption: { text: '图注', credit: '作者' },
+ photo: [{ file_id: 'not-visible' }],
+ },
+ { type: 'pullquote', text: '引文', credit: '出处' },
+ { type: 'buttons', buttons: [{ text: '按钮', callback_data: 'not-visible' }] },
+ {
+ type: 'paragraph',
+ text: { type: 'button', button: { text: '行内按钮', callback_data: 'not-visible' } },
},
- { type: 'list', items: [{ blocks: [{ type: 'paragraph', text: '注释' }] }] },
- { type: 'photo', caption: '图注', file_id: 'not-visible' },
],
},
});
- const expected = '示例城市税率 7%\n来源 (https://example.com/tax)\n城市\t税率\n注释\n图注';
+ const expected =
+ '示例城市税率 7%\n来源 (https://example.com/tax)\n城市\t税率\n\t7%\n表格说明\n折叠标题\n详情\n1. 注释\n图注\n作者\n引文\n出处\n按钮\n行内按钮';
expect(replyContextOf(msg({ reply_to_message: rich }))?.text).toBe(expected);
expect(groupWindowEntryOf(rich).text).toBe(expected);
});
@@ -212,6 +238,11 @@ describe('replyContextOf', () => {
for (let i = 0; i < 100; i++) nested = { type: 'bold', text: nested };
rich.rich_message = { blocks: [{ type: 'paragraph', text: nested }] };
expect(groupWindowEntryOf(rich).text).toBe('');
+ rich.rich_message = { blocks: [
+ ...Array(3000).fill({ type: 'unknown', metadata: 'not-visible' }),
+ { type: 'paragraph', text: 'past budget' },
+ ] };
+ expect(groupWindowEntryOf(rich).text).toBe('');
rich.rich_message = {
blocks: [{ type: 'paragraph', text: 'x'.repeat(100_000) }],
};
diff --git a/packages/lizi-im/src/telegram/messageText.ts b/packages/lizi-im/src/telegram/messageText.ts
index 8ecc42dc10..3163547d65 100644
--- a/packages/lizi-im/src/telegram/messageText.ts
+++ b/packages/lizi-im/src/telegram/messageText.ts
@@ -15,7 +15,7 @@ export function telegramMessageText(message: TelegramTextContent): string {
if (message.caption?.trim()) return message.caption;
let remaining = MAX_TEXT;
let nodes = 2048;
- function visit(value: unknown, depth = 0, separator = ''): string {
+ function visit(value: unknown, depth = 0, separator = '', childSeparator = ''): string {
if (depth > 20 || --nodes < 0 || remaining <= 0) return '';
if (typeof value === 'string') {
const text = value.slice(0, remaining);
@@ -26,8 +26,9 @@ export function telegramMessageText(message: TelegramTextContent): string {
const parts: string[] = [];
for (const item of value) {
if (nodes <= 0 || remaining <= 0) break;
- const text = visit(item, depth + 1);
- if (text) parts.push(text);
+ const text = visit(item, depth + 1, childSeparator);
+ // Empty cells still occupy a column in a table row.
+ if (text || separator === '\t') parts.push(text);
}
return parts.join(separator);
}
@@ -38,13 +39,26 @@ export function telegramMessageText(message: TelegramTextContent): string {
const url = visit(item.url, depth + 1);
return url && url !== label ? `${label} (${url})` : label || url;
}
- if (item.text !== undefined) return visit(item.text, depth + 1);
if (item.type === 'mathematical_expression') return visit(item.expression, depth + 1);
if (item.type === 'custom_emoji') return visit(item.alternative_text, depth + 1);
- // Nested blocks, lists, tables and media captions. Unknown fields are ignored.
+ if (item.type === 'button') return visit(item.button, depth + 1);
+ // https://core.telegram.org/bots/api#richblocktable: cells is a row matrix.
+ if (item.type === 'table') {
+ return [visit(item.cells, depth + 1, '\n', '\t'), visit(item.caption, depth + 1)]
+ .filter(Boolean)
+ .join('\n');
+ }
+ if (typeof item.label === 'string' && Array.isArray(item.blocks)) {
+ return [visit(item.label, depth + 1), visit(item.blocks, depth + 1, '\n')]
+ .filter(Boolean)
+ .join(' ');
+ }
+ // RichText stays inline; block collections, captions and credits retain
+ // their boundaries. Do not return early on text: captions/quotes have credit.
const parts: string[] = [];
- for (const key of ['blocks', 'items', 'rows', 'cells', 'caption', 'credit']) {
- const text = visit(item[key], depth + 1, key === 'cells' ? '\t' : '\n');
+ for (const key of ['text', 'summary', 'blocks', 'items', 'caption', 'credit', 'buttons']) {
+ const separator = key === 'blocks' || key === 'items' || key === 'buttons' ? '\n' : '';
+ const text = visit(item[key], depth + 1, separator);
if (text) parts.push(text);
}
return parts.join('\n');