Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

telegraf

> 编程语言
Open source

Modern Telegram Bot Framework for Node.js

9.2K stars0 likes0 views
WebsiteGitHub

About

Modern Telegram Bot Framework for Node.js

For 3.x users

  • 3.x docs
  • 4.0 release notes

Introduction

Bots are special Telegram accounts designed to handle messages automatically. Users can interact with bots by sending them command messages in private or group chats. These accounts serve as an interface for code running somewhere on your server.

Telegraf is a library that makes it simple for you to develop your own Telegram bots using JavaScript or TypeScript.

Features

  • Full Telegram Bot API 7.1 support
  • Excellent TypeScript typings
  • Lightweight
  • AWS λ / Firebase / Glitch / Fly.io / Whatever ready
  • http/https/fastify/Connect.js/express.js compatible webhooks
  • Extensible

Example

javascript
const { Telegraf } = require('telegraf')
const { message } = require('telegraf/filters')

const bot = new Telegraf(process.env.BOT_TOKEN)
bot.start((ctx) => ctx.reply('Welcome'))
bot.help((ctx) => ctx.reply('Send me a sticker'))
bot.on(message('sticker'), (ctx) => ctx.reply(''))
bot.hears('hi', (ctx) => ctx.reply('Hey there'))
bot.launch()

// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))
javascript
const { Telegraf } = require('telegraf')

const bot = new Telegraf(process.env.BOT_TOKEN)
bot.command('oldschool', (ctx) => ctx.reply('Hello'))
bot.command('hipster', Telegraf.reply('λ'))
bot.launch()

// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))

For additional bot examples see the new docs repo.

Resources

  • Getting started
  • API reference
  • Telegram groups (sorted by number of members):
    • English
    • Russian
    • Uzbek
    • Ethiopian
  • GitHub Discussions
  • Dependent repositories

Getting started

Telegram token

To use the Telegram Bot API, you first have to get a bot account by chatting with BotFather.

BotFather will give you a token, something like 123456789:AbCdefGhIJKlmNoPQRsTUVwxyZ.

Installation

shellscript
$ npm install telegraf

or

shellscript
$ yarn add telegraf

or

shellscript
$ pnpm add telegraf

Telegraf class

Telegraf instance represents your bot. It's responsible for obtaining updates and passing them to your handlers.

Start by listening to commands and launching your bot.

Context class

ctx you can see in every example is a Context instance. Telegraf creates one for each incoming update and passes it to your middleware. It contains the update, botInfo, and telegram for making arbitrary Bot API requests, as well as shorthand methods and getters.

This is probably the class you'll be using the most.

Shorthand methods

…

Production

Webhooks

…

Use createWebhook() if you want to attach Telegraf to an existing http server.

typescript
import { createServer } from "http";

createServer(await bot.createWebhook({ domain: "example.com" })).listen(3000);
typescript
import { createServer } from "https";

createServer(tlsOptions, await bot.createWebhook({ domain: "example.com" })).listen(8443);
  • AWS Lambda example integration
  • Google Cloud Functions example integration
  • express example integration
  • fastify example integration
  • koa example integration
  • NestJS framework integration module
  • Cloudflare Workers integration module
  • Use bot.handleUpdate to write new integrations

Error handling

If middleware throws an error or times out, Telegraf calls bot.handleError. If it rethrows, update source closes, and then the error is printed to console and process terminates. If it does not rethrow, the error is swallowed.

Default bot.handleError always rethrows. You can overwrite it using bot.catch if you need to.

⚠️ Swallowing unknown errors might leave the process in invalid state!

ℹ️ In production, systemd or pm2 can restart your bot if it exits for any reason.

Advanced topics

Working with files

Supported file sources:

  • Existing file_id
  • File path
  • Url
  • Buffer
  • ReadStream

Also, you can provide an optional name of a file as filename when you send the file.

…

Middleware

In addition to ctx: Context, each middleware receives next: () => Promise<void>.

As in Koa and some other middleware-based libraries, await next() will call next middleware and wait for it to finish:

typescript
import { Telegraf } from 'telegraf';
import { message } from 'telegraf/filters';

const bot = new Telegraf(process.env.BOT_TOKEN);

bot.use(async (ctx, next) => {
  console.time(`Processing update ${ctx.update.update_id}`);
  await next() // runs next middleware
  // runs after next middleware finishes
  console.timeEnd(`Processing update ${ctx.update.update_id}`);
})

bot.on(message('text'), (ctx) => ctx.reply('Hello World'));
bot.launch();

// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));

With this simple ability, you can:

  • extract information from updates and then await next() to avoid disrupting other middleware,
  • like Composer and Router, await next() for updates you don't wish to handle,
  • like session and Scenes, extend the context by mutating ctx before await next(),
  • intercept API calls,
  • reuse other people's code,
  • do whatever you come up with!

Usage with TypeScript

Telegraf is written in TypeScript and therefore ships with declaration files for the entire library. Moreover, it includes types for the complete Telegram API via the typegram package. While most types of Telegraf's API surface are self-explanatory, there's some notable things to keep in mind.

Extending Context

The exact shape of ctx can vary based on the installed middleware. Some custom middleware might register properties on the context object that Telegraf is not aware of. Consequently, you can change the type of ctx to fit your needs in order for you to have proper TypeScript types for your data. This is done through Generics:

typescript
import { Context, Telegraf } from 'telegraf'

// Define your own context type
interface MyContext extends Context {
  myProp?: string
  myOtherProp?: number
}

// Create your bot and tell it about your context type
const bot = new Telegraf<MyContext>('SECRET TOKEN')

// Register middleware and launch your bot as usual
bot.use((ctx, next) => {
  // Yay, `myProp` is now available here as `string | undefined`!
  ctx.myProp = ctx.chat?.first_name?.toUpperCase()
  return next()
})
// ...

Issues· 76 open

View all issuesOpen on GitHub
  • #2096

    ctx.telegram is a different Telegram instance than bot.telegram, so instrumentation applied at startup silently never runs

    Updated Sep 23, 2026
  • #2058

    Improve error tracing in TelegramError for debugging purposes

    Updated Sep 21, 2026
  • #2094

    reply_parameters silently dropped from multipart requests (sendVideo/sendPhoto with uploaded media) in 4.16.3 — fix exists but was never released

    Updated Sep 7, 2026
  • #1980

    [FR] Allow change fetch timeout

    Updated May 10, 2026
  • #2055

    Session is undefined if no defaultSession is used

    v5Updated May 10, 2026
  • #2050

    Support API v8.0

    Updated May 10, 2026
  • #2067

    Use fresh version of @telegraf/types

    Updated May 7, 2026
  • #2082

    Remove `node-fetch` dependency in favor of native `fetch`

    Updated May 5, 2026
  • #894

    Type defenition of ctx.scene.state

    help wantedinvestigatescenesUpdated May 5, 2026
  • #1827

    The typing of class `SceneContextScene` is incorrect

    enhancementUpdated May 5, 2026

> Tags

TypeScriptbotbot-apibot-frameworkhacktoberfest

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 29, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言