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.
This commit is contained in:
2026-04-08 23:31:11 +07:00
parent 68b9604598
commit 37c2a01677
4 changed files with 51 additions and 5 deletions

View File

@@ -30,6 +30,7 @@ Cloudflare Workers with two entry points exported from `src/index.js`:
| Method | Path | Handler | Purpose | | Method | Path | Handler | Purpose |
|--------|------|---------|---------| |--------|------|---------|---------|
| GET | `/` | inline | Health check | | 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/telegram` | `bot-commands.js` | grammY `webhookCallback("cloudflare-mod")` |
| POST | `/webhook/status/:secret` | `statuspage-webhook.js` | Receives Statuspage webhooks | | POST | `/webhook/status/:secret` | `statuspage-webhook.js` | Receives Statuspage webhooks |

View File

@@ -80,15 +80,15 @@ npm run deploy
Note the worker URL from the output (e.g., `https://claude-status-webhook.<your-subdomain>.workers.dev`). Note the worker URL from the output (e.g., `https://claude-status-webhook.<your-subdomain>.workers.dev`).
### 6. Set Telegram webhook ### 6. Set up Telegram bot
Replace `<BOT_TOKEN>` and `<WORKER_URL>` 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<BOT_TOKEN>/setWebhook?url=<WORKER_URL>/webhook/telegram" https://<WORKER_URL>/webhook/setup/<WEBHOOK_SECRET>
``` ```
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 ### 7. Configure Statuspage webhook

43
src/bot-setup.js Normal file
View File

@@ -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,
});
}

View File

@@ -2,10 +2,12 @@ import { Hono } from "hono";
import { handleTelegramWebhook } from "./bot-commands.js"; import { handleTelegramWebhook } from "./bot-commands.js";
import { handleStatuspageWebhook } from "./statuspage-webhook.js"; import { handleStatuspageWebhook } from "./statuspage-webhook.js";
import { handleQueue } from "./queue-consumer.js"; import { handleQueue } from "./queue-consumer.js";
import { setupBot } from "./bot-setup.js";
const app = new Hono(); const app = new Hono();
app.get("/", (c) => c.text("Claude Status Bot is running")); 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/telegram", (c) => handleTelegramWebhook(c));
app.post("/webhook/status/:secret", (c) => handleStatuspageWebhook(c)); app.post("/webhook/status/:secret", (c) => handleStatuspageWebhook(c));