Top Related Projects
Modern Telegram Bot Framework for Node.js
Telegram Bot API for NodeJS
We have made you a wrapper you can't refuse
aiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio
Cross-platform library for building Telegram clients
Golang bindings for the Telegram Bot API
Quick Overview
grammY is a modern and feature-rich framework for building Telegram bots in TypeScript and JavaScript. It offers a simple yet powerful API, excellent TypeScript support, and a wide range of plugins to extend functionality.
Pros
- Strong TypeScript support with comprehensive type definitions
- Extensive plugin ecosystem for easy feature expansion
- Built-in support for long polling and webhooks
- Excellent documentation and active community support
Cons
- Steeper learning curve compared to some simpler bot frameworks
- May be overkill for very basic bot projects
- Requires Node.js environment, which may not be suitable for all deployment scenarios
Code Examples
- Creating a simple echo bot:
import { Bot } from "grammy";
const bot = new Bot("BOT_TOKEN");
bot.on("message", (ctx) => ctx.reply(ctx.message.text));
bot.start();
- Using middleware for authentication:
import { Bot, Context } from "grammy";
const bot = new Bot("BOT_TOKEN");
function authenticate(ctx: Context, next: () => Promise<void>) {
if (ctx.from?.id === ADMIN_USER_ID) {
return next();
}
ctx.reply("Unauthorized");
}
bot.use(authenticate);
bot.command("admin", (ctx) => ctx.reply("Welcome, admin!"));
bot.start();
- Handling inline keyboards:
import { Bot, InlineKeyboard } from "grammy";
const bot = new Bot("BOT_TOKEN");
bot.command("start", async (ctx) => {
const keyboard = new InlineKeyboard().text("Click me!", "button-1");
await ctx.reply("Welcome!", { reply_markup: keyboard });
});
bot.callbackQuery("button-1", async (ctx) => {
await ctx.answerCallbackQuery("You clicked the button!");
});
bot.start();
Getting Started
To get started with grammY, follow these steps:
-
Install grammY:
npm install grammy
-
Create a new bot file (e.g.,
bot.ts
):import { Bot } from "grammy"; const bot = new Bot("BOT_TOKEN"); bot.command("start", (ctx) => ctx.reply("Hello, I'm a grammY bot!")); bot.start();
-
Replace
"BOT_TOKEN"
with your actual bot token obtained from the BotFather. -
Run your bot:
npx ts-node bot.ts
For more advanced usage and features, refer to the official grammY documentation.
Competitor Comparisons
Modern Telegram Bot Framework for Node.js
Pros of Telegraf
- Larger community and ecosystem with more plugins and middleware
- More mature project with longer development history
- Extensive documentation and examples
Cons of Telegraf
- Slightly more complex API compared to grammY
- Less focus on TypeScript, though still supported
- Larger bundle size
Code Comparison
Telegraf:
const { Telegraf } = require('telegraf');
const bot = new Telegraf(process.env.BOT_TOKEN);
bot.command('start', (ctx) => ctx.reply('Welcome!'));
bot.on('text', (ctx) => ctx.reply('You said: ' + ctx.message.text));
bot.launch();
grammY:
import { Bot } from "grammy";
const bot = new Bot(process.env.BOT_TOKEN);
bot.command("start", (ctx) => ctx.reply("Welcome!"));
bot.on("message:text", (ctx) => ctx.reply(`You said: ${ctx.message.text}`));
bot.start();
Both frameworks offer similar functionality for creating Telegram bots. Telegraf has a more established ecosystem, while grammY focuses on TypeScript support and a simpler API. The code examples show that both frameworks have similar syntax for basic bot operations, with grammY using more modern JavaScript features and TypeScript by default.
Telegram Bot API for NodeJS
Pros of node-telegram-bot-api
- Mature and well-established library with a large user base
- Extensive documentation and community support
- Supports a wide range of Telegram Bot API features
Cons of node-telegram-bot-api
- Less type-safe compared to grammY's TypeScript-first approach
- May require more boilerplate code for common tasks
- Lacks some modern features and optimizations present in grammY
Code Comparison
node-telegram-bot-api:
const TelegramBot = require('node-telegram-bot-api');
const bot = new TelegramBot(token, {polling: true});
bot.on('message', (msg) => {
bot.sendMessage(msg.chat.id, 'Hello!');
});
grammY:
import { Bot } from "grammy";
const bot = new Bot(token);
bot.command("start", (ctx) => ctx.reply("Hello!"));
bot.start();
The code comparison shows that grammY offers a more concise and type-safe approach to bot development. It uses modern ES6+ syntax and provides built-in methods for common commands, while node-telegram-bot-api requires more manual setup and event handling.
Both libraries are capable of creating functional Telegram bots, but grammY's focus on TypeScript and modern JavaScript features may appeal to developers looking for a more streamlined and type-safe experience. node-telegram-bot-api, on the other hand, offers a tried-and-tested solution with extensive community support and documentation.
We have made you a wrapper you can't refuse
Pros of python-telegram-bot
- Mature and well-established library with a large community
- Comprehensive documentation and extensive examples
- Supports both synchronous and asynchronous programming
Cons of python-telegram-bot
- Larger codebase and potentially steeper learning curve
- May have slower performance compared to grammY's TypeScript implementation
- Less focus on modern features like webhooks and inline queries
Code Comparison
python-telegram-bot:
from telegram.ext import Updater, CommandHandler
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Hello!")
updater = Updater(token='YOUR_TOKEN', use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.start_polling()
grammY:
import { Bot } from "grammy";
const bot = new Bot("YOUR_TOKEN");
bot.command("start", (ctx) => ctx.reply("Hello!"));
bot.start();
Both libraries offer similar functionality, but grammY's TypeScript implementation provides a more concise and modern approach. python-telegram-bot offers more flexibility with its support for both synchronous and asynchronous programming, while grammY focuses on a streamlined, performance-oriented design. The choice between the two depends on the developer's preferred language and specific project requirements.
aiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio
Pros of aiogram
- Written in Python, which may be more familiar to some developers
- Extensive documentation and active community support
- Built-in support for asyncio, allowing for efficient handling of multiple updates
Cons of aiogram
- Slightly more verbose syntax compared to grammY
- Less type safety than TypeScript-based grammY
- May have a steeper learning curve for developers new to asyncio
Code Comparison
aiogram:
from aiogram import Bot, Dispatcher, types
bot = Bot(token="YOUR_BOT_TOKEN")
dp = Dispatcher(bot)
@dp.message_handler(commands=['start'])
async def send_welcome(message: types.Message):
await message.reply("Welcome!")
grammY:
import { Bot } from "grammy";
const bot = new Bot("YOUR_BOT_TOKEN");
bot.command("start", (ctx) => ctx.reply("Welcome!"));
bot.start();
Both frameworks offer similar functionality, but grammY's syntax is more concise. aiogram requires explicit handling of asynchronous operations, while grammY abstracts this away. grammY also benefits from TypeScript's type checking, potentially reducing runtime errors. However, aiogram's Python-based approach may be more accessible to developers already familiar with the language and its ecosystem.
Cross-platform library for building Telegram clients
Pros of td
- Native C++ implementation, offering high performance and low-level control
- Comprehensive support for all Telegram API features
- Cross-platform compatibility, including mobile platforms
Cons of td
- Steeper learning curve due to C++ complexity
- Requires more setup and configuration compared to grammY
- Less beginner-friendly documentation
Code Comparison
td (C++):
auto client = td::Client::create();
client->send({td_api::make_object<td_api::setAuthenticationPhoneNumber>
(phone_number, nullptr)});
grammY (TypeScript):
const bot = new Bot(process.env.BOT_TOKEN);
bot.command("start", (ctx) => ctx.reply("Hello!"));
bot.start();
Key Differences
- td provides a lower-level API, while grammY offers a higher-level abstraction
- grammY is specifically designed for building Telegram bots, while td is a more general-purpose Telegram client library
- td requires compilation and linking, whereas grammY can be easily installed via npm and run with Node.js
Use Cases
- td: Complex applications requiring full Telegram API access and performance optimization
- grammY: Rapid development of Telegram bots with a focus on simplicity and ease of use
Golang bindings for the Telegram Bot API
Pros of telegram-bot-api
- Written in Go, offering high performance and concurrency
- Lightweight and efficient, suitable for resource-constrained environments
- Provides a low-level API, giving developers more control over bot interactions
Cons of telegram-bot-api
- Less abstraction and higher-level features compared to grammY
- Smaller ecosystem and fewer built-in utilities
- Steeper learning curve for developers new to Go or low-level bot development
Code Comparison
telegram-bot-api:
bot, err := tgbotapi.NewBotAPI("YOUR_TOKEN")
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := bot.GetUpdatesChan(u)
for update := range updates {
// Handle update
}
grammY:
const bot = new Bot("YOUR_TOKEN");
bot.command("start", (ctx) => ctx.reply("Hello!"));
bot.on("message", (ctx) => ctx.reply("Got your message!"));
bot.start();
The telegram-bot-api example shows a more low-level approach, requiring manual update handling. grammY provides a higher-level abstraction with built-in command and event handling, simplifying bot development for many use cases.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
The Telegram Bot Framework
grammY makes it easy to create Telegram bots. Both for beginners and at scale.
You want grammY because it is easy to use. It is very powerful and always up to date. It has the best documentation in town. It is extremely efficient and scales up effortlessly. It has a thriving ecosystem of plugins, a friendly community chat, seamless integrations with web frameworks and databases, and so much more.
Are you ready? ð¤ð
Bots are written in TypeScript (or JavaScript) and run on Node.js or Deno.
Quickstart
If you are new to Telegram bots, read the official Introduction for Developers written by the Telegram team.
Visit @BotFather and create a new bot. You will obtain a bot token.
Create a new directory and run
npm install grammy
inside it. Then create a file bot.js
with this content:
const { Bot } = require("grammy");
// Create a bot object
const bot = new Bot(""); // <-- place your bot token in this string
// Register listeners to handle messages
bot.on("message:text", (ctx) => ctx.reply("Echo: " + ctx.message.text));
// Start the bot (using long polling)
bot.start();
Now you can run the bot via
node bot.js
and it will echo all received text messages.
Congrats! You just wrote a Telegram bot :)
Going Further
grammY has an excellent documentation, and an API Reference. It even integrates with your code editor, e.g. VS Code. You can hover over any element of grammY to get a detailed description of what that thing does or means.
If you are still stuck, just join the Telegram chat and ask for help. People are nice there and we appreciate your question, no matter what it is :)
Here are some more resources to support you:
Resources
grammY website
âmain project website and documentation. Gets you started and explains all concepts.
grammY API reference
âreference of everything that grammY exports. Useful to look up descriptions about any element of grammY.
grammY examples
ârepository full of example bots. Includes a setup to easily run any of them.
Awesome grammY
âlist of awesome projects built with grammY. Helpful if you want to see some real-world usage.
grammY chat
âThe chat where you can ask any question about grammY or bots in general. We are also open for feedback, ideas, and contributions!
The Russian community chat can be found here.
grammY news
âThe channel where updates to grammY and the ecosystem are posted. We are also on Twitter.
Telegram Bot API Reference
âdocumentation of the API that Telegram offers, and that grammY connects to under the hood.
Deno Support
All grammY packages published by @grammyjs run natively on Deno. We are compiling every codebase to still run on Node.js.
However, given that most bot developers are still using Node.js, all documentation is written Node.js-first. We may migrate it if Deno overtakes Node.js. If you are already on Deno today, import grammY from https://deno.land/x/grammy/mod.ts
.
You may also be interested in why we support Deno.
JavaScript Bundles
The grammY core package in this repository is available as a JavaScript bundle via https://bundle.deno.dev/.
This lets you transpile all published versions including current main
branch to standalone JavaScript files.
For example, the most recent source on main
is available from https://bundle.deno.dev/https://raw.githubusercontent.com/grammyjs/grammY/main/src/mod.ts.
Being compatible with browsers is especially useful for running bots on Cloudflare Workers.
For this reason, we also include a web bundle in our npm package.
You can simply do import { Bot } from "grammy/web"
.
Contribution Guide »
Contributors â¨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Top Related Projects
Modern Telegram Bot Framework for Node.js
Telegram Bot API for NodeJS
We have made you a wrapper you can't refuse
aiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio
Cross-platform library for building Telegram clients
Golang bindings for the Telegram Bot API
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot