{"id":28239442,"url":"https://github.com/idchlife/node-telegram-bot-api-middleware","last_synced_at":"2025-06-11T07:31:19.747Z","repository":{"id":53600762,"uuid":"59349752","full_name":"idchlife/node-telegram-bot-api-middleware","owner":"idchlife","description":"Built better telegram bots faster and with ease!","archived":false,"fork":false,"pushed_at":"2021-02-09T12:32:39.000Z","size":13,"stargazers_count":61,"open_issues_count":1,"forks_count":10,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-05-19T02:12:28.773Z","etag":null,"topics":["bot","middleware","telegram"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/idchlife.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-21T08:29:26.000Z","updated_at":"2025-01-10T16:42:28.000Z","dependencies_parsed_at":"2022-09-14T17:02:25.050Z","dependency_job_id":null,"html_url":"https://github.com/idchlife/node-telegram-bot-api-middleware","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idchlife%2Fnode-telegram-bot-api-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idchlife%2Fnode-telegram-bot-api-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idchlife%2Fnode-telegram-bot-api-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idchlife%2Fnode-telegram-bot-api-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/idchlife","download_url":"https://codeload.github.com/idchlife/node-telegram-bot-api-middleware/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/idchlife%2Fnode-telegram-bot-api-middleware/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259221916,"owners_count":22823983,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bot","middleware","telegram"],"created_at":"2025-05-19T02:12:27.830Z","updated_at":"2025-06-11T07:31:19.736Z","avatar_url":"https://github.com/idchlife.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/idchlife/node-telegram-bot-api-middleware.svg?branch=master)](https://travis-ci.org/idchlife/node-telegram-bot-api-middleware)\n\nUsing middleware for node-telegram-bot-api from yagop: https://github.com/yagop/node-telegram-bot-api\n\n## Why?\nSo, you happened to be here. And I assume you like writing telegram bots using **yagop**-s library node-telegram-bot-api.\nBut sometimes you end making multiple promises, generators by hand, async/await constructions and all that.\nI present you the way of using power of generators and **co** library by **tj** by adding middleware before executing bot message callback\n\n## Installation\n\n    npm i node-telegram-bot-api-middleware --save\n\nThen you can use it like this:\n\n```js\n    const TelegramBot = require('node-telegram-bot-api');\n    const bot = new TelegramBot(TOKEN, { polling: true });\n    const use = require('node-telegram-bot-api-middleware').use;\n    \n    // Simple middleware that adds random method to your context\n    function randomMiddleware() {\n      this.random = (number) =\u003e Math.floor(Math.random() * number)\n    }\n    \n    let response = use(randomMiddleware);\n    \n    bot.onText(/\\/command/, response(function() {\n      bot.sendMessage(this.chatId, this.random(10));\n    });\n    \n    // or\n    response = response.use(function() {\n      bot.sendMessage(this.chatId, this.random());\n    });\n    \n    bot.onText(/\\/command/, response);\n    \n    // or\n    \n    response = response(function() {\n      bot.sendMessage(this.chatId, this.random());\n    });\n    \n    bot.onText(/\\/command/, response);\n```\n\n## Available at the moment middleware\n - **simpleauth** - https://github.com/idchlife/node-telegram-bot-api-middleware-simpleauth\n\n## Usage\n\n```js\n  const use = require('node-telegram-bot-api-middleware').use;\n  // Your configured bot\n  const bot = require('./bot');\n  \n  // You can use simple functions\n  function middleware2() {\n    // You will already have msg, chatId in context. This is achieved by added already inside\n    // library default middleware, that populates context with those things, as well as method .stop()\n    // (see further down about .stop)\n    \n    this.quickResponse = function* (text) {\n      yield bot.sendMessage(this.chatId, text);\n    }.bind(this);\n  }\n  \n  // You can use generators\n  function* middleware2() {\n    yield this.quickResponse('You wrote something to this bot!');\n    \n    console.log('Answer sent');\n  }\n  \n  // You cannot use short functions if you're going to use context passed from\n  // previous middleware. `This wont't work: `\n  const notWorkingResponse = use(() =\u003e { console.log(this.msg.chat.id); });\n  \n  // Be aware of adding middlewares in proper order,\n  // because they will be executed in order in which you added them\n  const response = use(middleware).use(middleware2);\n  \n  // You can also add more middleware to variable that already has set of middlewares\n  // Adding more, it will create new set of middleware, not affecting old set of\n  // middlewares. response still will have 2 middlewares.\n  const checkAuth = response.use(function() {\n    // Imagine, this also can be method from other middleware,\n    // e.g.: this.isAuthenticated()\n    const userIsAuthenticated = false;\n    \n    if (!userIsAuthenticated) {\n      this.quickResponse('You are not authenticated to do that');\n      \n      // If you want to prevent executing next middlewares, use .stop()\n      this.stop();\n    }\n  });\n  \n  bot.onText(/\\/need_auth/, checkAuth(function() {\n    // Give some info only to authenticated user\n  }));\n```\n\n### Handling errors.\n\nDefault error handler built in will console.error all errors from middleware and proceed to next one.\n\nYou 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()):\n\n```js\n  const middleware = require('middleware');\n  const use = middleware.use;\n  \n  middleware.setErrorHandler(err =\u003e {\n    // Your own logic for handling errors\n  });\n```\n\nSometime you might need to set custom error handler to your middleware.\nThis done line this:\n\n```js\n  function yourCustomMiddleware() {\n    this.methodWithPossibilityOfErrorOccuring();\n  }\n  \n  yourCustomMiddleware.onErrorHandler = err =\u003e {\n    // Log or do some other things with error\n  }\n```\n\n## How does it work\n\n**use** - is just a function, that returns another function, that accepts middleware as arguments or object with\nmessage data on bot.onText executiong. It also has .use method, that is just copy of function itself. Useful when\nwriting code like use(middleware).use(middleware)(yourCallbackFunction)\n\n\nBasically you can write even like this:\n\n```js\n    use(middleware)(middleware).use(middleware)(botCallbackArguments); // botCallbackArguments will be passed by bot, and executed function will be also by bot.\n```\n\nFor more information on this topic look into index.js file. There are many comments explaining how does it work.\n\n\n## Help yourself and everyone build better bots!\n\nAs 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.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidchlife%2Fnode-telegram-bot-api-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fidchlife%2Fnode-telegram-bot-api-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidchlife%2Fnode-telegram-bot-api-middleware/lists"}