grammyjs / grammY

The Telegram Bot Framework.
https://grammy.dev
MIT License
2.33k stars 112 forks source link

How to run Grammy in Nextjs using the APP folders? #499

Closed null-prophet closed 1 year ago

null-prophet commented 1 year ago

What is the equivalent way to running the webhook call in the new app folder structure in nextjs?

https://nextjs.org/docs/app/building-your-application/routing/route-handlers

The structure of the files is slightly different and the handlers are based on rest/http verbs (GET, PUT etc). I ended up just stuffing stuff into the pages folder and it works. But it would be nice to translate all the code to the new way of doing things.

Thanks again for this excellent framework too, super easy to use!

KnorpelSenf commented 1 year ago

But it would be nice to translate all the code to the new way of doing things.

What do you mean?

barinbritva commented 1 year ago

@KnorpelSenf I have the same question as @null-prophet. For me the problem is following. I've set up Grammy using webhooks using Express. Now I try to run the same in Next.js 12, but I couldn't so far. All the examples I found are about previous Next.js folder structure. There was enough to just create /api/webhook.ts with the content:

import { bot } from '../src/bot'
import { webhookCallback } from 'grammy'

// webhookCallback will make sure that the correct middleware(listener) function is called
export default webhookCallback(bot, 'http')

Next.js 13 doesn't allow to do default export, it forces to define named methods like POST, etc.

I tried to implement that as /app/api/webhook/route.ts:

export function POST() {
       // do we need to use 'http' adapter or 'next-js'?
    webhookCallback(bot, 'http');
    return NextResponse.json({ok: true});
}

I create the bot like this:

const bot = createBot();
bot.api.setWebhook(`${process.env.NEXT_PUBLIC_TELEGRAM_BOT_HOOK_URL}/api/webhook`);

In this case webhook status getWebhookInfo gives me OK, but bot doesn't respond any command. I tried do like this:

export function POST() {
    return webhookCallback(bot, 'http');
}

This setup gives me Wrong response from the webhook: 500 Internal Server Error.

So, at the moment I just run my bot completely separated from the main Next.js files using express setup. I would like to run it using Next.js if possible

Any suggestions? I would appreciate any help

KnorpelSenf commented 1 year ago

I have never used nextjs, and I don't plan on starting with that, so I'm not the right person to explain nextjs specifics. Perhaps @PonomareVlad knows?

PonomareVlad commented 1 year ago

@null-prophet and @barinbritva can you test this example on your cases ?

barinbritva commented 1 year ago

@PonomareVlad thanks a lot! That fixes everything. I don't know why I didn't figure out to try:

export const POST = webhookCallback(bot, 'std/http');
KnorpelSenf commented 1 year ago

@barinbritva great to hear! Could you approve the pull request that adds the example? :)

barinbritva commented 1 year ago

@KnorpelSenf done!

KnorpelSenf commented 1 year ago

Nice! Thanks everyone! Closing.

null-prophet commented 12 months ago

thanks! will test out shortly.