File size: 1,186 Bytes
b8b98dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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 });
    }
}