auto-retry
)
Retry API Requests (Consider using the throttler plugin instead.
This plugin is an API transformer function, which means that it let’s you intercept and modify outgoing HTTP requests on the fly. More specifically, this plugin will automatically detect if an API requests fails with a retry
value, i.e. because of rate limiting. It will then catch the error, wait the specified period of time, and then retry the request.
Be Gentle With the Bot API Server
Telegram is generously providing information about how long your bot must wait before the next request. Using the auto
plugin will allow your bot to perform better during load spikes, as the requests will not simply fail because of the flood limit. However, auto-retry should not be used if you want to avoid hitting rate limits on a regular basis. If you regularly cross the threshold of how many requests you may perform, Telegram may take measures such as restricting or banning your bot.
You can install this plugin on the bot
object:
import { autoRetry } from "@grammyjs/auto-retry";
// Use the plugin.
bot.api.config.use(autoRetry());
const { autoRetry } = require("@grammyjs/auto-retry");
// Use the plugin.
bot.api.config.use(autoRetry());
import { autoRetry } from "https://esm.sh/@grammyjs/auto-retry";
// Use the plugin.
bot.api.config.use(autoRetry());
If you now call e.g. send
and run into a rate limit, it will look like the request just takes unusually long. Under the hood, multiple HTTP requests are being performed, with the appropriate delays in between.
You may pass an options object that specifies a maximum number of retries (max
, default: 3), or a threshold for a maximum time to wait (max
, default: 1 hour).
As soon as the maximum number of retries is exhausted, subsequent errors for the same request will not be retried again. Instead, the error object from Telegram is passed on, effectively failing the request with a Grammy
.
Similarly, if the request ever fails with retry
larger than what is specified by the option max
, the request will fail immediately.
autoRetry({
maxRetryAttempts: 1, // only repeat requests once
maxDelaySeconds: 5, // fail immediately if we have to wait >5 seconds
});
You can use retry
to include all other internal server errors by Telegram (status code >= 500) in the above procedure. Those errors will be retried immediately, but they also respect the max
option.
Plugin Summary
- Name:
auto
-retry - Source: https://
github .com /grammyjs /auto -retry - Reference: https://
doc .deno .land /https:// raw .githubusercontent .com /grammyjs /auto -retry /main /src /index .ts