fix: avoid threaded replies for agent output
This commit is contained in:
@@ -148,7 +148,7 @@ describe("createTelegramBot", () => {
|
|||||||
expect(payload.ReplyToSender).toBe("Ada");
|
expect(payload.ReplyToSender).toBe("Ada");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sends replies as native replies without chaining", async () => {
|
it("sends replies without native reply threading", async () => {
|
||||||
onSpy.mockReset();
|
onSpy.mockReset();
|
||||||
sendMessageSpy.mockReset();
|
sendMessageSpy.mockReset();
|
||||||
const replySpy = replyModule.__replySpy as unknown as ReturnType<
|
const replySpy = replyModule.__replySpy as unknown as ReturnType<
|
||||||
@@ -174,7 +174,7 @@ describe("createTelegramBot", () => {
|
|||||||
|
|
||||||
expect(sendMessageSpy.mock.calls.length).toBeGreaterThan(1);
|
expect(sendMessageSpy.mock.calls.length).toBeGreaterThan(1);
|
||||||
for (const call of sendMessageSpy.mock.calls) {
|
for (const call of sendMessageSpy.mock.calls) {
|
||||||
expect(call[2]?.reply_to_message_id).toBe(101);
|
expect(call[2]?.reply_to_message_id).toBeUndefined();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -199,7 +199,6 @@ export function createTelegramBot(opts: TelegramBotOptions) {
|
|||||||
token: opts.token,
|
token: opts.token,
|
||||||
runtime,
|
runtime,
|
||||||
bot,
|
bot,
|
||||||
replyToMessageId: msg.message_id,
|
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
runtime.error?.(danger(`Telegram handler failed: ${String(err)}`));
|
runtime.error?.(danger(`Telegram handler failed: ${String(err)}`));
|
||||||
@@ -222,10 +221,8 @@ async function deliverReplies(params: {
|
|||||||
token: string;
|
token: string;
|
||||||
runtime: RuntimeEnv;
|
runtime: RuntimeEnv;
|
||||||
bot: Bot;
|
bot: Bot;
|
||||||
replyToMessageId?: number;
|
|
||||||
}) {
|
}) {
|
||||||
const { replies, chatId, runtime, bot } = params;
|
const { replies, chatId, runtime, bot } = params;
|
||||||
const replyTarget = params.replyToMessageId;
|
|
||||||
for (const reply of replies) {
|
for (const reply of replies) {
|
||||||
if (!reply?.text && !reply?.mediaUrl && !(reply?.mediaUrls?.length ?? 0)) {
|
if (!reply?.text && !reply?.mediaUrl && !(reply?.mediaUrls?.length ?? 0)) {
|
||||||
runtime.error?.(danger("Telegram reply missing text/media"));
|
runtime.error?.(danger("Telegram reply missing text/media"));
|
||||||
@@ -236,14 +233,9 @@ async function deliverReplies(params: {
|
|||||||
: reply.mediaUrl
|
: reply.mediaUrl
|
||||||
? [reply.mediaUrl]
|
? [reply.mediaUrl]
|
||||||
: [];
|
: [];
|
||||||
if (replyTarget && isVerbose()) {
|
|
||||||
logVerbose(
|
|
||||||
`telegram reply-send: chatId=${chatId} replyToMessageId=${replyTarget} kind=${mediaList.length ? "media" : "text"}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
if (mediaList.length === 0) {
|
if (mediaList.length === 0) {
|
||||||
for (const chunk of chunkText(reply.text || "", 4000)) {
|
for (const chunk of chunkText(reply.text || "", 4000)) {
|
||||||
await sendTelegramText(bot, chatId, chunk, runtime, replyTarget);
|
await sendTelegramText(bot, chatId, chunk, runtime);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -255,18 +247,14 @@ async function deliverReplies(params: {
|
|||||||
const file = new InputFile(media.buffer, media.fileName ?? "file");
|
const file = new InputFile(media.buffer, media.fileName ?? "file");
|
||||||
const caption = first ? (reply.text ?? undefined) : undefined;
|
const caption = first ? (reply.text ?? undefined) : undefined;
|
||||||
first = false;
|
first = false;
|
||||||
const replyOpts = replyTarget ? { reply_to_message_id: replyTarget } : {};
|
|
||||||
if (kind === "image") {
|
if (kind === "image") {
|
||||||
await bot.api.sendPhoto(chatId, file, { caption, ...replyOpts });
|
await bot.api.sendPhoto(chatId, file, { caption });
|
||||||
} else if (kind === "video") {
|
} else if (kind === "video") {
|
||||||
await bot.api.sendVideo(chatId, file, { caption, ...replyOpts });
|
await bot.api.sendVideo(chatId, file, { caption });
|
||||||
} else if (kind === "audio") {
|
} else if (kind === "audio") {
|
||||||
await bot.api.sendAudio(chatId, file, { caption, ...replyOpts });
|
await bot.api.sendAudio(chatId, file, { caption });
|
||||||
} else {
|
} else {
|
||||||
await bot.api.sendDocument(chatId, file, {
|
await bot.api.sendDocument(chatId, file, { caption });
|
||||||
caption,
|
|
||||||
...replyOpts,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -363,12 +351,10 @@ async function sendTelegramText(
|
|||||||
chatId: string,
|
chatId: string,
|
||||||
text: string,
|
text: string,
|
||||||
runtime: RuntimeEnv,
|
runtime: RuntimeEnv,
|
||||||
replyToMessageId?: number,
|
|
||||||
): Promise<number | undefined> {
|
): Promise<number | undefined> {
|
||||||
try {
|
try {
|
||||||
const res = await bot.api.sendMessage(chatId, text, {
|
const res = await bot.api.sendMessage(chatId, text, {
|
||||||
parse_mode: "Markdown",
|
parse_mode: "Markdown",
|
||||||
reply_to_message_id: replyToMessageId,
|
|
||||||
});
|
});
|
||||||
return res.message_id;
|
return res.message_id;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -378,7 +364,6 @@ async function sendTelegramText(
|
|||||||
`telegram markdown parse failed; retrying without formatting: ${errText}`,
|
`telegram markdown parse failed; retrying without formatting: ${errText}`,
|
||||||
);
|
);
|
||||||
const res = await bot.api.sendMessage(chatId, text, {
|
const res = await bot.api.sendMessage(chatId, text, {
|
||||||
reply_to_message_id: replyToMessageId,
|
|
||||||
});
|
});
|
||||||
return res.message_id;
|
return res.message_id;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -223,10 +223,10 @@ export async function monitorWebInbox(options: {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
const reply = async (text: string) => {
|
const reply = async (text: string) => {
|
||||||
await sock.sendMessage(chatJid, { text }, { quoted: msg });
|
await sock.sendMessage(chatJid, { text });
|
||||||
};
|
};
|
||||||
const sendMedia = async (payload: AnyMessageContent) => {
|
const sendMedia = async (payload: AnyMessageContent) => {
|
||||||
await sock.sendMessage(chatJid, payload, { quoted: msg });
|
await sock.sendMessage(chatJid, payload);
|
||||||
};
|
};
|
||||||
const timestamp = msg.messageTimestamp
|
const timestamp = msg.messageTimestamp
|
||||||
? Number(msg.messageTimestamp) * 1000
|
? Number(msg.messageTimestamp) * 1000
|
||||||
|
|||||||
@@ -112,11 +112,6 @@ describe("web monitor inbox", () => {
|
|||||||
expect(sock.sendMessage).toHaveBeenCalledWith(
|
expect(sock.sendMessage).toHaveBeenCalledWith(
|
||||||
"999@s.whatsapp.net",
|
"999@s.whatsapp.net",
|
||||||
{ text: "pong" },
|
{ text: "pong" },
|
||||||
{
|
|
||||||
quoted: expect.objectContaining({
|
|
||||||
key: expect.objectContaining({ id: "abc" }),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
await listener.close();
|
await listener.close();
|
||||||
@@ -200,11 +195,6 @@ describe("web monitor inbox", () => {
|
|||||||
expect(sock.sendMessage).toHaveBeenCalledWith(
|
expect(sock.sendMessage).toHaveBeenCalledWith(
|
||||||
"999@s.whatsapp.net",
|
"999@s.whatsapp.net",
|
||||||
{ text: "pong" },
|
{ text: "pong" },
|
||||||
{
|
|
||||||
quoted: expect.objectContaining({
|
|
||||||
key: expect.objectContaining({ id: "abc" }),
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
await listener.close();
|
await listener.close();
|
||||||
|
|||||||
Reference in New Issue
Block a user