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): unify non-text rich block placeholders

Dashe17712364553
diff --git a/docs/product-rules/telegram-bot-parity.md b/docs/product-rules/telegram-bot-parity.md
index b243a3c000..f7d60b4c60 100644
--- a/docs/product-rules/telegram-bot-parity.md
+++ b/docs/product-rules/telegram-bot-parity.md
@@ -20,8 +20,9 @@ Cindy 有两个 Telegram bot,用户看到的是同一个产品:
ID 分开保存。官方在入队前组装引用,沿用既有任务持久化恢复;普通群窗口中继仍只送在线
桌面。两仓分别维护投影与回归测试,客户端升级不能代替官方服务端部署。
-个人 bot 的无说明富媒体块补充类型占位(文件、图片、音频、视频、动画、语音)及可见
-文件名,嵌套块与引用、群历史共用判据;这些占位仅描述消息内容,不代表附件已提供。
+个人 bot 的无说明非文字块统一投影类型占位(文件、图片、音频、视频、动画、语音、地图、
+分隔线);仅文件类媒体允许附加可见文件名,地图不输出坐标等字段。嵌套块与引用、群历史
+共用判据;这些占位仅描述消息内容,不代表附件已提供。
嵌套富媒体的字节下载尚未接入,附件仍沿用现有顶层路径。官方同名文字投影需独立
同步这部分占位,不能因客户端支持就声称官方服务端也已支持。
diff --git a/packages/lizi-im/src/telegram/__tests__/inbound.test.ts b/packages/lizi-im/src/telegram/__tests__/inbound.test.ts
index d2e3a05638..43209643d2 100644
--- a/packages/lizi-im/src/telegram/__tests__/inbound.test.ts
+++ b/packages/lizi-im/src/telegram/__tests__/inbound.test.ts
@@ -257,7 +257,7 @@ describe('replyContextOf', () => {
// Media-only blocks used to disappear even though their type/name is visible.
// Exercise the same unit entry points as text, including nested containers;
// these markers describe references and do not assert attachment delivery.
- it('keeps rich media labels and filenames in references and nested history', () => {
+ it('keeps non-text block labels and safe filenames in references and nested history', () => {
const rich = msg({
text: undefined,
rich_message: {
@@ -273,6 +273,18 @@ describe('replyContextOf', () => {
expect(replyContextOf(reply)).toMatchObject({ messageId: '1', text: '[文件: report.pdf]' });
expect(groupWindowEntryOf(rich).text).toBe('[文件: report.pdf]');
+ const map = {
+ type: 'map',
+ location: { latitude: 12.5, longitude: 45.25 },
+ zoom: 8,
+ width: 640,
+ height: 360,
+ map: { file_name: 'hidden-map-name' },
+ };
+ rich.rich_message = { blocks: [map] };
+ expect(replyContextOf(reply)).toMatchObject({ messageId: '1', text: '[地图]' });
+ expect(groupWindowEntryOf(rich).text).toBe('[地图]');
+
rich.rich_message = {
blocks: [
{
@@ -302,13 +314,15 @@ describe('replyContextOf', () => {
},
{ type: 'voice_note', voice_note: { file_id: 'hidden-id' } },
{ type: 'document', document: { file_id: 'hidden-id' } },
+ map,
+ { type: 'divider' },
{ type: 'unknown', document: { file_name: 'hidden-name' } },
],
},
],
};
const expected =
- '附件\n[图片]\n[音频: song.mp3]\n[视频: clip.mp4]\n[动画: loop.gif]\n[语音消息]\n[文件]';
+ '附件\n[图片]\n[音频: song.mp3]\n[视频: clip.mp4]\n[动画: loop.gif]\n[语音消息]\n[文件]\n[地图]\n[分隔线]';
expect(replyContextOf(reply)?.text).toBe(expected);
expect(groupWindowEntryOf(rich).text).toBe(expected);
rich.has_protected_content = true;
diff --git a/packages/lizi-im/src/telegram/messageText.ts b/packages/lizi-im/src/telegram/messageText.ts
index ab93dae45f..56ae6ac100 100644
--- a/packages/lizi-im/src/telegram/messageText.ts
+++ b/packages/lizi-im/src/telegram/messageText.ts
@@ -1,6 +1,6 @@
/** Bot API incoming RichMessage uses blocks, unlike outgoing markdown/html.
* Keep visible text aligned with telegram-hook-server/telegram/messageText.ts;
- * media-only labels are a personal-bot extension, not attachment delivery.
+ * non-text labels are a personal-bot extension, not attachment delivery.
* Only visible content is read; media IDs, callback data and metadata stay out.
*/
export interface TelegramTextContent {
@@ -16,13 +16,17 @@ const MAX_COLUMNS = 20;
// of their own; budget work independently of the visible output length.
const MAX_DEPTH = 64;
const MAX_NODES = 32_768 * MAX_DEPTH;
-const RICH_MEDIA_LABELS = new Map([
- ['document', '文件'],
- ['photo', '图片'],
- ['audio', '音频'],
- ['video', '视频'],
- ['animation', '动画'],
- ['voice_note', '语音消息'],
+// Visible non-text blocks have one fallback policy. Only file-bearing types
+// may contribute file_name; maps never expose location/zoom or other metadata.
+const NON_TEXT_BLOCKS = new Map<string, { label: string; filename?: boolean }>([
+ ['document', { label: '文件', filename: true }],
+ ['photo', { label: '图片' }],
+ ['audio', { label: '音频', filename: true }],
+ ['video', { label: '视频', filename: true }],
+ ['animation', { label: '动画', filename: true }],
+ ['voice_note', { label: '语音消息' }],
+ ['map', { label: '地图' }],
+ ['divider', { label: '分隔线' }],
]);
export function telegramMessageText(message: TelegramTextContent): string {
@@ -111,15 +115,15 @@ export function telegramMessageText(message: TelegramTextContent): string {
}
const text = parts.join('\n');
if (text.trim()) return text;
- const label = typeof item.type === 'string' ? RICH_MEDIA_LABELS.get(item.type) : undefined;
- if (label) {
+ const fallback = typeof item.type === 'string' ? NON_TEXT_BLOCKS.get(item.type) : undefined;
+ if (fallback) {
// Only the visible filename is allowed out of the media object. Never
// recurse into it: file IDs, thumbnails, MIME and other metadata stay out.
- const media = item[item.type as string];
+ const media = fallback.filename ? item[item.type as string] : undefined;
const name =
media && typeof media === 'object' && 'file_name' in media ? media.file_name : undefined;
return visit(
- `[${label}${typeof name === 'string' && name.trim() ? `: ${name}` : ''}]`,
+ `[${fallback.label}${typeof name === 'string' && name.trim() ? `: ${name}` : ''}]`,
depth + 1,
);
}