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(im): decode entities in Telegram plaintext fallback

Zhao Dengfeng39db8554ab72
diff --git a/packages/lizi-im/src/telegram/__tests__/markdown.test.ts b/packages/lizi-im/src/telegram/__tests__/markdown.test.ts
index e58326d7c4..c5f2d152d4 100644
--- a/packages/lizi-im/src/telegram/__tests__/markdown.test.ts
+++ b/packages/lizi-im/src/telegram/__tests__/markdown.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
-import { markdownToTelegramHtml } from '../markdown.js';
+import { markdownToTelegramHtml, stripTelegramHtmlTags } from '../markdown.js';
describe('markdownToTelegramHtml', () => {
it('HTML 实体转义(防注入)', () => {
@@ -10,6 +10,12 @@ describe('markdownToTelegramHtml', () => {
expect(html).toContain('&');
});
+ it('400 回退纯文本时解码实体但不误删转义标签', () => {
+ const { html } = markdownToTelegramHtml('A & B < C');
+ expect(stripTelegramHtmlTags(html)).toBe('A & B < C');
+ expect(stripTelegramHtmlTags('&lt;b&gt;')).toBe('<b>');
+ });
+
it('粗体/斜体/删除线/行内 code', () => {
const { html } = markdownToTelegramHtml('**bold** *it* ~~gone~~ `x<y`');
expect(html).toContain('<b>bold</b>');
diff --git a/packages/lizi-im/src/telegram/__tests__/outboundPolicy.test.ts b/packages/lizi-im/src/telegram/__tests__/outboundPolicy.test.ts
index 352b46724b..ca5436cf60 100644
--- a/packages/lizi-im/src/telegram/__tests__/outboundPolicy.test.ts
+++ b/packages/lizi-im/src/telegram/__tests__/outboundPolicy.test.ts
@@ -72,7 +72,7 @@ describe('outboundPolicy', () => {
});
expect(calls).toEqual([
['<b>粗</b> &amp; 细', true],
- ['粗 &amp; 细', false],
+ ['粗 & 细', false],
]);
});
diff --git a/packages/lizi-im/src/telegram/markdown.ts b/packages/lizi-im/src/telegram/markdown.ts
index 42970a3c12..7bcf7f71ca 100644
--- a/packages/lizi-im/src/telegram/markdown.ts
+++ b/packages/lizi-im/src/telegram/markdown.ts
@@ -36,6 +36,13 @@ export function escapeHtml(text: string): string {
* replace 对嵌套构造(`<<b>`)剥不干净。输出走无 parse_mode 的 text 字段,
* 残余尖括号只是字面字符, 不会被再解释。
*/
+const TELEGRAM_ENTITY_TEXT: Record<string, string> = {
+ '&lt;': '<',
+ '&gt;': '>',
+ '&quot;': '"',
+ '&amp;': '&',
+};
+
export function stripTelegramHtmlTags(html: string): string {
let out = '';
let inTag = false;
@@ -48,7 +55,9 @@ export function stripTelegramHtmlTags(html: string): string {
out += ch;
}
}
- return out;
+ // 先剥标签再解码,避免把原文里的转义 <tag> 当成真标签删掉;
+ // 单次映射不会把 &amp;lt; 二次解码成 <。
+ return out.replace(/&(?:lt|gt|quot|amp);/g, (entity) => TELEGRAM_ENTITY_TEXT[entity]);
}
function splitByCodeFence(text: string): Segment[] {