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): preserve rich media reference labels

Dash124be6bf4696
diff --git a/docs/product-rules/telegram-bot-parity.md b/docs/product-rules/telegram-bot-parity.md
index 22b7aecd12..b243a3c000 100644
--- a/docs/product-rules/telegram-bot-parity.md
+++ b/docs/product-rules/telegram-bot-parity.md
@@ -20,6 +20,11 @@ Cindy 有两个 Telegram bot,用户看到的是同一个产品:
ID 分开保存。官方在入队前组装引用,沿用既有任务持久化恢复;普通群窗口中继仍只送在线
桌面。两仓分别维护投影与回归测试,客户端升级不能代替官方服务端部署。
+个人 bot 的无说明富媒体块补充类型占位(文件、图片、音频、视频、动画、语音)及可见
+文件名,嵌套块与引用、群历史共用判据;这些占位仅描述消息内容,不代表附件已提供。
+嵌套富媒体的字节下载尚未接入,附件仍沿用现有顶层路径。官方同名文字投影需独立
+同步这部分占位,不能因客户端支持就声称官方服务端也已支持。
+
2026-10-17 群触发与空回复:默认召唤模式下,仅提到显示名(包括句首的「Cindy 赞助」)
不触发。个人 bot 保留显式 `@显示名` 的兼容入口;平台提及、回复 bot、命令及用户主动
开启的全响应模式沿用原有规则。Telegram 成功轮次没有正文和附件时不补发「空回复」或
diff --git a/packages/lizi-im/src/telegram/__tests__/inbound.test.ts b/packages/lizi-im/src/telegram/__tests__/inbound.test.ts
index b28d016bd9..d2e3a05638 100644
--- a/packages/lizi-im/src/telegram/__tests__/inbound.test.ts
+++ b/packages/lizi-im/src/telegram/__tests__/inbound.test.ts
@@ -254,6 +254,67 @@ describe('replyContextOf', () => {
expect(replyContextOf(m)?.text).toBe(expected);
});
+ // 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', () => {
+ const rich = msg({
+ text: undefined,
+ rich_message: {
+ blocks: [
+ {
+ type: 'document',
+ document: { file_name: 'report.pdf', file_id: 'hidden-id', mime_type: 'hidden-mime' },
+ },
+ ],
+ },
+ });
+ const reply = msg({ reply_to_message: rich });
+ expect(replyContextOf(reply)).toMatchObject({ messageId: '1', text: '[文件: report.pdf]' });
+ expect(groupWindowEntryOf(rich).text).toBe('[文件: report.pdf]');
+
+ rich.rich_message = {
+ blocks: [
+ {
+ type: 'details',
+ summary: '附件',
+ blocks: [
+ {
+ type: 'collage',
+ blocks: [
+ { type: 'photo', photo: [{ file_id: 'hidden-id' }], caption: { text: ' ' } },
+ {
+ type: 'audio',
+ audio: {
+ file_name: 'song.mp3',
+ file_id: 'hidden-id',
+ performer: 'hidden-performer',
+ },
+ },
+ ],
+ },
+ {
+ type: 'slideshow',
+ blocks: [
+ { type: 'video', video: { file_name: 'clip.mp4', file_id: 'hidden-id' } },
+ { type: 'animation', animation: { file_name: 'loop.gif', file_id: 'hidden-id' } },
+ ],
+ },
+ { type: 'voice_note', voice_note: { file_id: 'hidden-id' } },
+ { type: 'document', document: { file_id: 'hidden-id' } },
+ { type: 'unknown', document: { file_name: 'hidden-name' } },
+ ],
+ },
+ ],
+ };
+ const expected =
+ '附件\n[图片]\n[音频: song.mp3]\n[视频: clip.mp4]\n[动画: loop.gif]\n[语音消息]\n[文件]';
+ expect(replyContextOf(reply)?.text).toBe(expected);
+ expect(groupWindowEntryOf(rich).text).toBe(expected);
+ rich.has_protected_content = true;
+ expect(replyContextOf(reply)).toBeNull();
+ });
+
it('uses an explicit quote and still respects protection of the original', () => {
const m = msg({
quote: { text: '选中的片段' },
diff --git a/packages/lizi-im/src/telegram/messageText.ts b/packages/lizi-im/src/telegram/messageText.ts
index 0e440c1c17..ab93dae45f 100644
--- a/packages/lizi-im/src/telegram/messageText.ts
+++ b/packages/lizi-im/src/telegram/messageText.ts
@@ -1,5 +1,6 @@
/** Bot API incoming RichMessage uses blocks, unlike outgoing markdown/html.
- * Keep this projection aligned with telegram-hook-server/telegram/messageText.ts.
+ * Keep visible text aligned with telegram-hook-server/telegram/messageText.ts;
+ * media-only 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 {
@@ -15,6 +16,14 @@ 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', '语音消息'],
+]);
export function telegramMessageText(message: TelegramTextContent): string {
if (message.text?.trim()) return message.text;
@@ -100,7 +109,21 @@ export function telegramMessageText(message: TelegramTextContent): string {
const text = visit(item[key], depth + 1, separator);
if (text) parts.push(text);
}
- return parts.join('\n');
+ 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) {
+ // 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 name =
+ media && typeof media === 'object' && 'file_name' in media ? media.file_name : undefined;
+ return visit(
+ `[${label}${typeof name === 'string' && name.trim() ? `: ${name}` : ''}]`,
+ depth + 1,
+ );
+ }
+ return text;
}
const text = visit(message.rich_message?.blocks, 0, '\n').slice(0, MAX_TEXT);
return text.trim() ? text : '';