grammY Throttler (转换器)
关于
这个节奏器旨在限制和排队向外的 Telegram API 调用以适应官方 Telegram API 费率限制。 。 。 .
配置
节流器接受以下形式的单个可选参数:
type ThrottlerOptions = {
global?: Bottleneck.ConstructorOptions; // For throttling all api calls
group?: Bottleneck.ConstructorOptions; // For throttling outgoing group messages
out?: Bottleneck.ConstructorOptions; // For throttling outgoing private messages
};
可用的对象属性完整列表 Bottleneck.ConstructorOptions 可见于 圆顶。 。 。 .
如果没有通过参数,所创建的节流器将使用默认配置设置,这种设置应当适合大多数使用的情况。 默认配置如下:
// Outgoing Global Throttler
const globalConfig = {
reservoir: 30, // Number of new jobs that throttler will accept at start
reservoirRefreshAmount: 30, // Number of jobs that throttler will accept after refresh
reservoirRefreshInterval: 1000, // Interval in milliseconds where reservoir will refresh
};
// Outgoing Group Throttler
const groupConfig = {
maxConcurrent: 1, // Only 1 job at a time
minTime: 1000, // Wait this many milliseconds to be ready, after a job
reservoir: 20, // Number of new jobs that throttler will accept at start
reservoirRefreshAmount: 20, // Number of jobs that throttler will accept after refresh
reservoirRefreshInterval: 60000, // Interval in milliseconds where reservoir will refresh
};
// Outgoing Private Throttler
const outConfig = {
maxConcurrent: 1, // Only 1 job at a time
minTime: 1000, // Wait this many milliseconds to be ready, after a job
};
使用量
import { Bot } from 'https://lib.deno.dev/x/grammy@1/mod.ts';
import { run } from 'https://lib.deno.dev/x/grammy_runner@1/mod.ts';
import { apiThrottler, bypassThrottler } from 'https://lib.deno.dev/x/grammy_transformer_throttler@1/mod.ts';
const botToken = Deno.env.get('BOT_TOKEN');
if (!botToken) {
throw Error('BOT_TOKEN is required');
}
const bot = new Bot(botToken);
const throttler = apiThrottler();
bot.api.config.use(throttler);
// Experimental: Do not throttle update-initiated first response
bot.use(bypassThrottler);
bot.command('/example', ctx => ctx.reply('I am throttled'));
// If you are using throttler, you most likely want to use a runner to handle updates concurrently
run(bot);