Files
Arah2/bot/scripts/set-menu-button.mjs

62 lines
2.2 KiB
JavaScript

#!/usr/bin/env node
/*
* set-menu-button.mjs — point the bot's menu button at the Mini App (phase 3).
* ---------------------------------------------------------------------------
* Phase 2 deliberately armed the menu button as type `commands` so the command
* keyboard became visible. The Mini App replaces that as the menu button's
* DEFAULT action for private chats: type `web_app` opens the deployed origin
* directly from the bot's attach/menu area.
*
* Behavior: reads WEBHOOK_URL from bot/.dev.vars (gitignored) and derives the
* app origin by stripping the /telegram/webhook path — the same origin every
* other phase-3 surface deploys to. Default scope (all private chats).
* Exit 0 only when Telegram answers ok:true; the token is never echoed.
*/
import { readFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
function readDevVars() {
const devVarsPath = path.join(fileURLToPath(new URL(import.meta.url)), "..", "..", ".dev.vars");
let raw = "";
try {
raw = readFileSync(devVarsPath, "utf8");
} catch {
return {};
}
const vars = {};
for (const line of raw.split("\n")) {
const trimmed = line.trim();
if (trimmed.length === 0 || trimmed.startsWith("#")) continue;
const eq = trimmed.indexOf("=");
if (eq === -1) continue;
vars[trimmed.slice(0, eq).trim()] = trimmed.slice(eq + 1).trim();
}
return vars;
}
const devVars = readDevVars();
const token = process.env.BOT_TOKEN ?? devVars.BOT_TOKEN;
const webhookUrl = process.env.WEBHOOK_URL ?? devVars.WEBHOOK_URL;
if (!token || !webhookUrl) {
console.error("Missing BOT_TOKEN or WEBHOOK_URL (bot/.dev.vars or env)");
process.exit(1);
}
const origin = new URL(webhookUrl).origin;
const appUrl = `${origin}/`;
const res = await fetch(`https://api.telegram.org/bot${token}/setChatMenuButton`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
menu_button: { type: "web_app", text: "araharah", web_app: { url: appUrl } },
}),
});
const body = await res.json();
if (!body.ok) {
console.error(`setChatMenuButton failed: ${body.error_code} ${body.description}`);
process.exit(1);
}
console.log("menu button -> web_app", appUrl);