diff --git a/src/telegram/bot-native-commands.ts b/src/telegram/bot-native-commands.ts index c57e1097485..17958daa289 100644 --- a/src/telegram/bot-native-commands.ts +++ b/src/telegram/bot-native-commands.ts @@ -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}` diff --git a/src/telegram/bot/delivery.test.ts b/src/telegram/bot/delivery.test.ts index b8eb6274409..c21e55ccf6c 100644 --- a/src/telegram/bot/delivery.test.ts +++ b/src/telegram/bot/delivery.test.ts @@ -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");