telegram: align sent hooks with command session

This commit is contained in:
Vincent Koc
2026-03-09 09:35:21 -07:00
parent 0504fb35fe
commit 017b389549
2 changed files with 42 additions and 11 deletions

View File

@@ -595,17 +595,6 @@ export const registerTelegramNativeCommands = ({
return;
}
const { threadSpec, route, mediaLocalRoots, tableMode, chunkMode } = runtimeContext;
const deliveryBaseOptions = buildCommandDeliveryBaseOptions({
chatId,
accountId: route.accountId,
sessionKeyForInternalHooks: route.sessionKey,
mirrorIsGroup: isGroup,
mirrorGroupId: isGroup ? String(chatId) : undefined,
mediaLocalRoots,
threadSpec,
tableMode,
chunkMode,
});
const threadParams = buildTelegramThreadParams(threadSpec) ?? {};
const commandDefinition = findCommandByNativeName(command.name, "telegram");
@@ -680,6 +669,17 @@ export const registerTelegramNativeCommands = ({
userId: String(senderId || chatId),
targetSessionKey: sessionKey,
});
const deliveryBaseOptions = buildCommandDeliveryBaseOptions({
chatId,
accountId: route.accountId,
sessionKeyForInternalHooks: commandSessionKey,
mirrorIsGroup: isGroup,
mirrorGroupId: isGroup ? String(chatId) : undefined,
mediaLocalRoots,
threadSpec,
tableMode,
chunkMode,
});
const conversationLabel = isGroup
? msg.chat.title
? `${msg.chat.title} id:${chatId}`

View File

@@ -258,6 +258,37 @@ describe("deliverReplies", () => {
expect(triggerInternalHook).not.toHaveBeenCalled();
});
it("emits internal message:sent with success=false on delivery failure", async () => {
const runtime = createRuntime(false);
const sendMessage = vi.fn().mockRejectedValue(new Error("network error"));
const bot = createBot({ sendMessage });
await expect(
deliverWith({
sessionKeyForInternalHooks: "agent:test:telegram:123",
replies: [{ text: "hello" }],
runtime,
bot,
}),
).rejects.toThrow("network error");
expect(triggerInternalHook).toHaveBeenCalledWith(
expect.objectContaining({
type: "message",
action: "sent",
sessionKey: "agent:test:telegram:123",
context: expect.objectContaining({
to: "123",
content: "hello",
success: false,
error: "network error",
channelId: "telegram",
conversationId: "123",
}),
}),
);
});
it("passes media metadata to message_sending hooks", async () => {
messageHookRunner.hasHooks.mockImplementation((name: string) => name === "message_sending");