Skip to content

Commit e5beb87

Browse files
committed
fix tg messages.
1 parent 13f839c commit e5beb87

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

packages/client-telegram/src/messageManager.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import { stringToUuid } from "@elizaos/core";
1717
import { generateMessageResponse, generateShouldRespond } from "@elizaos/core";
1818
import { telegramMessageHandlerTemplate, telegramShouldRespondTemplate, telegramAutoPostTemplate, telegramPinnedMessageTemplate } from "./templates";
19-
import { cosineSimilarity } from "./utils";
19+
import { cosineSimilarity, escapeMarkdown } from "./utils";
2020
import {
2121
MESSAGE_CONSTANTS,
2222
TIMING_CONSTANTS,
@@ -805,7 +805,7 @@ export class MessageManager {
805805
const sentMessages: Message.TextMessage[] = [];
806806

807807
for (let i = 0; i < chunks.length; i++) {
808-
const chunk = chunks[i];
808+
const chunk = escapeMarkdown(chunks[i]);
809809
const sentMessage = (await ctx.telegram.sendMessage(
810810
ctx.chat.id,
811811
chunk,

packages/client-telegram/src/utils.ts

+23
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,29 @@ export function cosineSimilarity(text1: string, text2: string, text3?: string):
7575
return dotProduct / maxMagnitude;
7676
}
7777

78+
export function escapeMarkdown(text: string): string {
79+
// Don't escape if it's a code block
80+
if (text.startsWith('```') && text.endsWith('```')) {
81+
return text;
82+
}
83+
84+
// Split the text by code blocks
85+
const parts = text.split(/(```[\s\S]*?```)/g);
86+
87+
return parts.map((part, index) => {
88+
// If it's a code block (odd indices in the split result will be code blocks)
89+
if (index % 2 === 1) {
90+
return part;
91+
}
92+
// For regular text, only escape characters that need escaping in Markdown
93+
return part
94+
// First preserve any intended inline code spans
95+
.replace(/`.*?`/g, match => match)
96+
// Then only escape the minimal set of special characters that need escaping in Markdown mode
97+
.replace(/([*_`\\])/g, '\\$1');
98+
}).join('');
99+
}
100+
78101
/**
79102
* Splits a message into chunks that fit within Telegram's message length limit
80103
*/

packages/plugin-bootstrap/src/actions/continue.ts

+3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ export const continueAction: Action = {
110110

111111
// Check for immediate double response (responding twice in a row to the same message)
112112
const lastAgentMessage = agentMessages[0];
113+
console.log(`[CONTINUE] Last agent message inReplyTo: ${lastAgentMessage?.content?.inReplyTo}`);
114+
console.log(`[CONTINUE] Current message id: ${message.id}`);
113115
if (lastAgentMessage?.content?.inReplyTo === message.id) {
116+
console.log('[CONTINUE] Matched inReplyTo with current message');
114117
// If our last message was already a response to this message, only allow continue if:
115118
// 1. The last message had a CONTINUE action
116119
// 2. We haven't hit the maxContinuesInARow limit

0 commit comments

Comments
 (0)