### Complete Integration Example with Deno and Runner Source: https://context7.com/grammyjs/transformer-throttler/llms.txt Full example showing Deno usage with grammY runner for production-ready concurrent update handling with throttling. Apply the throttler transformer to all API calls and optionally enable first-response bypass. ```typescript // Deno import style 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"; // Get bot token from environment const botToken = Deno.env.get("BOT_TOKEN"); if (!botToken) { throw Error("BOT_TOKEN is required"); } const bot = new Bot(botToken); // Apply throttler transformer to all API calls const throttler = apiThrottler(); bot.api.config.use(throttler); // Optional: Enable first-response bypass for better UX bot.use(bypassThrottler); // Register handlers bot.command("example", (ctx) => ctx.reply("I am throttled")); bot.command("bulk", async (ctx) => { // Simulate sending to multiple chats - all throttled automatically for (let i = 0; i < 10; i++) { await ctx.reply(`Message ${i + 1} of 10`); } }); // Use runner for concurrent update processing // This is recommended when using throttler to handle updates in parallel run(bot); ``` -------------------------------- ### Initialize and Use grammY API Throttler Source: https://github.com/grammyjs/transformer-throttler/blob/master/src/README.md Initializes the grammY API throttler and applies it to the bot's API configuration. Requires a bot token and uses a runner for concurrent update handling. ```typescript 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 } 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); bot.command('/example', ctx => ctx.reply('I am throttled')); // If you are using throttler, you most likely want to use a runner to handle update concurrently run(bot); ``` -------------------------------- ### Configure Throttler with Bottleneck Strategies Source: https://context7.com/grammyjs/transformer-throttler/llms.txt Configure the API throttler with specific Bottleneck strategies for handling queue behavior when full or jobs fail. Use `BottleneckStrategy.LEAK` to drop oldest jobs or `BottleneckStrategy.OVERFLOW` to reject new jobs. ```typescript import { Bot } from "grammy"; import { apiThrottler, BottleneckStrategy } from "@grammyjs/transformer-throttler"; const bot = new Bot("YOUR_BOT_TOKEN"); // Configure throttler with specific Bottleneck strategies const throttler = apiThrottler({ global: { reservoir: 30, reservoirRefreshAmount: 30, reservoirRefreshInterval: 1000, strategy: BottleneckStrategy.LEAK, // Drop oldest jobs when full }, group: { maxConcurrent: 1, minTime: 1000, reservoir: 20, reservoirRefreshAmount: 20, reservoirRefreshInterval: 60000, strategy: BottleneckStrategy.OVERFLOW, // Reject new jobs when full }, }); bot.api.config.use(throttler); // Available strategies: // BottleneckStrategy.LEAK - Drop oldest jobs when queue is full // BottleneckStrategy.OVERFLOW - Reject new jobs when queue is full // BottleneckStrategy.OVERFLOW_PRIORITY - Reject lowest priority jobs // BottleneckStrategy.BLOCK - Block new jobs until queue has space bot.command("test", (ctx) => ctx.reply("Throttled with custom strategy")); bot.start(); ``` -------------------------------- ### Basic grammY Bot with Throttler Integration Source: https://github.com/grammyjs/transformer-throttler/blob/master/README.md Integrates the apiThrottler into a grammY bot and uses bypassThrottler for specific commands. Requires BOT_TOKEN environment variable. ```typescript 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); ``` -------------------------------- ### Configure API Throttler with Custom Options Source: https://context7.com/grammyjs/transformer-throttler/llms.txt Customize rate limiting parameters for high-traffic bots. This allows fine-grained control over global, group, and private chat message limits. ```typescript import { Bot } from "grammy"; import { apiThrottler } from "@grammyjs/transformer-throttler"; import type { APIThrottlerOptions } from "@grammyjs/transformer-throttler"; // Create bot instance const bot = new Bot("YOUR_BOT_TOKEN"); // Option 2: Custom configuration for high-traffic bots const customOptions: APIThrottlerOptions = { global: { reservoir: 30, reservoirRefreshAmount: 30, reservoirRefreshInterval: 1000, }, group: { maxConcurrent: 1, minTime: 1000, reservoir: 20, reservoirRefreshAmount: 20, reservoirRefreshInterval: 60000, }, out: { maxConcurrent: 1, minTime: 1000, }, }; const customThrottler = apiThrottler(customOptions); bot.api.config.use(customThrottler); // Handle commands - all replies will be automatically throttled bot.command("start", (ctx) => ctx.reply("Welcome! Messages are throttled.")); bot.command("broadcast", async (ctx) => { // Even bulk operations are automatically rate-limited const userIds = [123456789, 987654321, 456789123]; for (const userId of userIds) { await bot.api.sendMessage(userId, "Broadcast message"); } }); bot.start(); ``` -------------------------------- ### Configure API Throttler with Default Settings Source: https://context7.com/grammyjs/transformer-throttler/llms.txt Use the default configuration for standard rate limiting. This transformer intercepts and queues API calls to prevent hitting Telegram's rate limits. ```typescript import { Bot } from "grammy"; import { apiThrottler } from "@grammyjs/transformer-throttler"; // Create bot instance const bot = new Bot("YOUR_BOT_TOKEN"); // Option 1: Use default configuration (recommended for most use cases) const throttler = apiThrottler(); bot.api.config.use(throttler); // Handle commands - all replies will be automatically throttled bot.command("start", (ctx) => ctx.reply("Welcome! Messages are throttled.")); bot.command("broadcast", async (ctx) => { // Even bulk operations are automatically rate-limited const userIds = [123456789, 987654321, 456789123]; for (const userId of userIds) { await bot.api.sendMessage(userId, "Broadcast message"); } }); bot.start(); ``` -------------------------------- ### Default Global Throttler Configuration Source: https://github.com/grammyjs/transformer-throttler/blob/master/README.md Default configuration for the global API throttler. Sets the reservoir size and refresh rate for outgoing API calls. ```typescript // Outgoing Global Throttler const globalConfig = { reservoir: 30, reservoirRefreshAmount: 30, reservoirRefreshInterval: 1000, }; ``` -------------------------------- ### Default Private Throttler Configuration Source: https://github.com/grammyjs/transformer-throttler/blob/master/README.md Default configuration for the private message throttler. Limits concurrent messages and sets a minimum time between them. ```typescript // Outgoing Private Throttler const outConfig = { maxConcurrent: 1, minTime: 1000, }; ``` -------------------------------- ### Default Group Throttler Configuration Source: https://github.com/grammyjs/transformer-throttler/blob/master/README.md Default configuration for the group message throttler. Limits concurrent messages and sets a minimum time between them, along with reservoir settings. ```typescript // Outgoing Group Throttler const groupConfig = { maxConcurrent: 1, minTime: 1000, reservoir: 20, reservoirRefreshAmount: 20, reservoirRefreshInterval: 60000, }; ``` -------------------------------- ### Bypass Throttler for Immediate First Response Source: https://context7.com/grammyjs/transformer-throttler/llms.txt Use bypassThrottler to allow the first API call within an update handler to execute immediately, while subsequent calls are throttled. This is useful for quick acknowledgments. ```typescript import { Bot } from "grammy"; import { run } from "@grammyjs/runner"; import { apiThrottler, bypassThrottler } from "@grammyjs/transformer-throttler"; const bot = new Bot("YOUR_BOT_TOKEN"); // Set up the API throttler const throttler = apiThrottler(); bot.api.config.use(throttler); // Enable bypass for first response in each update handler bot.use(bypassThrottler); bot.command("quick", async (ctx) => { // This first reply bypasses the throttler for immediate response await ctx.reply("Quick response!"); // Subsequent calls in the same handler are throttled normally await ctx.reply("This message is throttled"); await ctx.reply("This message is also throttled"); }); bot.on("message", async (ctx) => { // First response is immediate await ctx.reply("Got your message!"); // Additional processing with throttled API calls await bot.api.sendMessage(ctx.chat.id, "Processing complete"); }); // Use runner for concurrent update handling with throttling run(bot); ``` -------------------------------- ### Throttler Options Type Definition Source: https://github.com/grammyjs/transformer-throttler/blob/master/README.md Defines the configuration options for the throttler, allowing separate configurations for global API calls, group messages, and private messages. ```typescript type ThrottlerOptions = { global?: Bottleneck.ConstructorOptions; group?: Bottleneck.ConstructorOptions; out?: Bottleneck.ConstructorOptions; }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.