// api/order.js — Vercel serverless function (optional backend) // Receives an exchange request from the website and forwards it to your // own Telegram chat via a bot, so you get the order even if the user // never presses "send" in their own Telegram. // // Setup (see DEPLOY.md): // 1. Create a bot with @BotFather -> get the bot token. // 2. Create a Telegram group "Orders", add your bot to it. // 3. Find the group's chat id (DEPLOY.md explains how). // 4. In Vercel project -> Settings -> Environment Variables add: // TELEGRAM_BOT_TOKEN = // TELEGRAM_CHAT_ID = // 5. In index.html set: var BACKEND_ENABLED = true; export default async function handler(req, res) { if (req.method !== "POST") { return res.status(405).json({ ok: false, error: "Method not allowed" }); } const token = process.env.TELEGRAM_BOT_TOKEN; const chatId = process.env.TELEGRAM_CHAT_ID; if (!token || !chatId) { return res.status(500).json({ ok: false, error: "Server not configured" }); } const b = req.body || {}; // Basic sanity checks (we only expect 2–4 requests, keep it simple) const id = String(b.id || "").slice(0, 20); const send = String(b.send || "").slice(0, 80); const get = String(b.get || "").slice(0, 80); const address = String(b.address || "").slice(0, 120); const telegram = String(b.telegram || "").slice(0, 60); const email = String(b.email || "").slice(0, 80); if (!id || !address || !telegram) { return res.status(400).json({ ok: false, error: "Missing fields" }); } const text = `🟢 New exchange request ${id}\n` + `Send: ${send}\n` + `Get: ${get}\n` + `Receive address: ${address}\n` + `Telegram: ${telegram}` + (email ? `\nEmail: ${email}` : ""); try { const r = await fetch(`https://api.telegram.org/bot${token}/sendMessage`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ chat_id: chatId, text, disable_web_page_preview: true }), }); const j = await r.json(); return res.status(200).json({ ok: !!j.ok }); } catch (e) { return res.status(500).json({ ok: false, error: "Telegram send failed" }); } }