Telegraf.js is a modern, lightweight, and extensible framework for developing Telegram bots using Node.js. It provides full support for the Telegram Bot API, currently at version 7.1 (as of v4.16.0), offering a comprehensive set of tools for handling messages, commands, and various update types. The library is actively maintained, with frequent minor releases incorporating new Bot API features and bug fixes, ensuring compatibility with the latest Telegram platform capabilities. Key differentiators include excellent TypeScript typings, compatibility with serverless environments like AWS Lambda and Firebase Functions, and support for various webhook setups (HTTP/HTTPS, Fastify, Express). It aims for simplicity and extensibility, allowing developers to build complex bot logic efficiently. The current stable version is 4.16.3.
npm install telegrafVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes a Telegraf bot with a token from environment variables, sets up basic command handlers for /start and /help, reacts to stickers, and echoes text messages. It demonstrates event listeners and graceful shutdown.
Refer to the v4.0.0 release notes and migration guide for specific changes. Update middleware registration, context access, and error handling patterns. Full TypeScript rewrite impacts type declarations.
Ensure all asynchronous middleware functions correctly `await` their operations. Enable `EXPERIMENTAL_SESSION_CHECKS=1` in development to catch these bugs proactively. Always return promises from async middleware if not explicitly `await`ing them.
Upgrade to Telegraf v4.15.3 or newer to get fixes for media upload and thumbnail handling. Always validate file paths and ensure correct media object structures are provided to API methods.
Update imports to use `import { filterName } from 'telegraf/filters'` (ESM) or `const { filterName } = require('telegraf/filters')` (CommonJS).Ensure you have integrated a session middleware (e.g., `telegraf-session-local` or a custom one) and added it to your bot's middleware chain (`bot.use(sessionMiddleware)`). Also, check for missing `await` keywords in async middleware functions interacting with `ctx.session`.
For CommonJS, use `const { Telegraf } = require('telegraf')`. For ES Modules, use `import { Telegraf } from 'telegraf'`. Ensure your `package.json` `type` field is correctly set to 'module' for ESM or omitted/set to 'commonjs' for CJS.Double-check your `BOT_TOKEN` environment variable or hardcoded string. Ensure it's the exact token provided by BotFather. Generate a new token if you suspect the current one is compromised or invalid.
Ensure the code accessing `ctx.reply` is within a middleware or handler function properly receiving a `Context` object from Telegraf. In TypeScript, verify `ctx` is typed as `Context` or a more specific `ComposerContext`.
No dependency data recorded yet.