diff --git a/src/telegram/allowed-updates.ts b/src/telegram/allowed-updates.ts index e32fefd096f..a081373e810 100644 --- a/src/telegram/allowed-updates.ts +++ b/src/telegram/allowed-updates.ts @@ -7,5 +7,8 @@ export function resolveTelegramAllowedUpdates(): ReadonlyArray { + try { + const post = ctx.channelPost; + if (!post) { + return; + } + + const chatId = post.chat.id; + + // Check group allowlist (channels use the same groups config) + const groupAllowlist = resolveGroupPolicy(chatId); + if (groupAllowlist.allowlistEnabled && !groupAllowlist.allowed) { + return; + } + + const { groupConfig } = resolveTelegramGroupConfig(chatId, undefined); + if (!groupConfig || groupConfig.enabled === false) { + return; + } + + // Build a synthetic `from` field since channel posts may not have one. + // Use sender_chat (the bot/user that posted) if available. + const syntheticFrom = post.sender_chat + ? { + id: post.sender_chat.id, + is_bot: true as const, + first_name: post.sender_chat.title || "Channel", + username: post.sender_chat.username, + } + : { + id: chatId, + is_bot: true as const, + first_name: post.chat.title || "Channel", + username: post.chat.username, + }; + + const syntheticMsg: Message = { + ...post, + from: post.from ?? syntheticFrom, + chat: { + ...post.chat, + type: "supergroup" as const, + }, + } as Message; + + const syntheticCtx = Object.create(ctx, { + message: { value: syntheticMsg, writable: true, enumerable: true }, + }); + + const storeAllowFrom = await readChannelAllowFromStore( + "telegram", + process.env, + accountId, + ).catch(() => []); + + await inboundDebouncer.enqueue({ + ctx: syntheticCtx, + msg: syntheticMsg, + allMedia: [], + storeAllowFrom, + debounceKey: null, + botUsername: ctx.me?.username, + }); + } catch (err) { + runtime.error?.(danger(`channel_post handler failed: ${String(err)}`)); + } + }); };