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

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