Closed null-prophet closed 1 year ago
But it would be nice to translate all the code to the new way of doing things.
What do you mean?
@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
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?
@null-prophet and @barinbritva can you test this example on your cases ?
@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');
@barinbritva great to hear! Could you approve the pull request that adds the example? :)
@KnorpelSenf done!
Nice! Thanks everyone! Closing.
thanks! will test out shortly.
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!