forked from pinks/eris
webhooks #1
47
bot/mod.ts
47
bot/mod.ts
|
@ -1,7 +1,3 @@
|
||||||
|
|
||||||
// This new serve here is an issue, since there is already another serve for the UI in /api/mod.ts
|
|
||||||
import { serve } from "https://deno.land/std/http/server.ts";
|
|
||||||
// End of issue
|
|
||||||
import { sequentialize } from "grammy_runner";
|
import { sequentialize } from "grammy_runner";
|
||||||
import { Api, Bot, Context, RawApi, session, SessionFlavor } from "grammy";
|
import { Api, Bot, Context, RawApi, session, SessionFlavor } from "grammy";
|
||||||
import { FileFlavor, hydrateFiles } from "grammy_files";
|
import { FileFlavor, hydrateFiles } from "grammy_files";
|
||||||
|
@ -177,35 +173,26 @@ bot.command("crash", () => {
|
||||||
throw new Error("Crash command used");
|
throw new Error("Crash command used");
|
||||||
});
|
});
|
||||||
|
|
||||||
// New section that uses a webhook instead of long polling.
|
// Set up the webhook in the telegram API and initialize the bot
|
||||||
|
await bot.api.setWebhook('https://nyxdev.akiru.de:8443/webhook');
|
||||||
// Set up the webhook and initialize the bot
|
|
||||||
await bot.api.setWebhook('https://YOUR_URL:8443/webhook');
|
|
||||||
await bot.init();
|
await bot.init();
|
||||||
|
|
||||||
// Function to handle incoming requests
|
// Function to handle incoming webhook requests
|
||||||
async function handleRequest(req: Request): Promise<Response> {
|
export async function handleWebhook(req: Request): Promise<Response> {
|
||||||
if (req.method === "POST" && new URL(req.url).pathname === "/webhook") {
|
try {
|
||||||
try {
|
const body = await req.json();
|
||||||
const body = await req.json();
|
console.log("Received webhook data:", JSON.stringify(body, null, 2));
|
||||||
console.log("Received webhook data:", JSON.stringify(body, null, 2));
|
|
||||||
|
|
||||||
// Log before processing update
|
// Log before processing update
|
||||||
console.log("Processing update through handleUpdate...");
|
console.log("Processing update through handleUpdate...");
|
||||||
await bot.handleUpdate(body); // Process the update
|
await bot.handleUpdate(body); // Process the update
|
||||||
// Log after processing update
|
// Log after processing update
|
||||||
console.log("Update processed successfully.");
|
console.log("Update processed successfully.");
|
||||||
|
|
||||||
return new Response(JSON.stringify({ status: 'ok' }), { headers: { 'Content-Type': 'application/json' } });
|
return new Response(JSON.stringify({ status: 'ok' }), { headers: { 'Content-Type': 'application/json' } });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Detailed error logging
|
// Detailed error logging
|
||||||
console.error("Error in handleRequest or handleUpdate:", error);
|
console.error("Error in handleWebhook:", error);
|
||||||
return new Response("Error", { status: 500 });
|
return new Response("Error", { status: 500 });
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return new Response("Not Found", { status: 404 });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the server
|
|
||||||
console.log("Listening for webhooks on https://nyx.akiru.de:8443/webhook");
|
|
||||||
await serve(handleRequest, { port: 8443 });
|
|
||||||
|
|
Loading…
Reference in New Issue