How to Build a Telegram (and Bale) Bot With Node.js: Step by Step
A practical tutorial: create a Telegram bot with Node.js and grammY, add commands and buttons, deploy it with a webhook, keep the token safe, and run the same code as a Bale bot.
- telegram bot tutorial
- build a telegram bot
- node.js telegram bot
- grammY
- bale bot

This tutorial builds a small but real Telegram bot with Node.js: it answers commands, shows buttons, and runs on a server with a webhook. At the end, the same code runs as a bot on Bale, the Iranian messenger, with one line changed.
You need Node.js 18 or newer and a Telegram account. The examples use grammY, a well-maintained bot framework for JavaScript and TypeScript (version 1.46 at the time of writing).
1. Get a bot token
- In Telegram, open @BotFather and send
/newbot. - Choose a display name, then a username that ends in
bot. - BotFather replies with a token like
123456:ABC-.... Anyone with this token controls your bot, so treat it like a password.
2. Create the project
mkdir my-bot && cd my-bot
npm init -y
npm install grammy
npm pkg set type=module
Keep the token out of your code. Put it in an environment variable:
export BOT_TOKEN="123456:ABC-..."
3. Write the bot
Create bot.js:
import { Bot, InlineKeyboard } from "grammy";
const bot = new Bot(process.env.BOT_TOKEN);
bot.command("start", (ctx) =>
ctx.reply("Hi! I can show prices and take orders.", {
reply_markup: new InlineKeyboard().text("Prices", "prices").text("Order", "order"),
}),
);
bot.callbackQuery("prices", async (ctx) => {
await ctx.answerCallbackQuery();
await ctx.reply("Basic plan: $10 a month.");
});
bot.callbackQuery("order", async (ctx) => {
await ctx.answerCallbackQuery();
await ctx.reply("Send your name and phone number, and we will call you.");
});
bot.on("message:text", (ctx) => ctx.reply(`You said: ${ctx.message.text}`));
bot.catch((err) => console.error("Bot error:", err.error));
bot.start();
Run it with node bot.js, open your bot in Telegram and send /start. bot.start() uses long polling: the bot asks Telegram for new messages in a loop. It is perfect for development.
4. Run it in production with a webhook
On a server, a webhook is usually better: Telegram sends each update to your URL, so nothing runs in a loop. With Express:
npm install express
import express from "express";
import { Bot, webhookCallback } from "grammy";
const bot = new Bot(process.env.BOT_TOKEN);
bot.command("start", (ctx) => ctx.reply("Hi from the webhook!"));
const app = express();
app.use(express.json());
app.use("/telegram", webhookCallback(bot, "express", { secretToken: process.env.WEBHOOK_SECRET }));
app.listen(3000);
Then tell Telegram where to send updates, once:
await bot.api.setWebhook("https://your-domain.com/telegram", { secret_token: process.env.WEBHOOK_SECRET });
The secretToken makes sure only Telegram can call your endpoint. Your server needs HTTPS.
5. The same bot on Bale
Bale offers a bot API that follows the same design as Telegram’s, at https://tapi.bale.ai. Create a bot with Bale’s BotFather, then point grammY at Bale:
const bot = new Bot(process.env.BALE_TOKEN, {
client: { apiRoot: "https://tapi.bale.ai" },
});
Most basics (messages, commands, buttons) work the same way. Bale does not support every Telegram feature, so test each feature you rely on, especially payments and media.
6. Security checklist
- Never commit the token. Use environment variables or an encrypted secrets file. A token pushed to GitHub is found by bots within minutes; this is happening a lot with AI coding agents.
- Validate every input. Users can send anything, including very long text and unexpected files.
- Check who is allowed. For admin commands, compare
ctx.from.idwith a list of allowed user IDs. - Handle errors.
bot.catchkeeps one bad update from crashing the bot.
Where to go next
- Store orders in a database such as PostgreSQL instead of replying only.
- Add in-bot payments; the guide to business bots covers payments, security and cost.
- If you only need a contact form on your website that sends messages to Telegram or Bale, kabot does exactly that on a single Cloudflare Worker.
If you need a production bot built for your business, with payments, an admin panel and support, send me the details.