Skip to content

Commit f79117e

Browse files
committed
fix(telegram): cache edited messages
1 parent ce5df33 commit f79117e

3 files changed

Lines changed: 79 additions & 24 deletions

File tree

‎extensions/telegram/src/bot-handlers.runtime.ts‎

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,6 +2201,55 @@ export const registerTelegramHandlers = ({
22012201
errorMessage: string;
22022202
};
22032203

2204+
const normalizeChannelPostMessage = (post: Message): Message => {
2205+
const chatId = post.chat.id;
2206+
const syntheticFrom = post.sender_chat
2207+
? {
2208+
id: post.sender_chat.id,
2209+
is_bot: true as const,
2210+
first_name: post.sender_chat.title || "Channel",
2211+
username: post.sender_chat.username,
2212+
}
2213+
: {
2214+
id: chatId,
2215+
is_bot: true as const,
2216+
first_name: post.chat.title || "Channel",
2217+
username: post.chat.username,
2218+
};
2219+
return {
2220+
...post,
2221+
from: post.from ?? syntheticFrom,
2222+
chat: {
2223+
...post.chat,
2224+
type: "supergroup" as const,
2225+
},
2226+
} as Message;
2227+
};
2228+
2229+
const recordEditedMessageForReplyChain = async (
2230+
ctxForDedupe: TelegramUpdateKeyContext,
2231+
msg: Message,
2232+
) => {
2233+
if (shouldSkipUpdate(ctxForDedupe)) {
2234+
return;
2235+
}
2236+
const isGroup = msg.chat.type === "group" || msg.chat.type === "supergroup";
2237+
const isForum = await resolveTelegramForumFlag({
2238+
chatId: msg.chat.id,
2239+
chatType: msg.chat.type,
2240+
isGroup,
2241+
isForum: msg.chat.is_forum,
2242+
getChat,
2243+
});
2244+
const normalizedMsg = withResolvedTelegramForumFlag(msg, isForum);
2245+
const resolvedThreadId = resolveTelegramForumThreadId({
2246+
isForum,
2247+
messageThreadId: normalizedMsg.message_thread_id,
2248+
});
2249+
const dmThreadId = !isGroup ? normalizedMsg.message_thread_id : undefined;
2250+
recordMessageForReplyChain(normalizedMsg, resolvedThreadId ?? dmThreadId);
2251+
};
2252+
22042253
const handleInboundMessageLike = async (event: InboundTelegramEvent) => {
22052254
try {
22062255
if (shouldSkipUpdate(event.ctxForDedupe)) {
@@ -2329,6 +2378,14 @@ export const registerTelegramHandlers = ({
23292378
});
23302379
});
23312380

2381+
bot.on("edited_message", async (ctx) => {
2382+
const msg = ctx.editedMessage;
2383+
if (!msg) {
2384+
return;
2385+
}
2386+
await recordEditedMessageForReplyChain(ctx, msg);
2387+
});
2388+
23322389
// Handle channel posts — enables bot-to-bot communication via Telegram channels.
23332390
// Telegram bots cannot see other bot messages in groups, but CAN in channels.
23342391
// This handler normalizes channel_post updates into the standard message pipeline.
@@ -2339,27 +2396,7 @@ export const registerTelegramHandlers = ({
23392396
}
23402397

23412398
const chatId = post.chat.id;
2342-
const syntheticFrom = post.sender_chat
2343-
? {
2344-
id: post.sender_chat.id,
2345-
is_bot: true as const,
2346-
first_name: post.sender_chat.title || "Channel",
2347-
username: post.sender_chat.username,
2348-
}
2349-
: {
2350-
id: chatId,
2351-
is_bot: true as const,
2352-
first_name: post.chat.title || "Channel",
2353-
username: post.chat.username,
2354-
};
2355-
const syntheticMsg: Message = {
2356-
...post,
2357-
from: post.from ?? syntheticFrom,
2358-
chat: {
2359-
...post.chat,
2360-
type: "supergroup" as const,
2361-
},
2362-
} as Message;
2399+
const syntheticMsg = normalizeChannelPostMessage(post);
23632400

23642401
await handleInboundMessageLike({
23652402
ctxForDedupe: ctx,
@@ -2381,4 +2418,12 @@ export const registerTelegramHandlers = ({
23812418
errorMessage: "channel_post handler failed",
23822419
});
23832420
});
2421+
2422+
bot.on("edited_channel_post", async (ctx) => {
2423+
const post = ctx.editedChannelPost;
2424+
if (!post) {
2425+
return;
2426+
}
2427+
await recordEditedMessageForReplyChain(ctx, normalizeChannelPostMessage(post));
2428+
});
23842429
};

‎extensions/telegram/src/bot-updates.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type TelegramUpdateKeyContext = {
2525
update_id?: number;
2626
message?: Message;
2727
channelPost?: Message;
28+
editedMessage?: Message;
2829
editedChannelPost?: Message;
2930
callbackQuery?: { id?: string; message?: Message };
3031
};
@@ -41,14 +42,21 @@ export const buildTelegramUpdateKey = (ctx: TelegramUpdateKeyContext) => {
4142
if (callbackId) {
4243
return `callback:${callbackId}`;
4344
}
45+
const editedMsg =
46+
ctx.editedMessage ??
47+
ctx.editedChannelPost ??
48+
ctx.update?.edited_message ??
49+
ctx.update?.edited_channel_post;
50+
const editedChatId = editedMsg?.chat?.id;
51+
const editedMessageId = editedMsg?.message_id;
52+
if (editedChatId !== undefined && typeof editedMessageId === "number") {
53+
return `edited-message:${editedChatId}:${editedMessageId}`;
54+
}
4455
const msg =
4556
ctx.message ??
4657
ctx.channelPost ??
47-
ctx.editedChannelPost ??
4858
ctx.update?.message ??
49-
ctx.update?.edited_message ??
5059
ctx.update?.channel_post ??
51-
ctx.update?.edited_channel_post ??
5260
ctx.callbackQuery?.message;
5361
const chatId = msg?.chat?.id;
5462
const messageId = msg?.message_id;

‎extensions/telegram/src/sequential-key.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type TelegramSequentialKeyContext = {
1616
me?: UserFromGetMe;
1717
message?: Message;
1818
channelPost?: Message;
19+
editedMessage?: Message;
1920
editedChannelPost?: Message;
2021
update?: {
2122
message?: Message;
@@ -56,6 +57,7 @@ export function getTelegramSequentialKey(ctx: TelegramSequentialKeyContext): str
5657
const msg =
5758
ctx.message ??
5859
ctx.channelPost ??
60+
ctx.editedMessage ??
5961
ctx.editedChannelPost ??
6062
ctx.update?.message ??
6163
ctx.update?.edited_message ??

0 commit comments

Comments
 (0)