Reputation: 1061
We have a Telegram bot. It has around 1.2 million subscribers.
Now we're facing a problem in sending messages to these number of subscribers.
Telegram bot API does not provide any batch message functionality and we'd have to send individual requests to Telegram. The problem is, after a few thousand messages, Telegram starts responding with Error 429: too many requests, and does not accept any requests for a while.
How can we effectively message our subscribers?
Upvotes: 29
Views: 55054
Reputation: 121
Java version:
private Integer executeMessage(SendMessage message) {
try {
Message execute = execute(message);
return execute.getMessageId();
} catch (TelegramApiException e) {
if (e.getMessage().contains("[429]")) {
Pattern pattern = Pattern.compile("retry after (\\d+)");
Matcher matcher = pattern.matcher(e.getMessage());
if (matcher.find()) {
long delay = Integer.parseInt(matcher.group(1)) * 1000L;
try {
Thread.sleep(delay);
} catch (InterruptedException ex) {
..
}
}
}
}
return null;
}
Upvotes: 0
Reputation: 5129
What I did with 100% degree of success was:
1- Read the returning JSON when status code is 429. That should be something like:
{
"ok":false,
"error_code":429,
"description":"Too Many Requests: retry after 35",
"parameters":{
"retry_after":35
}
}
2- Add a sleep based on parameters.retry_after from that JSON
3- After sleep completes, send again the last message that gave error
4- Continue to send messages on queue until you get the next 429 error
5- Repeat steps 1 and on until all messages are delivered
My code:
def postNews(news: list[str]):
tries = 3
for link in news:
for i in range(tries):
result = requests.get(f'https://api.telegram.org/botXX:YY/sendMessage', params={'chat_id': '-999999999999999', 'text': link})
if result.status_code == 200:
#//sleep to avoid being blocked
time.sleep(2)
break
#//skip error sleep on last try
if i == tries - 1:
continue
#//there is some error, let us do some sleep
sleepTime = 3
if result.status_code == 429:
sleepTime = result.json()['parameters']['retry_after']
time.sleep(sleepTime)
else:
#//this else gets executed if the loop don't break
print(f'{link} - {result.status_code} {result.reason} {result.text}')
Upvotes: 5
Reputation: 1951
It can happen also if a Telegram group is in the slow mode and the bot tries to send more that one message at once to that group. I fixed this by adding a delay to the bot trigger mechanism.
Upvotes: 0
Reputation: 51
I had similar problems with messages, the pause between which was 0.5 seconds (this is much less than 30 messages per second!). The problem was only associated with messages, the content of which I tried to change. So when you try to use "edit_message_text" or "edit_message_media" take more pause between messages.
Upvotes: 5
Reputation: 908
Based on the Telegram Bots FAQ for sending messages, you should consider this:
If you're sending bulk notifications to multiple users, the API will not allow more than 30 messages per second or so. Consider spreading out notifications over large intervals of 8—12 hours for best results.
Upvotes: 8
Reputation: 10866
You should simply implement a global rate limiter to ensure no single user gets above a fixed number of messages per second. to be safe, set your limiter at lower than 30, maybe even to 5 msgs per second.
Really anything higher than 5 messages per second to a single user quickly becomes an annoyance.
cheers.
Upvotes: 14
Reputation: 211
I'm the owner of Ramona Bot. There is a limit for sending message to users. as they said ~30 message per second. Otherwise you will get that Error 429.
Upvotes: 14