Convert Figma logo to code with AI

grammyjs logogrammY

The Telegram Bot Framework.

2,172
110
2,172
14

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

4,591

aiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio

6,953

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

  1. 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();
  1. 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();
  1. 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:

  1. Install grammY:

    npm install grammy
    
  2. 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();
    
  3. Replace "BOT_TOKEN" with your actual bot token obtained from the BotFather.

  4. 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.

4,591

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.

6,953

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 Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

grammY

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):

KnorpelSenf
KnorpelSenf

🤔 💻 📖 🎨 💡 ⚠️ 🔌 📦 👀 🧑‍🏫 📆 🚇 🔊 ️️️️♿️
Heero
Heero

🔌 📓 💡 📖 👀 💻 🤔
Wojciech Pawlik
Wojciech Pawlik

🤔 👀 🚇 📦 🔧
Alessandro Bertozzi
Alessandro Bertozzi

📖
trgwii
trgwii

💻 👀
KnightNiwrem
KnightNiwrem

💻 🐛 🔌 📖 💡 👀 🧑‍🏫
Muthu Kumar
Muthu Kumar

👀
EdJoPaTo
EdJoPaTo

🔌 📖 🤔 👀 🐛 💻
Amir Zouerami
Amir Zouerami

📖 🔌 💡
Roj
Roj

📖 👀 🚇 🌍 💻 🤔 🧑‍🏫 💡
jokasimr
jokasimr

🐛
Ciki Momogi
Ciki Momogi

📖 🌍
AndreoliBR
AndreoliBR

👀
Kirill Loskutov
Kirill Loskutov

📖 🐛 🤔 🎨 💬 👀 💻 🔌
Andrew Lane
Andrew Lane

🐛 👀
code-withAshish
code-withAshish

📖 💬 🐛 👀
Stephane Mensah
Stephane Mensah

🐛 🔌
Asaku01
Asaku01

📖
ppsimn
ppsimn

🐛
Satont
Satont

🔌 📖
deptyped
deptyped

💡 📖 ✅ 🐛 🌍
Jacek Nowacki
Jacek Nowacki

📖 💻 🐛 👀 🤔
Outvi V
Outvi V

💻
Ikko Ashimine
Ikko Ashimine

📖
Yevhen Denesiuk
Yevhen Denesiuk

👀 🐛 💻
prastian
prastian

🐛 💻
Sayem Chowdhury
Sayem Chowdhury

🤔
kospra
kospra

🤔 💻
Chimit
Chimit

📖
Calsi
Calsi

📖
Jonas Zohren
Jonas Zohren

🐛 💻
linbuxiao
linbuxiao

📖 🌍
JiquanWang99
JiquanWang99

📖 🌍
Borhan Hafez
Borhan Hafez

🔌
WingLim
WingLim

📖 🌍 💻 🔌 🤔
taotie111
taotie111

📖 🌍
Merlin
Merlin

📖
Darvesh
Darvesh

🐛 💻 👀
dcdunkan
dcdunkan

🐛 💻 🔌 👀 📖 🤔 🚇 🔧 🧑‍🏫 🚧
Kid
Kid

📖 🌍
Slava Fomin II
Slava Fomin II

🐛 📖
Kiko Beats
Kiko Beats

📖
Vsevolod
Vsevolod

💻 🤔 👀
Habemuscode
Habemuscode

👀 📖 🌍 🚧
Nikita Kolmogorov
Nikita Kolmogorov

🔌
Vitaliy Meshchaninov
Vitaliy Meshchaninov

🐛 💻
Дилян Палаузов
Дилян Палаузов

🐛 💻
lmx-Hexagram
lmx-Hexagram

📖
Ilya Semenov
Ilya Semenov

🤔 👀 💻
abdollahzadehAli
abdollahzadehAli

📖 💡
Saeed Nasiri
Saeed Nasiri

📖
Hesoyam
Hesoyam

📖
yrzam
yrzam

🐛
drmikecrowe
drmikecrowe

👀
Martin
Martin

📖 🐛 👀
Pavel
Pavel

💡
Thor 雷神 Schaeff
Thor 雷神 Schaeff

💡
x066it
x066it

🐛 👀
kolay
kolay

👀
Evgeny Nepomnyashchiy
Evgeny Nepomnyashchiy

👀
Ananta Krsna dasa
Ananta Krsna dasa

📖
Mighty Ali
Mighty Ali

💻 👀 🤔
三三
三三

🐛 💻
Roz
Roz

