From 04b0f9bdf5b41aad4b9b4866eb797361933c5526 Mon Sep 17 00:00:00 2001 From: Akiru Date: Thu, 25 Jan 2024 09:08:16 +0000 Subject: [PATCH] bot/mod.ts aktualisiert --- bot/mod.ts | 50 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/bot/mod.ts b/bot/mod.ts index 527956a..71177bf 100644 --- a/bot/mod.ts +++ b/bot/mod.ts @@ -1,7 +1,11 @@ + +// 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 { Api, Bot, Context, RawApi, session, SessionFlavor } from "grammy"; import { FileFlavor, hydrateFiles } from "grammy_files"; import { hydrateReply, ParseModeFlavor } from "grammy_parse_mode"; -import { run, sequentialize } from "grammy_runner"; import { error, info, warning } from "std/log/mod.ts"; import { sessions } from "../api/sessionsRoute.ts"; import { formatUserChat } from "../utils/formatUserChat.ts"; @@ -13,16 +17,6 @@ import { pnginfoCommand, pnginfoQuestion } from "./pnginfoCommand.ts"; import { queueCommand } from "./queueCommand.ts"; import { txt2imgCommand, txt2imgQuestion } from "./txt2imgCommand.ts"; -// This section sets the configuration into the database. Limits the jobs per user and maximum jobs to safe defaults. -import { setConfig, getConfig } from "../app/config.ts"; - -// Set the new configuration -await setConfig({ maxUserJobs: 1, maxJobs: 500 }); - -// Log the updated configuration to the console, for debugging. -// const updatedConfig = await getConfig(); -// console.log("Updated Configuration:", updatedConfig); - interface SessionData { chat: ErisChatData; user: ErisUserData; @@ -183,7 +177,35 @@ bot.command("crash", () => { throw new Error("Crash command used"); }); -export async function runBot() { - const runner = run(bot, { runner: { silent: true } }); - await runner.task(); +// New section that uses a webhook instead of long polling. + +// Set up the webhook and initialize the bot +await bot.api.setWebhook('https://YOUR_URL:8443/webhook'); +await bot.init(); + +// Function to handle incoming requests +async function handleRequest(req: Request): Promise { + if (req.method === "POST" && new URL(req.url).pathname === "/webhook") { + try { + const body = await req.json(); + console.log("Received webhook data:", JSON.stringify(body, null, 2)); + + // Log before processing update + console.log("Processing update through handleUpdate..."); + await bot.handleUpdate(body); // Process the update + // Log after processing update + console.log("Update processed successfully."); + + return new Response(JSON.stringify({ status: 'ok' }), { headers: { 'Content-Type': 'application/json' } }); + } catch (error) { + // Detailed error logging + console.error("Error in handleRequest or handleUpdate:", error); + 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 });