Skip to content

Commit c83a53c

Browse files
authored
fix(telegram): complete live-location observer path
1 parent f85e1e8 commit c83a53c

3 files changed

Lines changed: 73 additions & 12 deletions

File tree

‎extensions/telegram/src/bot-handlers.inbound-pipeline.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function createTelegramInboundHandlers(
129129
msg: Message;
130130
requireConfiguredGroup: boolean;
131131
botUserId: number;
132-
providerUpdate?: { id: number; kind: "edited_message" };
132+
providerUpdate?: { id: number; kind: "edited_message" | "edited_channel_post" };
133133
}) => {
134134
if (shouldSkipUpdate(params.ctxForDedupe)) {
135135
return;
@@ -160,7 +160,7 @@ function createTelegramInboundHandlers(
160160
}
161161
await recordMessageForReplyChain(normalizedMsg, gate.context.threadSpec, params.botUserId);
162162
if (params.providerUpdate) {
163-
await emitTelegramLiveLocationMessageHook({
163+
emitTelegramLiveLocationMessageHook({
164164
accountId,
165165
msg: normalizedMsg,
166166
updateId: params.providerUpdate.id,
@@ -379,6 +379,10 @@ function createTelegramInboundHandlers(
379379
msg: normalizeChannelPostMessage(post),
380380
requireConfiguredGroup: true,
381381
botUserId: resolveBotUserId(ctx),
382+
providerUpdate:
383+
typeof ctx.update?.update_id === "number"
384+
? { id: ctx.update.update_id, kind: "edited_channel_post" }
385+
: undefined,
382386
});
383387
return { kind: "recorded" };
384388
};

‎extensions/telegram/src/location-message-hook.test.ts‎

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ describe("Telegram location message hooks", () => {
1818
beforeEach(() => {
1919
hookRunner.hasHooks.mockClear();
2020
hookRunner.runInboundClaim.mockClear();
21-
hookRunner.runMessageReceived.mockClear();
21+
hookRunner.runMessageReceived.mockReset();
22+
hookRunner.runMessageReceived.mockResolvedValue(undefined);
2223
});
2324

24-
it("ignores non-location edits", async () => {
25+
it("ignores non-location edits", () => {
2526
const msg = {
2627
chat: { id: 1234, type: "private" },
2728
message_id: 456,
@@ -31,7 +32,7 @@ describe("Telegram location message hooks", () => {
3132
text: "edited text",
3233
} as Message;
3334

34-
await emitTelegramLiveLocationMessageHook({
35+
emitTelegramLiveLocationMessageHook({
3536
accountId: "main",
3637
msg,
3738
updateId: 9002,
@@ -42,7 +43,7 @@ describe("Telegram location message hooks", () => {
4243
expect(hookRunner.runMessageReceived).not.toHaveBeenCalled();
4344
});
4445

45-
it("emits edited live locations through the global message-received contract", async () => {
46+
it("emits edited live locations through the global message-received contract", () => {
4647
const msg = {
4748
chat: { id: 1234, type: "private" },
4849
message_id: 456,
@@ -57,7 +58,7 @@ describe("Telegram location message hooks", () => {
5758
},
5859
} as Message;
5960

60-
await emitTelegramLiveLocationMessageHook({
61+
emitTelegramLiveLocationMessageHook({
6162
accountId: "main",
6263
msg,
6364
updateId: 9002,
@@ -98,7 +99,7 @@ describe("Telegram location message hooks", () => {
9899
expect(hookRunner.runInboundClaim).not.toHaveBeenCalled();
99100
});
100101

101-
it("emits the terminal edit when live-location sharing stops", async () => {
102+
it("emits the terminal edit when live-location sharing stops", () => {
102103
const msg = {
103104
chat: { id: 1234, type: "private" },
104105
message_id: 456,
@@ -108,7 +109,7 @@ describe("Telegram location message hooks", () => {
108109
location: { latitude: 43.8376, longitude: 18.4534 },
109110
} as Message;
110111

111-
await emitTelegramLiveLocationMessageHook({
112+
emitTelegramLiveLocationMessageHook({
112113
accountId: "main",
113114
msg,
114115
updateId: 9003,
@@ -125,4 +126,56 @@ describe("Telegram location message hooks", () => {
125126
);
126127
expect(hookRunner.runInboundClaim).not.toHaveBeenCalled();
127128
});
129+
130+
it("emits edited channel-post live locations with their provider update kind", () => {
131+
const msg = {
132+
chat: { id: -1001234, type: "supergroup", title: "Travel updates" },
133+
message_id: 456,
134+
date: 1_786_094_460,
135+
edit_date: 1_786_094_580,
136+
from: { id: 789, is_bot: true, first_name: "Travel updates" },
137+
location: { latitude: 43.8376, longitude: 18.4534, live_period: 900 },
138+
} as Message;
139+
140+
emitTelegramLiveLocationMessageHook({
141+
accountId: "main",
142+
msg,
143+
updateId: 9004,
144+
updateKind: "edited_channel_post",
145+
isForum: false,
146+
});
147+
148+
expect(hookRunner.runMessageReceived).toHaveBeenCalledWith(
149+
expect.objectContaining({
150+
providerUpdate: expect.objectContaining({
151+
id: "9004",
152+
kind: "edited_channel_post",
153+
}),
154+
}),
155+
expect.any(Object),
156+
);
157+
});
158+
159+
it("does not wait for plugin observers on the Telegram inbound path", () => {
160+
hookRunner.runMessageReceived.mockReturnValueOnce(new Promise(() => {}));
161+
const msg = {
162+
chat: { id: 1234, type: "private" },
163+
message_id: 456,
164+
date: 1_786_094_460,
165+
edit_date: 1_786_094_520,
166+
from: { id: 789, is_bot: false, first_name: "Pat" },
167+
location: { latitude: 43.8376, longitude: 18.4534, live_period: 900 },
168+
} as Message;
169+
170+
expect(
171+
emitTelegramLiveLocationMessageHook({
172+
accountId: "main",
173+
msg,
174+
updateId: 9005,
175+
updateKind: "edited_message",
176+
isForum: false,
177+
}),
178+
).toBeUndefined();
179+
expect(hookRunner.runMessageReceived).toHaveBeenCalledTimes(1);
180+
});
128181
});

‎extensions/telegram/src/location-message-hook.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Message } from "grammy/types";
33
import { formatLocationText } from "openclaw/plugin-sdk/channel-inbound";
44
import {
55
deriveInboundMessageHookContext,
6+
fireAndForgetHook,
67
toPluginMessageContext,
78
toPluginMessageReceivedEvent,
89
} from "openclaw/plugin-sdk/hook-runtime";
@@ -77,13 +78,16 @@ function buildTelegramLocationMessageHook(params: {
7778
};
7879
}
7980

80-
export async function emitTelegramLiveLocationMessageHook(
81+
export function emitTelegramLiveLocationMessageHook(
8182
params: Parameters<typeof buildTelegramLocationMessageHook>[0],
82-
) {
83+
): void {
8384
const pair = buildTelegramLocationMessageHook(params);
8485
const runner = getGlobalHookRunner();
8586
if (!pair || !runner?.hasHooks("message_received", pair.context)) {
8687
return;
8788
}
88-
await runner.runMessageReceived(pair.event, pair.context);
89+
fireAndForgetHook(
90+
runner.runMessageReceived(pair.event, pair.context),
91+
"message_received plugin hook failed",
92+
);
8993
}

0 commit comments

Comments
 (0)