🐛 💻 👀 🚇 🤔 🧑‍🏫 🔌
Dani Haro
Dani Haro

💻 🔌 📖
Ryukaizen
Ryukaizen

📖
Alisher Ortiqov
Alisher Ortiqov

📖
Tony Tkachenko
Tony Tkachenko

📖
Ra
Ra

💻
sartoshi-foot-dao
sartoshi-foot-dao

📖
Yoel Navas E.
Yoel Navas E.

🤔
Vitor Gomes
Vitor Gomes

🐛 💻
Aditya
Aditya

🐛 👀
Udit Karode
Udit Karode

👀
Mike Rockétt
Mike Rockétt

👀 🐛
Srinivasa IK Varanasi
Srinivasa IK Varanasi

💻
abdoo9
abdoo9

🐛 💻 👀 📖
ak4zh
ak4zh

👀 🤔 💻
Nikolay Lapshin
Nikolay Lapshin

💻
Aquatica
Aquatica

📖 💬
Fa Dzikri
Fa Dzikri

👀 📖 🌍
Chandler Lattin
Chandler Lattin

💻 👀 🔌
Sergey Parfenyuk
Sergey Parfenyuk

🐛
Émerson Felinto
Émerson Felinto

🐛
Petr Stankin
Petr Stankin

🐛
Maxim Lebedev
Maxim Lebedev

🤔 💻
Madnex
Madnex

📖
Svyatoslav Tupchienko
Svyatoslav Tupchienko

💻
Vladislav Deryabkin
Vladislav Deryabkin

🐛 💻 👀
Kashyap Sharma
Kashyap Sharma

💡
AlexOwl
AlexOwl

🐛 💻
Shrimadhav U K
Shrimadhav U K

💻
Binamra Lamsal
Binamra Lamsal

🤔
gertminov
gertminov

📖 ✅
Stephan Psaras
Stephan Psaras

🐛
shevernitskiy
shevernitskiy

🐛 👀 💻
mrmaster009
mrmaster009

📖
Andrey Zontov
Andrey Zontov

🐛 💻 💬 🤔 📖 🌍
Abbass Al-Musawi
Abbass Al-Musawi

📖 🐛 💻
ArunR
ArunR

🐛 💻
NDA
NDA

🐛 🤔 💻
MatyiFKBT
MatyiFKBT

📖
Chris Andrew C. L.
Chris Andrew C. L.

🐛 💻 👀
Islam Kiiasov
Islam Kiiasov

💻
Shane Avery Sistoza
Shane Avery Sistoza

🤔 💻 ⚠️
Maicol
Maicol

💻
Nazar Antoniuk
Nazar Antoniuk

📖 🌍 🚧
Aleksei Ivanov
Aleksei Ivanov

👀
Vladislav Ponomarev
Vladislav Ponomarev

⚠️ 💻 📦
Louie Tan
Louie Tan

👀
Leandro Vargas
Leandro Vargas

🐛 💻
Sean Yap
Sean Yap

🐛 💻
Sergey Solovev
Sergey Solovev

🤔 👀
Sree (Taylor's Version)
Sree (Taylor's Version)

🐛 💻
Yaroslav Vovchenko
Yaroslav Vovchenko

🐛 💻
gabe
gabe

👀
Lavrentiy Rubtsov
Lavrentiy Rubtsov

📖
Josh Gillies
Josh Gillies

💻
Uladzislau Hramyka
Uladzislau Hramyka

🐛
Gabriele Belluardo
Gabriele Belluardo

🐛 💻
Dim Chen
Dim Chen

🐛 💻
fwqaaq
fwqaaq

🤔 💻
Janek Szynal
Janek Szynal

🤔
Alexander Mordvinov
Alexander Mordvinov

🤔
Ash
Ash

👀
Winston H.
Winston H.

🤔 💻 👀 ⚠️
Hero Protagonist
Hero Protagonist

💻 🐛 🤔 🔌
Mobin Askari
Mobin Askari

🔌
Ubertao
Ubertao

🤔 💻
Grigory
Grigory

🤔 💻 📖
aleveha
aleveha

💻
barinbritva
barinbritva

👀
Lyudmil Ivanov
Lyudmil Ivanov

💡
lexomis
lexomis

👀
Andrew Sologor
Andrew Sologor

👀
rayz
rayz

💬 🤔 💻
Zaid
Zaid

🔧

This project follows the all-contributors specification. Contributions of any kind welcome!

NPM DownloadsLast 30 Days