Files
claude-status-webhook/src/bot-setup.js
tiennm99 30ffaae612 feat: add /help, /history, /uptime commands and enhance /status
- /help: detailed guide with examples for all commands
- /status: overall indicator, emoji markers, updated time, status page link
- /history [count]: recent incidents with impact, dates, links (max 10)
- /uptime: component health with last change time
- Split info commands into bot-info-commands.js for modularity
- Register all 7 commands in bot-setup.js
2026-04-08 23:41:53 +07:00

47 lines
1.5 KiB
JavaScript

const TELEGRAM_API = "https://api.telegram.org/bot";
const BOT_COMMANDS = [
{ command: "help", description: "Detailed command guide" },
{ command: "start", description: "Subscribe to status notifications" },
{ command: "stop", description: "Unsubscribe from notifications" },
{ command: "status", description: "Current system status" },
{ command: "subscribe", description: "Set notification preferences" },
{ command: "history", description: "Recent incidents" },
{ command: "uptime", description: "Component health overview" },
];
/**
* 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,
});
}