Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mediv0/telegram-rate-limiter
dead simple rate limiter for telegram bots with typescript support.
https://github.com/mediv0/telegram-rate-limiter
rate-limiter telegram telegram-bot telegrambot
Last synced: 3 months ago
JSON representation
dead simple rate limiter for telegram bots with typescript support.
- Host: GitHub
- URL: https://github.com/mediv0/telegram-rate-limiter
- Owner: mediv0
- Created: 2022-03-30T11:33:12.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-12-16T20:29:49.000Z (about 1 year ago)
- Last Synced: 2024-10-29T07:21:25.627Z (4 months ago)
- Topics: rate-limiter, telegram, telegram-bot, telegrambot
- Language: TypeScript
- Homepage:
- Size: 79.1 KB
- Stars: 7
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
## Telegram Rate Limiter
dead simple rate limiter for telegram bots with typescript support.
can be used to limit the number of messages sent by a user, using a token bucket algorithm. works fine with both telegraf and Telegram-Bot-API packages.
Supported Drivers:
- Memory ( development only )
- Redis ( Production )adding other drivers is not supported yet. ( will be soon )
## Install
```
yarn add @mediv0/rate-limit
```
or
```
npm install @mediv0/rate-limit
```## usage example
```js
import { Limiter } from "@mediv0/rate-limit";// init
const limiter = new Limiter("memory", {
interval: 10,
max: 5,
driverOptions: {},
});// Telegram-Bot-API example
bot.on("message", async (msg) => {
try {
const userId = msg.chat.id;
await limiter.limit(userId);
bot.sendMessage(chatId, "Hello World!");
} catch (e) {
// catch rate limit errors
}
});
```## Options
`Limiter`
#### class Limiter (driver: `K`, options: `ILimiterOptions`)
**driver** -> `memory` | `redis`
**ILimiterOptions** ->
```
{
max: number; // maximum number of messages allowed in the interval
interval: number; // interval in minutes e.g -> 2
driverOptions: T; // driver specific options
}
```for example, in snipet below, a user can send maximum 100 messages in interval of 5 minutes. if the user sends more than 100 messages, the limiter will throw an error.
```js
const limiter = new Limiter("memory", {
interval: 5,
max: 100,
driverOptions: {},
});
```### Redis Driver
driver used in this packages is from [npm redis](https://www.npmjs.com/package/redis)
you can pass options used in redis package to rate limiter.
**[list of redis options](https://github.com/redis/node-redis/blob/HEAD/docs/client-configuration.md)**
```js
limiter = new Limiter("redis", {
interval: 10,
max: 5,
driverOptions: {
url: "redis://localhost:6379",
name: "test",
password: "test",
legacyMode: false,
// other options
},
});
```