Skip to content

chahakshahcs5/telegram-bot-boilerplate

Repository files navigation

πŸ€– Telegram Bot Boilerplate

A production-ready Telegram bot template built with Node.js, TypeScript, MongoDB, and Docker. Includes auto-tunneling via ngrok for seamless local development with webhooks.

Node.js TypeScript MongoDB grammY License: MIT


✨ Features

  • πŸ—οΈ TypeScript β€” Fully typed codebase with strict mode
  • πŸƒ MongoDB + Mongoose β€” Database ready out of the box
  • 🎯 grammY β€” Modern, fast Telegram Bot framework
  • 🌐 Auto ngrok tunneling β€” Webhooks work locally without manual setup
  • 🐳 Docker & Docker Compose β€” One-command dev environment with hot reload
  • πŸ”„ 3 bot modes β€” Polling, webhook, and auto-ngrok (auto-detected)
  • πŸ›‘ Graceful shutdown β€” Clean cleanup of bot, tunnel, and server
  • ⚑ Hot reload β€” Instant feedback during development

πŸ“ Project Structure

β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ index.ts          # Entry point β€” mode detection & server setup
β”‚   β”œβ”€β”€ bot.ts            # Bot commands & message handlers
β”‚   β”œβ”€β”€ config.ts         # Environment config loader
β”‚   └── db/
β”‚       └── connection.ts # MongoDB connection handler
β”œβ”€β”€ .env.example          # Environment variables template
β”œβ”€β”€ docker-compose.yml    # Docker services (app + MongoDB)
β”œβ”€β”€ Dockerfile            # Multi-stage build (dev + prod)
β”œβ”€β”€ tsconfig.json         # TypeScript configuration
β”œβ”€β”€ nodemon.json          # Nodemon config for Docker hot reload
└── package.json

πŸš€ Quick Start

Prerequisites

1. Use this template

Click the "Use this template" button on GitHub, or clone it:

git clone https://github.com/YOUR_USERNAME/telegram-bot-boilerplate.git
cd telegram-bot-boilerplate

2. Install dependencies

npm install

3. Configure environment

cp .env.example .env

Edit .env with your values:

BOT_TOKEN=your_bot_token_here
MONGO_URI=mongodb://localhost:27017/telegram-bot
PORT=3000
NGROK_AUTHTOKEN=your_ngrok_authtoken_here  # Optional, for webhook mode

4. Start the bot

npm run dev

That's it! πŸŽ‰ Send /start to your bot on Telegram.


πŸ”„ Bot Modes

The bot automatically selects the best mode based on your environment variables:

Priority Condition Mode Best For
1 WEBHOOK_URL is set Production Webhook Deployed servers
2 NGROK_AUTHTOKEN is set Auto Ngrok Tunnel Local dev with webhooks
3 Neither is set Long Polling Simplest local dev

Polling Mode (default)

No public URL needed. The bot polls Telegram for updates:

BOT_TOKEN=your_token
# No WEBHOOK_URL or NGROK_AUTHTOKEN

Ngrok Mode (auto-tunnel)

Automatically creates a public URL via ngrok and sets the webhook:

BOT_TOKEN=your_token
NGROK_AUTHTOKEN=your_ngrok_token

Get your free auth token at dashboard.ngrok.com

Production Webhook Mode

Uses your own public URL:

BOT_TOKEN=your_token
WEBHOOK_URL=https://your-domain.com

🐳 Docker

Development (with hot reload)

docker-compose up

This starts:

  • App container β€” with hot reload via volume mounts
  • MongoDB container β€” persistent data in a Docker volume

Edit files in src/ and changes reflect instantly.

Production

docker build --target production -t telegram-bot .
docker run --env-file .env telegram-bot

πŸ› οΈ Available Scripts

Script Description
npm run dev Start with hot reload (tsx watch)
npm run dev:docker Start with hot reload in Docker (nodemon)
npm run build Compile TypeScript to dist/
npm start Run compiled production build

🧩 Extending the Bot

Adding a new command

Edit src/bot.ts:

bot.command("ping", async (ctx) => {
  await ctx.reply("πŸ“ Pong!");
});

Adding a Mongoose model

Create src/models/user.ts:

import mongoose from "mongoose";

const userSchema = new mongoose.Schema({
  telegramId: { type: Number, required: true, unique: true },
  username: String,
  firstName: String,
  createdAt: { type: Date, default: Date.now },
});

export const User = mongoose.model("User", userSchema);

Use it in your bot:

import { User } from "./models/user.js";

bot.command("start", async (ctx) => {
  await User.findOneAndUpdate(
    { telegramId: ctx.from?.id },
    {
      username: ctx.from?.username,
      firstName: ctx.from?.first_name,
    },
    { upsert: true },
  );
  await ctx.reply("Welcome! You've been registered. πŸŽ‰");
});

πŸ“š Tech Stack

Technology Purpose
grammY Telegram Bot framework
Mongoose MongoDB ODM
tsx TypeScript execution & watch mode
@ngrok/ngrok Programmatic tunneling
Docker Containerization

πŸ“„ License

This project is licensed under the MIT License.


Made with ❀️ β€” Happy bot building!

About

Telegram bot boilerplate using Node.js, TypeScript, MongoDB, Docker and Ngrok proxy for local development and deployment to Render

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors