import { NextRequest, NextResponse } from "next/server"; import { Telegraf } from "telegraf"; const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN || ""; export async function GET(request: NextRequest) { if (!TELEGRAM_BOT_TOKEN) { return NextResponse.json({ success: false, error: "No telegram token in ENV" }, { status: 500 }); } try { const host = request.headers.get("host"); const protocol = host?.includes("localhost") ? "http" : "https"; const webhookUrl = `${protocol}://${host}/api/telegram/webhook`; const bot = new Telegraf(TELEGRAM_BOT_TOKEN); // Setting Webhook await bot.telegram.setWebhook(webhookUrl); // Getting current webhook info to verify const webhookInfo = await bot.telegram.getWebhookInfo(); return NextResponse.json({ success: true, message: `Webhook successfully registered to ${webhookUrl}`, info: webhookInfo }); } catch (error) { console.error("Error registering webhook:", error); return NextResponse.json({ success: false, error: "Failed to register webhook" }, { status: 500 }); } }