// Cloudflare Pages Functions // file path: functions/api/notify.js export async function onRequestPost(context) { const { request, env } = context; let body; try { body = await request.json(); } catch { return new Response('bad json', { status: 400 }); } const text = (body && body.text) ? String(body.text) : ''; if (!text) return new Response('missing text', { status: 400 }); const token = env.TELEGRAM_BOT_TOKEN; const chatId = env.TELEGRAM_CHAT_ID; if (!token || !chatId) return new Response('missing telegram env', { status: 500 }); const url = `https://api.telegram.org/bot${token}/sendMessage`; const payload = { chat_id: chatId, text, disable_web_page_preview: true }; const resp = await fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(payload) }); if (!resp.ok) { const t = await resp.text().catch(() => ''); return new Response(`telegram error: ${resp.status} ${t}`.slice(0, 800), { status: 502 }); } return new Response('ok', { status: 200 }); }