mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 12:00:44 +00:00
fix: avoid dropping telegram fallback chunks
This commit is contained in:
@@ -1347,6 +1347,30 @@ describe("sendMessageTelegram", () => {
|
||||
expect(plainFallbackCalls.every((call) => String(call?.[1] ?? "").length > 0)).toBe(true);
|
||||
expect(res.messageId).toBe("93");
|
||||
});
|
||||
|
||||
it("cuts over to plain text when fallback text needs more chunks than html", async () => {
|
||||
const chatId = "123";
|
||||
const htmlText = `<b>${"A".repeat(5000)}</b>`;
|
||||
const plainText = "P".repeat(9000);
|
||||
const sendMessage = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ message_id: 94, chat: { id: chatId } })
|
||||
.mockResolvedValueOnce({ message_id: 95, chat: { id: chatId } })
|
||||
.mockResolvedValueOnce({ message_id: 96, chat: { id: chatId } });
|
||||
const api = { sendMessage } as unknown as { sendMessage: typeof sendMessage };
|
||||
|
||||
const res = await sendMessageTelegram(chatId, htmlText, {
|
||||
token: "tok",
|
||||
api,
|
||||
textMode: "html",
|
||||
plainText,
|
||||
});
|
||||
|
||||
expect(sendMessage).toHaveBeenCalledTimes(3);
|
||||
expect(sendMessage.mock.calls.every((call) => call[2]?.parse_mode === undefined)).toBe(true);
|
||||
expect(sendMessage.mock.calls.map((call) => String(call[1] ?? "")).join("")).toBe(plainText);
|
||||
expect(res.messageId).toBe("96");
|
||||
});
|
||||
});
|
||||
|
||||
describe("reactMessageTelegram", () => {
|
||||
|
||||
@@ -735,6 +735,7 @@ export async function sendMessageTelegram(
|
||||
rawText: string,
|
||||
context: string,
|
||||
): Promise<{ messageId: string; chatId: string }> => {
|
||||
const fallbackText = opts.plainText ?? rawText;
|
||||
let htmlChunks: string[];
|
||||
try {
|
||||
htmlChunks = splitTelegramHtmlChunks(rawText, 4000);
|
||||
@@ -744,13 +745,16 @@ export async function sendMessageTelegram(
|
||||
error,
|
||||
)}`,
|
||||
);
|
||||
return await sendPlainChunkedText(opts.plainText ?? rawText, context);
|
||||
return await sendPlainChunkedText(fallbackText, context);
|
||||
}
|
||||
const plainTextChunks = splitTelegramPlainTextFallback(
|
||||
opts.plainText ?? rawText,
|
||||
htmlChunks.length,
|
||||
4000,
|
||||
);
|
||||
const fixedPlainTextChunks = splitTelegramPlainTextChunks(fallbackText, 4000);
|
||||
if (fixedPlainTextChunks.length > htmlChunks.length) {
|
||||
logVerbose(
|
||||
`telegram ${context} plain-text fallback needs more chunks than HTML; sending plain text`,
|
||||
);
|
||||
return await sendPlainChunkedText(fallbackText, context);
|
||||
}
|
||||
const plainTextChunks = splitTelegramPlainTextFallback(fallbackText, htmlChunks.length, 4000);
|
||||
const chunks = htmlChunks.map((chunk, index) => ({
|
||||
rawText: chunk,
|
||||
htmlText: chunk,
|
||||
|
||||
Reference in New Issue
Block a user