Create a Telegram Bot in Next.js App Router
How to create a Telegram bot in a Next.js App Router application using the Grammy SDK?
How to create a Telegram bot in a Next.js App Router application using the Grammy SDK?
In this guide, you will learn how to use the Grammy SDK to create a Telegram bot in a Next.js application. You will go through the process of setting up a new Next.js project, configuring Grammy SDK with Next.js, and creating an API route to respond to user messages.
You’ll need the following:
Let’s get started by creating a new Next.js project. Open your terminal and run the following command:
npx create-next-app@latest my-appWhen prompted, choose:
Yes when prompted to use TypeScript.No when prompted to use ESLint.Yes when prompted to use Tailwind CSS.No when prompted to use src/ directory.Yes when prompted to use App Router.No when prompted to customize the default import alias (@/*).Once that is done, move into the project directory and start the app in development mode by executing the following command:
cd my-appnpm run devThe app should be running on localhost:3000. Now, let’s close the development server to install Grammy SDK.
First, run the following command to install the Grammy SDK:
grammy: A package that makes it easy to create Telegram bots.npm install grammyFurther, perform the following code addition in next.config.mjs file to allow grammy to use Node.js specific features in Next.js App Router:
// File: next.config.mjs // [!code focus]
/** @type {import('next').NextConfig} */const nextConfig = { experimental: { // [!code ++] // [!code focus] serverComponentsExternalPackages: ['grammy'], // [!code ++] // [!code focus] }, // [!code ++] // [!code focus]}
export default nextConfigNow, let’s move on to creating an API route to respond to user messages with the bot.
To create an API route that responds to the POST request by Telegram to respond to user messages interacting with your bot, create a file named route.ts in the app/api/bot directory with the following code:
export const dynamic = 'force-dynamic'
export const fetchCache = 'force-no-store'
import { Bot, webhookCallback } from 'grammy'
const token = process.env.TELEGRAM_BOT_TOKEN
if (!token) throw new Error('TELEGRAM_BOT_TOKEN environment variable not found.')
const bot = new Bot(token)bot.on('message:text', async (ctx) => { await ctx.reply(ctx.message.text)})
export const POST = webhookCallback(bot, 'std/http')The code above does the following:
Bot and webhookCallback helpers from the Grammy SDK.webhookCallback helper relying on standard HTTP Request and Response mechanism as the handler to incoming POST requests.With that done, let’s move on to deploying the application.
The code is now ready to deploy to Vercel. Use the following steps to deploy:
.env file.To configure Telegram to invoke the /api/bot route to respond to user interactions with your bot, run the following command after updating it with your Telegram Bot Token and Vercel Deployment URL.
curl https://api.telegram.org/bot<telegram_bot_token>/setWebhook?url=https://<your-deployment.vercel>.app/api/botIn this guide, you learned how to integrate Grammy SDK to create your own Telegram Bot in Next.js. Further, you learnt how to point the Telegram bot to your custom deployment URL to respond to user messages with the bot.
If you have any questions or comments, feel free to reach out to me on Twitter.
Learn how to deploy a server-side rendered Angular 20+ application to Cloudflare Workers, serving static assets from the edge and running SSR.
LaunchFast works with Backblaze B2 out of the box. Same STORAGE_PROVIDER=s3, same pre-signed upload flow. Point AWS_ENDPOINT_URL_S3 at your regional B2 S3 endpoint and add your application key.
LaunchFast works with Tigris out of the box. Same STORAGE_PROVIDER=s3, same pre-signed upload flow. Point AWS_ENDPOINT_URL_S3 at https://fly.storage.tigris.dev and add your credentials.