telebot-middleware v0.2.2
telebot-middleware
A modification of telebotframework by Suppen that adds middlewares
How to initialize a middleware bot
telebot-middleware exports a function.
This function takes a bot as a parameter, and converts it to a middleware bot.
The following code initializes a middleware bot
const TelegramBot = require("telebotframework").TelegramBot;
const telebotMiddleware = require("telebot-middleware");
let bot = TelegramBot("<token>");
telebotMiddleware(bot);You can now register middlewares to the bot by calling the method name on the bot relating to the message type.
bot
.command(/^\/start$/, (message, next) => {
...
})
.text((message, next) => {
...
});These are the currently supported message types
Command
Command let's you specify a pattern, received text messages will be matched against.
The command method takes two parameters, a RegExp or a string as a pattern, and a callback function.
On the first parameter passed to the callback, message, the property command will be the messages content.
Text
Text let's you receive only messages of the text type.
The text method takes one paramter, a callback function.
On the first parameter passed to the callback, message, the property text will be the messages content.
Document
Document let's you receive only messages of the document type.
The document method takes one paramter, a callback function.
On the first parameter passed to the callback, message, the property document will be the messages content.
Photo
Photo let's you receive only messages of the photo type.
The photo method takes one paramter, a callback function.
On the first parameter passed to the callback, message, the property photo will be the messages content.
Message
Message let's you receive any messages.
The message method takes one paramter, a callback function.
Planned features
- Get and Set methods for message, allowing middlewares to attach persistent data to message.
Changelog
#####0.2.0:
- Message object sent to middleware is now a copy. Changes made to message will not persist.
- Content appropriate to the message type will now be attached to message object.
- Updates README
#####0.1.2:
- Message middleware is now properly implemented, where it would never be called before.
#####0.1.0:
- Added support for command, text, document, photo and message middlewares.