grammY: Timeouts on Vercel deployments
I’m using the following configuration of handling webhooks. While it’s working in development (API route build time is under 1.5s), Vercel constantly reports function timeout without response. Some of the commands are using image send from static host with ctx.replyWithPhoto
. The std/http
method is the only one working for me, neither http
/https
, not next-js
are not working because of different issues
import { Context as GrammyContext, SessionFlavor } from "grammy";
import { Conversation, ConversationFlavor } from "@grammyjs/conversations";
export interface SessionData {
// session fields types
}
export type Context = GrammyContext &
ConversationFlavor &
SessionFlavor<SessionData>;
export type ConversationContext = Conversation<Context>;
export const POST = async (req: NextRequest, ...args: any[]) => {
const { data, error } = await supabase
.from("companies")
.select("bot_token")
.eq("slug", req.headers.get("host")!.split(".")[0])
.single();
if (error) {
return NextResponse.json({ ...error }, { status: 500 });
}
const token = data.bot_token;
const middleware = new Composer<Context>();
middleware.command("start", start);
middleware.command("request", request);
const bot = new Bot<Context>(token);
bot.use(
session({
initial: () => ({
...
}),
storage: enhanceStorage({
storage: freeStorage(token),
millisecondsToLive: 10 * 60 * 1000,
}),
})
);
// conversations, commands and handlers used here
bot.catch((err) => {
console.error("Error:", err);
});
const handleUpdate = webhookCallback(bot, "std/http", "throw", 15_000);
return handleUpdate(req, ...args);
};
Any suggestions or recommendations?
Thanks in advance!
About this issue
- Original URL
- State: closed
- Created 7 months ago
- Comments: 36 (17 by maintainers)
@KnorpelSenf, thank you
I think I have found a solution to the issue.
The key lies in how Next.js handles server dependencies during build time. Just add the
grammy
dependency toserverComponentsExternalPackages
in the next.config.js config and it should work. Find more information aboutserverComponentsExternalPackages"
here: https://nextjs.org/docs/app/api-reference/next-config-js/serverComponentsExternalPackagesMy code:
Route
Next.js config
I hope this will be useful to someone who also decides to create a telegram bot w/ grammY and Next.js.
At some point I decided to migrate my bot to Deno, so it will take me some time to replicate the existing bot back on Next.js. I will post an update as soon as I will do some testings
Hey @KnorpelSenf! Sorry for long reply, wasn’t able to test out the suggestion you made during weekends. I tried to implement it now, but it still failing with timeout. Including the source code of the endpoint and the screenshot of log for triggering the endpoint
app/api/bots/requests/route.ts
@KnorpelSenf
I’ve put up a pull request to add @thecoorum! 🎉