Node Telegram Bot Api Middleware

Built better telegram bots faster and with ease!
Alternatives To Node Telegram Bot Api Middleware
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Freqtrade21,153211 hours ago42July 03, 202264gpl-3.0Python
Free, open source crypto trading bot
Node Telegram Bot Api6,9871,94028312 hours ago60August 29, 202265mitJavaScript
Telegram Bot API for NodeJS
Pytelegrambotapi6,687865593 days ago103June 23, 20223gpl-2.0Python
Python Telegram bot api.
Telegraf6,5616872794 days ago211September 13, 202221mitTypeScript
Modern Telegram Bot Framework for Node.js
23 days ago10
悄咪咪收集的1000+个Telegram群合集,如果有更多好玩的telegram群,欢迎在 issue 提出或者pull requests
5 hours ago21June 20, 20229mitPython
跨平台 Python 异步聊天机器人框架 / Asynchronous multi-platform chatbot framework written in Python
Telegrambots3,55547918a month ago57June 21, 2022166mitJava
Java library to create bots using Telegram Bots API
Aiogram3,47535454 days ago75June 21, 2022103mitPython
aiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio
Core3,4732723a month ago91July 03, 202249mitPHP
PHP Telegram Bot based on the official Telegram Bot API
11 days ago76mitPHP
Freenom 域名自动续期。Freenom domain name renews automatically.
Alternatives To Node Telegram Bot Api Middleware
Select To Compare

Alternative Project Comparisons

Build Status

Using middleware for node-telegram-bot-api from yagop: yagop/node-telegram-bot-api


So, you happened to be here. And I assume you like writing telegram bots using yagop-s library node-telegram-bot-api. But sometimes you end making multiple promises, generators by hand, async/await constructions and all that. I present you the way of using power of generators and co library by tj by adding middleware before executing bot message callback


npm i node-telegram-bot-api-middleware --save

Then you can use it like this:

    const TelegramBot = require('node-telegram-bot-api');
    const bot = new TelegramBot(TOKEN, { polling: true });
    const use = require('node-telegram-bot-api-middleware').use;
    // Simple middleware that adds random method to your context
    function randomMiddleware() {
      this.random = (number) => Math.floor(Math.random() * number)
    let response = use(randomMiddleware);
    bot.onText(/\/command/, response(function() {
      bot.sendMessage(this.chatId, this.random(10));
    // or
    response = response.use(function() {
      bot.sendMessage(this.chatId, this.random());
    bot.onText(/\/command/, response);
    // or
    response = response(function() {
      bot.sendMessage(this.chatId, this.random());
    bot.onText(/\/command/, response);

Available at the moment middleware


  const use = require('node-telegram-bot-api-middleware').use;
  // Your configured bot
  const bot = require('./bot');
  // You can use simple functions
  function middleware2() {
    // You will already have msg, chatId in context. This is achieved by added already inside
    // library default middleware, that populates context with those things, as well as method .stop()
    // (see further down about .stop)
    this.quickResponse = function* (text) {
      yield bot.sendMessage(this.chatId, text);
  // You can use generators
  function* middleware2() {
    yield this.quickResponse('You wrote something to this bot!');
    console.log('Answer sent');
  // You cannot use short functions if you're going to use context passed from
  // previous middleware. `This wont't work: `
  const notWorkingResponse = use(() => { console.log(; });
  // Be aware of adding middlewares in proper order,
  // because they will be executed in order in which you added them
  const response = use(middleware).use(middleware2);
  // You can also add more middleware to variable that already has set of middlewares
  // Adding more, it will create new set of middleware, not affecting old set of
  // middlewares. response still will have 2 middlewares.
  const checkAuth = response.use(function() {
    // Imagine, this also can be method from other middleware,
    // e.g.: this.isAuthenticated()
    const userIsAuthenticated = false;
    if (!userIsAuthenticated) {
      this.quickResponse('You are not authenticated to do that');
      // If you want to prevent executing next middlewares, use .stop()
  bot.onText(/\/need_auth/, checkAuth(function() {
    // Give some info only to authenticated user

Handling errors.

Default error handler built in will console.error all errors from middleware and proceed to next one.

You can also set your own global error handler like this (WARNING: this will replace default error handler, if you need to combine default handler with your - use middleware.getDefaultErrorHandler()):

  const middleware = require('middleware');
  const use = middleware.use;
  middleware.setErrorHandler(err => {
    // Your own logic for handling errors

Sometime you might need to set custom error handler to your middleware. This done line this:

  function yourCustomMiddleware() {
  yourCustomMiddleware.onErrorHandler = err => {
    // Log or do some other things with error

How does it work

use - is just a function, that returns another function, that accepts middleware as arguments or object with message data on bot.onText executiong. It also has .use method, that is just copy of function itself. Useful when writing code like use(middleware).use(middleware)(yourCallbackFunction)

Basically you can write even like this:

    use(middleware)(middleware).use(middleware)(botCallbackArguments); // botCallbackArguments will be passed by bot, and executed function will be also by bot.

For more information on this topic look into index.js file. There are many comments explaining how does it work.

Help yourself and everyone build better bots!

As you can see, this is cool opportunity to create many different middleware libraries for different purposes. For database connection, for special file uploaders or many other things. Join the opensource! (if you did not yet) Create middleware for some other libraries or for your special case and share with everyone! Create an issue or send pull request and I will add link to your middleware in the list.

Popular Telegram Projects
Popular Telegram Bot Projects
Popular Messaging Categories
Related Searches

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Telegram Bot