refactor(telegram): simplify rich email entity detection

This commit is contained in:
Ayaan Zaidi
2026-06-24 06:19:00 -07:00
parent 51eec3a757
commit 2aa9d67635
4 changed files with 30 additions and 38 deletions

View File

@@ -1246,11 +1246,11 @@ describe("deliverReplies", () => {
chat: { id: "123" },
});
const bot = createBot({ sendMessage });
const oauthProfileText =
"OAuth profile: openai:keshavbotagent@gmail.com (keshavbotagent@gmail.com)";
await deliverWith({
replies: [
{ text: "OAuth profile: openai:keshavbotagent@gmail.com (keshavbotagent@gmail.com)" },
],
replies: [{ text: oauthProfileText }],
runtime,
bot,
richMessages: true,
@@ -1261,7 +1261,7 @@ describe("deliverReplies", () => {
};
const richMessage = raw.sendRichMessage.mock.calls[0]?.[0]?.rich_message;
expect(richMessage).toEqual({
html: "OAuth profile: openai:keshavbotagent@gmail.com (keshavbotagent@gmail.com)",
html: oauthProfileText,
skip_entity_detection: true,
});
});

View File

@@ -693,14 +693,16 @@ describe("createTelegramDraftStream", () => {
it("skips rich entity detection for draft text with provider-prefixed email addresses", async () => {
const api = createMockDraftApi();
const stream = createDraftStream(api, { richMessages: true });
const oauthProfileText =
"OAuth profile: openai:keshavbotagent@gmail.com (keshavbotagent@gmail.com)";
stream.update("OAuth profile: openai:keshavbotagent@gmail.com (keshavbotagent@gmail.com)");
stream.update(oauthProfileText);
await stream.flush();
expect(api.raw.sendRichMessage).toHaveBeenCalledWith({
chat_id: 123,
rich_message: {
html: "OAuth profile: openai:keshavbotagent@gmail.com (keshavbotagent@gmail.com)",
html: oauthProfileText,
skip_entity_detection: true,
},
});

View File

@@ -174,17 +174,11 @@ export function buildTelegramRichMarkdown(
markdown: string,
options?: TelegramRichMessageOptions,
): TelegramInputRichMessage {
const skipEntityDetection = shouldSkipTelegramRichEntityDetection(markdown, options);
return buildTelegramRichHtml(
markdownToTelegramRichHtml(markdown, {
...options,
skipEntityDetection,
}),
{
...options,
skipEntityDetection,
},
);
const richOptions = {
...options,
skipEntityDetection: shouldSkipTelegramRichEntityDetection(markdown, options),
};
return buildTelegramRichHtml(markdownToTelegramRichHtml(markdown, richOptions), richOptions);
}
export function buildTelegramRichHtml(
@@ -438,16 +432,14 @@ export function splitTelegramRichMessageTextChunks(params: {
tableMode?: MarkdownTableMode;
skipEntityDetection?: boolean;
}): TelegramRichTextChunk[] {
const skipEntityDetection = shouldSkipTelegramRichEntityDetection(params.text, {
skipEntityDetection: params.skipEntityDetection,
});
const markdownOptions = {
tableMode: params.tableMode,
skipEntityDetection: shouldSkipTelegramRichEntityDetection(params.text, {
skipEntityDetection: params.skipEntityDetection,
}),
};
const renderMarkdownChunk = (chunk: string) =>
prepareTelegramRichHtml(
markdownToTelegramRichHtml(chunk, {
tableMode: params.tableMode,
skipEntityDetection,
}),
);
prepareTelegramRichHtml(markdownToTelegramRichHtml(chunk, markdownOptions));
const htmlChunks =
params.textMode === "html"
? splitPreparedTelegramRichHtml({

View File

@@ -958,26 +958,24 @@ describe("sendMessageTelegram", () => {
it("skips rich entity detection for provider-prefixed email text", async () => {
botApi.sendMessage.mockResolvedValue({ message_id: 45, chat: { id: "123" } });
const oauthProfileText =
"OAuth profile: openai:keshavbotagent@gmail.com (keshavbotagent@gmail.com)";
await sendMessageTelegram(
"123",
"OAuth profile: openai:keshavbotagent@gmail.com (keshavbotagent@gmail.com)",
{
cfg: {
channels: {
telegram: {
richMessages: true,
},
await sendMessageTelegram("123", oauthProfileText, {
cfg: {
channels: {
telegram: {
richMessages: true,
},
},
token: "tok",
},
);
token: "tok",
});
expect(botRawApi.sendRichMessage).toHaveBeenCalledTimes(1);
const richMessage = botRawApi.sendRichMessage.mock.calls[0]?.[0]?.rich_message;
expect(richMessage).toEqual({
html: "OAuth profile: openai:keshavbotagent@gmail.com (keshavbotagent@gmail.com)",
html: oauthProfileText,
skip_entity_detection: true,
});
expect(richMessage?.html).not.toContain("mailto:");