From 37c2a0167704e0a477b8257e7ec7135080e0fe24 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Wed, 8 Apr 2026 23:31:11 +0700 Subject: [PATCH] feat: add setup endpoint to register bot commands and webhook GET /webhook/setup/:secret registers commands with Telegram and sets webhook URL in one step. --- CLAUDE.md | 1 + README.md | 10 +++++----- src/bot-setup.js | 43 +++++++++++++++++++++++++++++++++++++++++++ src/index.js | 2 ++ 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 src/bot-setup.js diff --git a/CLAUDE.md b/CLAUDE.md index d91fb9e..83a0ee0 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -30,6 +30,7 @@ Cloudflare Workers with two entry points exported from `src/index.js`: | Method | Path | Handler | Purpose | |--------|------|---------|---------| | GET | `/` | inline | Health check | +| GET | `/webhook/setup/:secret` | `bot-setup.js` | One-time: register bot commands + set Telegram webhook | | POST | `/webhook/telegram` | `bot-commands.js` | grammY `webhookCallback("cloudflare-mod")` | | POST | `/webhook/status/:secret` | `statuspage-webhook.js` | Receives Statuspage webhooks | diff --git a/README.md b/README.md index 8e265bf..d72b36d 100644 --- a/README.md +++ b/README.md @@ -80,15 +80,15 @@ npm run deploy Note the worker URL from the output (e.g., `https://claude-status-webhook..workers.dev`). -### 6. Set Telegram webhook +### 6. Set up Telegram bot -Replace `` and `` with your values: +Visit the setup URL in your browser to register bot commands and set the Telegram webhook in one step: -```bash -curl "https://api.telegram.org/bot/setWebhook?url=/webhook/telegram" +``` +https:///webhook/setup/ ``` -You should see `{"ok":true,"result":true,"description":"Webhook was set"}`. +You should see a JSON response with `{"ok":true}` for both `webhook` and `commands`. ### 7. Configure Statuspage webhook diff --git a/src/bot-setup.js b/src/bot-setup.js new file mode 100644 index 0000000..fcacb2e --- /dev/null +++ b/src/bot-setup.js @@ -0,0 +1,43 @@ +const TELEGRAM_API = "https://api.telegram.org/bot"; + +const BOT_COMMANDS = [ + { command: "start", description: "Subscribe to status notifications" }, + { command: "stop", description: "Unsubscribe from notifications" }, + { command: "status", description: "Check current Claude status" }, + { command: "subscribe", description: "Set notification preferences" }, +]; + +/** + * One-time setup: register bot commands and set Telegram webhook. + * GET /webhook/setup/:secret + */ +export async function setupBot(c) { + const secret = c.req.param("secret"); + if (secret !== c.env.WEBHOOK_SECRET) { + return c.text("Unauthorized", 401); + } + + const token = c.env.BOT_TOKEN; + const workerUrl = new URL(c.req.url).origin; + + // Set webhook URL + const webhookRes = await fetch(`${TELEGRAM_API}${token}/setWebhook`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ url: `${workerUrl}/webhook/telegram` }), + }); + const webhookData = await webhookRes.json(); + + // Register bot commands + const commandsRes = await fetch(`${TELEGRAM_API}${token}/setMyCommands`, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ commands: BOT_COMMANDS }), + }); + const commandsData = await commandsRes.json(); + + return c.json({ + webhook: webhookData, + commands: commandsData, + }); +} diff --git a/src/index.js b/src/index.js index fd7079d..aecb7ce 100644 --- a/src/index.js +++ b/src/index.js @@ -2,10 +2,12 @@ import { Hono } from "hono"; import { handleTelegramWebhook } from "./bot-commands.js"; import { handleStatuspageWebhook } from "./statuspage-webhook.js"; import { handleQueue } from "./queue-consumer.js"; +import { setupBot } from "./bot-setup.js"; const app = new Hono(); app.get("/", (c) => c.text("Claude Status Bot is running")); +app.get("/webhook/setup/:secret", (c) => setupBot(c)); app.post("/webhook/telegram", (c) => handleTelegramWebhook(c)); app.post("/webhook/status/:secret", (c) => handleStatuspageWebhook(c));