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/bot
pnpm add @igniter-js/bot
yarn add @igniter-js/bot
bun add @igniter-js/bot

Set 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_secret

Getting a Telegram Token

To get a Telegram bot token:

  1. Open Telegram and search for @BotFather
  2. Send /newbot and follow the instructions
  3. 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

  1. Deploy your application (or use ngrok for local testing)
  2. Update your webhook URL in the environment variables
  3. Send /start to your bot on Telegram
  4. 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.