(
token: string,
method: string,
params: Record<string, unknown> = {},
)
| 74 | const TELEGRAM_API = "https://api.telegram.org/bot"; |
| 75 | |
| 76 | async function tgApi<T = Record<string, unknown>>( |
| 77 | token: string, |
| 78 | method: string, |
| 79 | params: Record<string, unknown> = {}, |
| 80 | ): Promise<T> { |
| 81 | const url = `${TELEGRAM_API}${token}/${method}`; |
| 82 | const res = await fetch(url, { |
| 83 | method: "POST", |
| 84 | headers: { "Content-Type": "application/json" }, |
| 85 | body: JSON.stringify(params), |
| 86 | }); |
| 87 | const json = (await res.json()) as { |
| 88 | ok: boolean; |
| 89 | result?: T; |
| 90 | description?: string; |
| 91 | }; |
| 92 | if (!json.ok) { |
| 93 | throw new Error( |
| 94 | `Telegram ${method} failed: ${json.description ?? JSON.stringify(json)}`, |
| 95 | ); |
| 96 | } |
| 97 | return json.result as T; |
| 98 | } |
| 99 | |
| 100 | // ── Types ───────────────────────────────────────────────────────────────────── |
| 101 |
no outgoing calls
no test coverage detected