Quick Start
Get a bot running in minutes with this quick start guide.
This quick start guide will help you create a working Telegram bot in just a few minutes. We'll build a simple bot that responds to commands, demonstrating the core concepts of the Igniter.js Bots package. By the end of this guide, you'll have a fully functional bot that you can extend with more features.
The bot we're building will respond to /start and /ping commands, showing you how commands work, how to send messages, and how to set up webhook handlers. This foundation will help you understand the architecture before diving into more advanced features like middleware, dynamic commands, and multi-platform support.
Even though this is a simple example, it demonstrates several important concepts: command creation with validation using Bot.command(), webhook handling, message sending, and event listeners. Once you understand these basics, you'll be ready to build more sophisticated bots.
Quick Start Guide
Install Dependencies
Make sure you have the Bots package installed:
npm install @igniter-js/botpnpm add @igniter-js/botyarn add @igniter-js/botbun add @igniter-js/botSet Up Environment Variables
Create a .env file in your project root:
TELEGRAM_TOKEN=your_bot_token_here
TELEGRAM_WEBHOOK_URL=https://your-domain.com/api/telegram
TELEGRAM_SECRET=optional_webhook_secretGetting a Telegram Token
To get a Telegram bot token:
- Open Telegram and search for @BotFather
- Send
/newbotand follow the instructions - Copy the token you receive
Create the Bot
Create a new file src/bot.ts. For better organization, we'll use Bot.command() to create commands with validation:
import { Bot, telegram } from '@igniter-js/bot'
const startCommand = Bot.command({
name: 'start',
aliases: ['hello'],
description: 'Greets the user',
help: 'Use /start to get a welcome message',
async handle(ctx) {
await ctx.bot.send({
provider: ctx.provider,
channel: ctx.channel.id,
content: { type: 'text', content: '👋 Hello! Welcome to the bot!' }
})
}
})
const pingCommand = Bot.command({
name: 'ping',
aliases: [],
description: 'Check if the bot is alive',
help: 'Use /ping to check bot latency',
async handle(ctx) {
await ctx.bot.send({
provider: ctx.provider,
channel: ctx.channel.id,
content: { type: 'text', content: '🏓 pong!' }
})
}
})
export const bot = Bot.create({
id: 'quick-start-bot',
name: 'Quick Start Bot',
adapters: {
telegram: telegram({
token: process.env.TELEGRAM_TOKEN!,
handle: '@your_bot_username', // Replace with your bot's username
webhook: {
url: process.env.TELEGRAM_WEBHOOK_URL!,
secret: process.env.TELEGRAM_SECRET
}
})
},
commands: {
start: startCommand,
ping: pingCommand
},
on: {
message: async (ctx) => {
// Log all incoming messages
if (ctx.message.content?.type === 'text') {
console.log(`Received: ${ctx.message.content.content}`)
}
}
}
})
// Initialize the bot (registers webhooks, syncs commands)
await bot.start()Create the Webhook Handler
The webhook handler is the entry point where Telegram sends incoming messages. The bot.handle() method processes the incoming request, routes it through middleware and commands, and returns an appropriate response. This handler needs to be set up as an HTTP endpoint in your application.
Choose your framework:
Test Your Bot
- Deploy your application (or use ngrok for local testing)
- Update your webhook URL in the environment variables
- Send
/startto your bot on Telegram - You should receive a "Hello! Welcome to the bot!" message
It Works!
If you received the welcome message, congratulations! Your bot is working.
What You Built
You've just created a fully functional Telegram bot! Let's break down what you've accomplished and understand how each piece fits together:
Next Steps
Now that you have a working bot, here are some ways to extend it:
- Add More Commands: Create commands for specific functionality your bot needs
- Add Middleware: Implement authentication, logging, or rate limiting
- Explore Adapters: Try adding WhatsApp support or other platforms
- Read the Full Documentation: Deep dive into Commands, Middleware, and Advanced Features
The foundation you've built is solid and ready to grow. Every feature you add will follow these same patterns, making your bot easier to understand and maintain as it evolves.