// `peppa-dev tg ...`: the dev stand's PM chat — read bot messages and act as the PM. const API = process.env.TELEGRAM_API_URL ?? "http://127.0.0.1:3190"; type Msg = { message_id: number; text: string; edits: number; reply_to?: number; reply_markup?: { inline_keyboard: Array> } }; const get = async (p: string) => (await fetch(`${API}${p}`)).json() as Promise; const post = async (p: string, body: unknown) => (await fetch(`${API}${p}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) })).json() as Promise; const plain = (html: string) => html.replace(/<[^>]+>/g, "").replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&"); function show(m: Msg) { const buttons = (m.reply_markup?.inline_keyboard ?? []).flat(); console.log(`── #${m.message_id}${m.edits ? ` (edited ×${m.edits})` : ""}${m.reply_to ? ` ↩ #${m.reply_to}` : ""}`); console.log(plain(m.text)); if (buttons.length) console.log(` [${buttons.map((b) => `${b.text} → ${b.callback_data}`).join("] [")}]`); } async function messages(n = 10) { const all = await get("/_mock/messages"); all.slice(-n).forEach(show); if (!all.length) console.log("(no messages)"); } /** After an action, show what the bot did in reaction. */ async function aftermath(sinceIds: Set, edited: Map) { await new Promise((r) => setTimeout(r, 2500)); const all = await get("/_mock/messages"); const changed = all.filter((m) => !sinceIds.has(m.message_id) || (edited.get(m.message_id) ?? 0) !== m.edits); changed.forEach(show); const toasts = await get>("/_mock/callbacks"); if (toasts.length) console.log(`toast: ${toasts.at(-1)?.text ?? ""}`); } async function snapshot() { const all = await get("/_mock/messages"); return { ids: new Set(all.map((m) => m.message_id)), edits: new Map(all.map((m) => [m.message_id, m.edits])) }; } const [cmd, ...args] = process.argv.slice(2); switch (cmd) { case "messages": await messages(Number(args[0] ?? 10)); break; case "click": { const id = Number(args[0]); const want = args.slice(1).join(" ").toLowerCase(); const m = (await get("/_mock/messages")).find((x) => x.message_id === id); const b = (m?.reply_markup?.inline_keyboard ?? []).flat().find((x) => x.callback_data === want || x.text.toLowerCase().includes(want)); if (!b?.callback_data) throw new Error(`no button "${want}" on message #${id}`); const s = await snapshot(); await post("/_mock/click", { messageId: id, data: b.callback_data }); await aftermath(s.ids, s.edits); break; } case "reply": case "send": { const s = await snapshot(); await post("/_mock/send", cmd === "reply" ? { replyTo: Number(args[0]), text: args.slice(1).join(" ") } : { text: args.join(" ") }); await aftermath(s.ids, s.edits); break; } case "callbacks": console.log(JSON.stringify(await get("/_mock/callbacks"), null, 2)); break; default: console.log("peppa-dev tg messages [n] | click