|
1 | 1 | import { setWebhook, getMyCommands } from "@/server/channels/telegram/bot-api"; |
2 | 2 | import type { TelegramBotCommand } from "@/server/channels/telegram/bot-api"; |
3 | 3 | import { getTelegramBotCommands, syncTelegramCommands } from "@/server/channels/telegram/commands"; |
4 | | -import { setTelegramChannelConfig } from "@/server/channels/state"; |
| 4 | +import { buildTelegramWebhookUrl, setTelegramChannelConfig } from "@/server/channels/state"; |
5 | 5 | import { logInfo, logWarn } from "@/server/log"; |
6 | 6 | import { getInitializedMeta, getStore } from "@/server/store/store"; |
7 | 7 |
|
8 | | -/** |
9 | | - * Strip the Vercel protection bypass query parameter from a URL. |
10 | | - * Telegram's setWebhook silently drops registrations when the URL |
11 | | - * contains this parameter. |
12 | | - */ |
13 | | -export function stripBypassParam(url: string): string { |
14 | | - try { |
15 | | - const parsed = new URL(url); |
16 | | - if (parsed.searchParams.has("x-vercel-protection-bypass")) { |
17 | | - parsed.searchParams.delete("x-vercel-protection-bypass"); |
18 | | - return parsed.toString(); |
19 | | - } |
20 | | - } catch { |
21 | | - // Not a valid URL — return as-is. |
22 | | - } |
23 | | - return url; |
24 | | -} |
25 | | - |
26 | 8 | export const TELEGRAM_RECONCILE_KEY = |
27 | 9 | "telegram:integration:last-reconciled-at"; |
28 | 10 | export const TELEGRAM_WEBHOOK_RECONCILE_KEY = TELEGRAM_RECONCILE_KEY; |
@@ -73,9 +55,15 @@ export async function reconcileTelegramIntegration(options?: { |
73 | 55 | } |
74 | 56 | } |
75 | 57 |
|
76 | | - // Strip the bypass query param if present — Telegram silently rejects |
77 | | - // webhook URLs that contain it. |
78 | | - const webhookUrl = stripBypassParam(config.webhookUrl); |
| 58 | + // Prefer the dynamically-built URL (includes bypass param when configured). |
| 59 | + // Fall back to stored config.webhookUrl when the origin cannot be resolved |
| 60 | + // (e.g. in tests or environments without NEXT_PUBLIC_APP_URL). |
| 61 | + let webhookUrl: string; |
| 62 | + try { |
| 63 | + webhookUrl = buildTelegramWebhookUrl(); |
| 64 | + } catch { |
| 65 | + webhookUrl = config.webhookUrl; |
| 66 | + } |
79 | 67 | await setWebhook(config.botToken, webhookUrl, config.webhookSecret); |
80 | 68 |
|
81 | 69 | let commandsSynced = false; |
@@ -118,7 +106,7 @@ export async function reconcileTelegramIntegration(options?: { |
118 | 106 | await getStore().setValue(TELEGRAM_RECONCILE_KEY, checkedAt); |
119 | 107 |
|
120 | 108 | logInfo("channels.telegram_integration_reconciled", { |
121 | | - webhookUrl: stripBypassParam(config.webhookUrl), |
| 109 | + webhookUrl, |
122 | 110 | commandsSynced, |
123 | 111 | commandCount, |
124 | 112 | }); |
|
0 commit comments