fix(02-03): set-webhook auto-appends /telegram/webhook to a pathless URL

- Deploy walkthrough revealed a bare workers.dev origin setWebhook answers ok but drops updates at / (404)
- Normalize WEBHOOK_URL: no path -> append /telegram/webhook; invalid URL -> fail fast, never echo values (T-02-04)
- .dev.vars.example documents the path expectation
This commit is contained in:
araharah-executor
2026-10-22 19:56:24 +08:00
parent 2ea7f6edc7
commit d600d6e71a
2 changed files with 25 additions and 1 deletions
+1
View File
@@ -1,4 +1,5 @@
# Copy to .dev.vars and fill for local wrangler dev (D-15). NEVER commit .dev.vars. # Copy to .dev.vars and fill for local wrangler dev (D-15). NEVER commit .dev.vars.
BOT_TOKEN= BOT_TOKEN=
WEBHOOK_SECRET= WEBHOOK_SECRET=
# Full origin + path; set-webhook.mjs auto-appends /telegram/webhook if only an origin is set.
WEBHOOK_URL= WEBHOOK_URL=
+24 -1
View File
@@ -11,6 +11,10 @@
* Inputs come from bot/.dev.vars (gitignored, D-15): BOT_TOKEN, * Inputs come from bot/.dev.vars (gitignored, D-15): BOT_TOKEN,
* WEBHOOK_URL, WEBHOOK_SECRET. The bot token is never echoed — on failure * WEBHOOK_URL, WEBHOOK_SECRET. The bot token is never echoed — on failure
* only the API's error_code/description are printed (T-02-04). * only the API's error_code/description are printed (T-02-04).
* WEBHOOK_URL is normalized: if the configured origin points at the
* workers.dev root (no path), /telegram/webhook is appended automatically —
* a bare origin passed the 2026-10-22 deploy walkthrough caused Telegram to
* answer ok but deliver nothing (updates routed to "/" → 404).
* Exit 0 only when Telegram answers ok:true. * Exit 0 only when Telegram answers ok:true.
*/ */
import { readFileSync } from "node:fs"; import { readFileSync } from "node:fs";
@@ -49,8 +53,27 @@ if (missing.length > 0) {
process.exit(1); process.exit(1);
} }
// Auto-append the route path when the URL has none — prevents the recurring
// misconfiguration where a bare workers.dev origin is registered and the
// Worker answers ok:true but Telegram updates land on "/" (404, silent drop).
const WEBHOOK_ROUTE = "/telegram/webhook";
let normalizedUrl;
try {
const parsed = new URL(webhookUrl);
if (parsed.pathname === "/" || parsed.pathname === "") {
parsed.pathname = WEBHOOK_ROUTE;
normalizedUrl = parsed.toString();
console.log(`WEBHOOK_URL had no path — normalized to ...${WEBHOOK_ROUTE}`);
} else {
normalizedUrl = webhookUrl;
}
} catch {
console.error(`WEBHOOK_URL is not a valid URL — check bot/.dev.vars (value never echoed).`);
process.exit(1);
}
const payload = { const payload = {
url: webhookUrl, url: normalizedUrl,
secret_token: secret, secret_token: secret,
allowed_updates: ["message"], allowed_updates: ["message"],
}; };