@@ -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} ;
0 commit comments