grammyjs / website

The grammY documentation website.
https://grammy.dev
MIT License
46 stars 104 forks source link

Hosting: Supabase Edge Functions doesn't work #1057

Closed MultiTea closed 5 months ago

MultiTea commented 5 months ago

Hello,

I've been following the current hosting tutorial as show on this documentation page : https://grammy.dev/hosting/supabase. Deployment is successful and the webhook is correctly set, but despite following all these steps, Edge Functions logs keeps serving me the following message when I sent one of the examples commands :

TypeError: cannot read headers: request closed at get headerList (ext:deno_fetch/23_request.js:118:17) at request. (ext:deno_fetch/23_request.js:571:60) at get [headers] (ext:deno_fetch/23_request.js:253:46) at get headers (ext:deno_fetch/23_request.js:453:16) at stdHttp (https://deno.land/x/grammy@v1.23.0/convenience/frameworks.ts:224:17) at https://deno.land/x/grammy@v1.23.0/convenience/webhook.ts:24:75 at Object.handler (file:///home/deno/functions/telegram-bot/index.ts:20:18) at respond (ext:sb_core_main_js/js/http.js:162:38) at handleHttp (ext:sb_core_main_js/js/http.js:122:5) at eventLoopTick (ext:core/01_core.js:64:7)

event loop error: Interrupted: operation canceled at async readableStreamCollectIntoUint8Array (ext:deno_web/06_streams.js:1074:19) at async consumeBody (ext:deno_fetch/22_body.js:241:9)

Curiously, this doesn't happen whenever I use the decrepated version of Grammy (1.8.3) as shown on this tutorial : https://github.com/grammyjs/examples/blob/main/setups/supabase-edge-functions/supabase/functions/telegram-bot/index.ts

Any help would be appreciated,

Thanks

MultiTea commented 5 months ago

It turns out that this code excerpt taken from a recent repository using this hosing method works:

import {
  Bot,
  webhookCallback,
} from "https://deno.land/x/grammy@v1.20.3/mod.ts";

const token = Deno.env.get("BOT_TOKEN");
if (!token) throw new Error("BOT_TOKEN is unset");

const bot = new Bot(token);

bot.command("start", (ctx) => ctx.reply("Welcome! Up and running."));
bot.command("ping", (ctx) => ctx.reply(`Pong! ${new Date()}`));

const handleUpdate = webhookCallback(bot, "std/http");

Deno.serve(async (req) => {
  const headers = req.headers;
  try {
    const url = new URL(req.url);
    if (url.searchParams.get("secret") !== Deno.env.get("FUNCTION_SECRET")) {
      return new Response("not allowed", { status: 405 });
    }

    return await handleUpdate(req);
  } catch (err) {
    console.log(headers);
    console.error(err);
  }
  return new Response();
});

Even tough very few changes have been made from the recent example shown in the documentation. If anyone has an explanation why this is happening, let us know.

KnorpelSenf commented 5 months ago

Is the only difference the version of the grammY core library? If so, then it could be a regression in grammY. Can you narrow this down?

MultiTea commented 5 months ago

Sure, it seems that the import serve method is decrepated, and now use the Deno.serve. While I tried to import the latest version of grammY into the older method, it kept looping on the error I've shown above. Now, it seems to work like a charm even with the latest version of grammY. It was just a beginner mistake I guess.