
Using middleware for node-telegram-bot-api from yagop: https://github.com/yagop/node-telegram-bot-api
Why?
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
Installation
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;
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));
});
response = response.use(function() {
bot.sendMessage(this.chatId, this.random());
});
bot.onText(/\/command/, response);
response = response(function() {
bot.sendMessage(this.chatId, this.random());
});
bot.onText(/\/command/, response);
Available at the moment middleware
Usage
const use = require('node-telegram-bot-api-middleware').use;
const bot = require('./bot');
function middleware2() {
this.quickResponse = function* (text) {
yield bot.sendMessage(this.chatId, text);
}.bind(this);
}
function* middleware2() {
yield this.quickResponse('You wrote something to this bot!');
console.log('Answer sent');
}
const notWorkingResponse = use(() => { console.log(this.msg.chat.id); });
const response = use(middleware).use(middleware2);
const checkAuth = response.use(function() {
const userIsAuthenticated = false;
if (!userIsAuthenticated) {
this.quickResponse('You are not authenticated to do that');
this.stop();
}
});
bot.onText(/\/need_auth/, checkAuth(function() {
}));
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 => {
});
Sometime you might need to set custom error handler to your middleware.
This done line this:
function yourCustomMiddleware() {
this.methodWithPossibilityOfErrorOccuring();
}
yourCustomMiddleware.onErrorHandler = err => {
}
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);
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.