BBotnexio

Buy now!

← All guides

Build a Telegram Bot in TypeScript with grammY (2026)

Deutsche Version

Telegram bots are among the quickest bots to get running: no server is needed for a first version, and the Bot API is free. This guide builds a small bot in TypeScript with grammY: commands, an inline-button menu, and the choice between long polling and a webhook.

Step 1: Get a bot token from @BotFather

  1. Open a chat with @BotFather in Telegram and send /newbot.
  2. Pick a display name and a username that ends in bot.
  3. BotFather replies with a token like 123456:ABC-DEF…. Treat it like a password: put it in an .env file, never in Git.

Step 2: Set up the project

Per the grammY getting-started guide:

mkdir my-bot && cd my-bot
npm init -y
npm install -D typescript
npx tsc --init
npm install grammy

Step 3: Your first commands

import { Bot } from "grammy";

const bot = new Bot(process.env.BOT_TOKEN!);

bot.command("start", (ctx) => ctx.reply("Welcome! Send /help for a list of commands."));
bot.command("help", (ctx) => ctx.reply("/start – menu\n/help – this list"));
bot.on("message", (ctx) => ctx.reply("Got your message."));

bot.start(); // long polling

Compile with npx tsc and run node bot.js. Your bot answers in Telegram. Set DEBUG="grammy*" before starting to see grammY's logs.

Step 4: An inline-button menu

Inline buttons sit under a message and send a callback query when pressed (grammY keyboards):

import { Bot, InlineKeyboard } from "grammy";

const menu = new InlineKeyboard()
  .text("Language", "menu:lang")
  .text("Feedback", "menu:feedback");

bot.command("start", (ctx) => ctx.reply("What do you want to do?", { reply_markup: menu }));

bot.callbackQuery("menu:lang", async (ctx) => {
  await ctx.answerCallbackQuery();
  await ctx.reply("Choose: /language de or /language en");
});

// Answer every other button, or some clients show a loading spinner for up to a minute.
bot.on("callback_query:data", (ctx) => ctx.answerCallbackQuery());

Register the catch-all handler last.

Step 5: Long polling or webhook?

Long pollingWebhook
Server neededNo, runs on any machineYes, a public HTTPS URL
Setupbot.start()Telegram calls your URL; grammY offers webhookCallback for common web frameworks
Good forStarting out, small botsServerless or always-on hosting

grammY's deployment guide explains both. Start with polling, switch when you need to.

Step 6: Before you invite people

  • Rate limits: limit how often one user can trigger expensive commands.
  • Privacy: store as little as possible. A /privacy command with your contact details, plus /mydata and /deletedata, make it clear to users what you keep.
  • Sessions: if the bot needs to remember a user's language between restarts, store it in a small database such as SQLite, with a retention period.

Telegram Bot Starter

The Telegram Bot Starter is a template with automated tests for your own Telegram bot in TypeScript with grammY. Per the product page it includes /start (inline menu), /help, /language, /feedback, /cancel, /privacy, /mydata and /deletedata in German and English, a feedback flow with inline buttons, DE/EN from the user's Telegram language, sessions in SQLite (node:sqlite), a rate limit per user, admin commands (/stats, /feedbacks, /block, /unblock, /retention), long polling or webhook, Dockerfile and docker-compose. Requirements: Node.js 22.13 or newer (or Docker), your own bot token from @BotFather and your own hosting. Price: €19 incl. VAT. Licence: personal, non-commercial use for private and community groups and channels only, not shops, companies, paid channels or client work (licence). Not affiliated with Telegram.

Common problems

  • Bot doesn't answer: wrong token, or the bot is still running elsewhere with polling. Only one polling instance per token.
  • Buttons keep spinning: a callback query wasn't answered. Add ctx.answerCallbackQuery().
  • Webhook gets nothing: the URL isn't public HTTPS, or a webhook from an earlier setup still points elsewhere.

FAQ

Do I need a server for a Telegram bot?
Not to start. With long polling the bot runs on your own computer. For 24/7 you need a machine that stays on, or a webhook setup with public HTTPS.

grammY or Telegraf?
Both are TypeScript-friendly Node.js frameworks for the Bot API. This guide and the Telegram Bot Starter use grammY.

Is the Telegram Bot API free?
Creating a bot with @BotFather and using the Bot API cost nothing. Hosting is up to you.

Is the Telegram Bot Starter a subscription?
No. €19 incl. VAT, paid once, delivered as a ZIP.

Can I use the Telegram Bot Starter for my shop's customer support?
No. The licence covers non-commercial bots for private and community groups only, not customer support for shops or companies, paid channels or client work.

Related guides

→ Get the Telegram Bot Starter for €19

_Not affiliated with or endorsed by Telegram._