Introduction
grammY makes it easy to create Telegram bots. Whether you’re a beginner or building at scale, grammY provides a simple yet powerful API that’s always up to date with the latest Telegram Bot API features. In this guide, you’ll create your first working bot in just a few minutes.If you’re new to Telegram bots, read the official Introduction for Developers written by the Telegram team first.
Prerequisites
Before you begin, make sure you have:- Node.js (version 12.20.0 or higher) or Deno installed
- A bot token from @BotFather
Getting Your Bot Token
1
Open BotFather
Visit @BotFather on Telegram.
2
Create a new bot
Send the
/newbot command and follow the instructions.3
Save your token
BotFather will give you a bot token. Keep it secret! You’ll need it in the next steps.
Creating Your First Bot
- Node.js
- TypeScript
- Deno
1
Create a new directory
2
Install grammY
3
Create bot.js
Create a file called
bot.js with the following code:4
Run your bot
Understanding the Code
Let’s break down what each part of the code does:Creating a Bot Instance
Bot class is the core of grammY and represents your bot. You must provide your bot token obtained from BotFather.
Listening for Messages
ctx (context) object contains:
ctx.message- the incoming messagectx.reply()- a method to send a replyctx.api- full access to the Telegram Bot API
Starting the Bot
Adding More Features
Now that you have a basic bot running, let’s add some common features:Responding to Commands
Handling Different Message Types
Using the Bot API
Error Handling
It’s important to handle errors gracefully:Customizing Startup
You can customize how your bot starts:Best Practices
Use TypeScript for better development experience. grammY has excellent TypeScript support with full type inference. Your code editor will provide helpful autocomplete and type checking.
Next Steps
Congratulations! You’ve created your first Telegram bot with grammY. Here’s what to explore next:- Learn about Context and all the methods available
- Explore Middleware for handling complex logic
- Check out Plugins to extend your bot’s capabilities
- Learn about Deployment options for production
Resources
- grammY Documentation - Comprehensive guides and tutorials
- API Reference - Complete API documentation
- grammY Examples - Real-world bot examples
- Telegram Chat - Get help from the community
- Telegram Bot API - Official API documentation