Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Freqtrade | 21,153 | 2 | 11 hours ago | 42 | July 03, 2022 | 64 | gpl-3.0 | Python | ||
Free, open source crypto trading bot | ||||||||||
Node Telegram Bot Api | 6,987 | 1,940 | 283 | 12 hours ago | 60 | August 29, 2022 | 65 | mit | JavaScript | |
Telegram Bot API for NodeJS | ||||||||||
Pytelegrambotapi | 6,687 | 865 | 59 | 3 days ago | 103 | June 23, 2022 | 3 | gpl-2.0 | Python | |
Python Telegram bot api. | ||||||||||
Telegraf | 6,561 | 687 | 279 | 4 days ago | 211 | September 13, 2022 | 21 | mit | TypeScript | |
Modern Telegram Bot Framework for Node.js | ||||||||||
Telegramgroup | 4,684 | 23 days ago | 10 | |||||||
悄咪咪收集的1000+个Telegram群合集,如果有更多好玩的telegram群,欢迎在 issue 提出或者pull requests | ||||||||||
Nonebot2 | 4,012 | 5 hours ago | 21 | June 20, 2022 | 9 | mit | Python | |||
跨平台 Python 异步聊天机器人框架 / Asynchronous multi-platform chatbot framework written in Python | ||||||||||
Telegrambots | 3,555 | 479 | 18 | a month ago | 57 | June 21, 2022 | 166 | mit | Java | |
Java library to create bots using Telegram Bots API | ||||||||||
Aiogram | 3,475 | 35 | 45 | 4 days ago | 75 | June 21, 2022 | 103 | mit | Python | |
aiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio | ||||||||||
Core | 3,473 | 27 | 23 | a month ago | 91 | July 03, 2022 | 49 | mit | PHP | |
PHP Telegram Bot based on the official Telegram Bot API | ||||||||||
Freenom | 2,891 | 11 days ago | 76 | mit | PHP | |||||
Freenom 域名自动续期。Freenom domain name renews automatically. |
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);
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);
}.bind(this);
}
// 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(this.msg.chat.id); });
// 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()
this.stop();
}
});
bot.onText(/\/need_auth/, checkAuth(function() {
// Give some info only to authenticated user
}));
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() {
this.methodWithPossibilityOfErrorOccuring();
}
yourCustomMiddleware.onErrorHandler = err => {
// Log or do some other things with error
}
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.
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.