import { API, ALLOWED_IDS } from "./config"; interface TgResponse { ok: boolean; result?: T; description?: string; } export async function tg( method: string, body: Record ): Promise> { const res = await fetch(`${API}/${method}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); return res.json() as Promise>; } export function allowed(chatId: number): boolean { if (ALLOWED_IDS.length === 0) return true; return ALLOWED_IDS.includes(chatId); } export async function sendMessage( chatId: number, text: string, opts?: Record ): Promise { const safeText = String(text || ""); try { await tg("sendMessage", { chat_id: chatId, text: safeText, parse_mode: "HTML", disable_web_page_preview: true, ...opts, }); } catch (e: unknown) { const msg = e instanceof Error ? e.message : String(e); if (msg.includes("parse") || msg.includes("HTML")) { await tg("sendMessage", { chat_id: chatId, text: safeText, ...opts, }); } else { throw e; } } }