Context Middleware
This middleware is designed to be used with node-telegram-bot-api-middleware
.
For each incoming message this middleware adds a custom context object this.ctx
that is shared between all handlers.
Useful for designing complex request processing logic involving custom handler interoperation.
Context can be used to create generic handler that:
- displays help information if there is no any specific handler for the request
- processes arbitrary user text input that is not meant for any specific handler to process. In this case it is best to also use state machines to fully control the bot's logic.
Intallation
npm i node-telegram-bot-api-middleware-context
Usage
const TelegramBot = require('node-telegram-bot-api')
const use = require('node-telegram-bot-api-middleware').use
const createContextMiddleware = require('node-telegram-bot-api-middleware-context')
const bot = new TelegramBot('YOUR_TELEGRAM_BOT_TOKEN', { polling: true })
let response = use(createContextMiddleware())
bot.onText(
new RegExp('/start'),
response(function(msg, match) {
this.ctx.messageHandled = true
this.ctx.arbitraryProp = 'value'
bot.sendMessage(this.chatId, `Message '${msg.text}' was handled by a specific handler`)
})
)
bot.onText(
/.+/,
response(function(msg, match) {
if (this.ctx.messageHandled) {
return
}
bot.sendMessage(this.chatId, `Message '${msg.text}' was handled by a generic handler`)
})
)