tdlib / td

Cross-platform library for building Telegram clients
https://core.telegram.org/tdlib
Boost Software License 1.0
7.09k stars 1.44k forks source link

Telegram limits on sending messages #1331

Closed graymind123 closed 3 years ago

graymind123 commented 3 years ago

I have a bot with more than 150K users. In peak time, it is needed to send more than 1000 message to my subscriber. My issue is with time. it is very important to send message on time and with minimum delay. As you know Telegram has some limits on number of messages, a bot can send per second( 30 msg per second). I want to know if I use Tdlib library, instead of Bot API, can I overcome this issue somehow? any one has any suggestion to send these bunch of messages from bot to subscribers with least delay?

levlam commented 3 years ago

No, you can't. All limits are server-side.

Do you already have problems? Why do you have to send a lot of private messages simultaneously?

graymind123 commented 3 years ago

Users subscribe to my bot to receive alert about special events happened in forex markets (for example). So it is important to send messages on time. for now I have to limit my bot users to 1K or less. And use multiple bots names in series: fooBot1, fooBot2, .. . it is really annoying and inconvenient. buy the way, I cant send message to channel, because there is many kind of messages (market events), and users can select their desired type to received from bot.(not all messages)

levlam commented 3 years ago

There is no way to avoid flood limits for this use case.

graymind123 commented 3 years ago

thanks for your quick reply.

HellFinger-s commented 3 years ago

@levlam what is meant by this phrase High-performance. In the Telegram Bot API, each TDLib instance handles more than 24,000 active bots simultaneously if there are no increased limits on sending messages

RememberTheAir commented 3 years ago

@HellFinger-s limits as we were talking about in this issue are account-based. Each one of these 24.000 bots is a different account

HellFinger-s commented 3 years ago

@RememberTheAir Sorry, but I don't quite understand you

RememberTheAir commented 3 years ago

If you try to send 150.000 messages in a row from a single bot account, Telegram will rate-limit that account pretty soon, after a bunch of messages. But this is just one account. Tdlib can handle many simultaniously. If one of these accounts tries to send 150.000 messages and gets rate-limited by Telegram, the other account the tdlib isnatnce is handling will not be rate-limited as well, because they are different bots account. Think about it as logging in with multiple accounts on the Android app. If you get one of these accounts banned (hypothetically), the other ones will not be affected

HellFinger-s commented 3 years ago

You mean that if one of the bot account gets rate-limited, I can authorize new bot account and keep sending messages? And is there any limit of sending if I authorize like user?

levlam commented 3 years ago

@HellFinger-s User limits are much lower than bot limits.

HellFinger-s commented 3 years ago

@levlam Ok. But I didn't understand completely that RememberTheAir said. It turns out, I need two different bot accounts or not?

jumoog commented 3 years ago

don't do that.. your accounts will be suspended.

levlam commented 3 years ago

@HellFinger-s Bots can't send messages to random users. Authorizing another bot wouldn't help you.

TrashAccount3424 commented 3 years ago

@graymind123 you need to ask your users to create a bot and send you the token then send the messages to them via their own bot (make sure they start it first) you can still use you own bot for configurations and stuff so you dont have to listen to each bots updates. you still have send limits ofc but they are per bot and each bot only has one user so should not be a problem. this way you could run these 24k bots parallel on one TDLib according to the message above probably more if you ignore incoming updates not quite clear what telegram gains from this as it obviously wastes more resources on their side but whatever stupid limits requires stupid solutions

NabiKAZ commented 1 year ago

you need to ask your users to create a bot and send you the token then send the messages to them via their own bot...

In this case, you should think about sending 150,000 messages, although to different tokens, it is not considered DDOS by Telegram and the IP is not blocked?!

NabiKAZ commented 1 year ago

... a bot can send per second( 30 msg per second). ...

306060*24 = 2592000 It means that the Telegram bot cannot send birthday greetings to more than 2,592,000 people!!! ;-)

TrashAccount3424 commented 1 year ago

you need to ask your users to create a bot and send you the token then send the messages to them via their own bot...

In this case, you should think about sending 150,000 messages, although to different tokens, it is not considered DDOS by Telegram and the IP is not blocked?!

150k message in what amount of time? In the original message it was 150k user and 1000 time-sensitive messages that need to be send to 1000 different user by 1000 different bots.

TrashAccount3424 commented 1 year ago

... a bot can send per second( 30 msg per second). ...

30_60_60*24 = 2592000 It means that the Telegram bot cannot send birthday greetings to more than 2,592,000 people!!! ;-)

Your math is completely wrong. With 2592000 users only about 7101 would have birthday on every single day and it would only take about 4 minutes to send these.

NabiKAZ commented 1 year ago

... a bot can send per second( 30 msg per second). ...

30_60_60*24 = 2592000 It means that the Telegram bot cannot send birthday greetings to more than 2,592,000 people!!! ;-)

Your math is completely wrong. With 2592000 users only about 7101 would have birthday on every single day and it would only take about 4 minutes to send these.

But you are wrong. I think you divided this number by 365 days of the year. But why do you think that all 2,592,000 people should be born on different days of the year? If we assume that all of them were born on the same day. You cannot send a message to the next person that day.

jumoog commented 1 year ago

You can calculate how many people probably have a birthday on the same day.

https://en.wikipedia.org/wiki/Birthday_problem

TrashAccount3424 commented 1 year ago

... a bot can send per second( 30 msg per second). ...

30_60_60*24 = 2592000 It means that the Telegram bot cannot send birthday greetings to more than 2,592,000 people!!! ;-)

Your math is completely wrong. With 2592000 users only about 7101 would have birthday on every single day and it would only take about 4 minutes to send these.

But you are wrong. I think you divided this number by 365 days of the year. But why do you think that all 2,592,000 people should be born on different days of the year? If we assume that all of them were born on the same day. You cannot send a message to the next person that day.

why do you think that all 2,592,000 people should be born on different days of the year?

I assumed even distribution over the year to get a realistic base load. Some days would have more, some less but nowhere near reaching the limits.

If we assume that all of them were born on the same day.

That is a completely useless assumption, This is like throwing a 365-sided dice 2592000 times and get the same side every time.

TrashAccount3424 commented 1 year ago

You can calculate how many people probably have a birthday on the same day.

https://en.wikipedia.org/wiki/Birthday_problem

Does not apply here, were dont try to find out random birthday collisions. The average birthdays per day is all birthdays divided by the number of days.

NabiKAZ commented 1 year ago

... a bot can send per second( 30 msg per second). ...

30_60_60*24 = 2592000 It means that the Telegram bot cannot send birthday greetings to more than 2,592,000 people!!! ;-)

Your math is completely wrong. With 2592000 users only about 7101 would have birthday on every single day and it would only take about 4 minutes to send these.

But you are wrong. I think you divided this number by 365 days of the year. But why do you think that all 2,592,000 people should be born on different days of the year? If we assume that all of them were born on the same day. You cannot send a message to the next person that day.

why do you think that all 2,592,000 people should be born on different days of the year?

I assumed even distribution over the year to get a realistic base load. Some days would have more, some less but nowhere near reaching the limits.

If we assume that all of them were born on the same day.

That is a completely useless assumption, This is like throwing a 365-sided dice 2592000 times and get the same side every time.

And what if my system has 946,080,000 members? If you don't say that this is an impossible and pointless assumption! 😉

TrashAccount3424 commented 1 year ago

And what if my system has 946,080,000 members? If you don't say that this is an impossible and pointless assumption! 😉

Your math was wrong, using a different number doesn't change that.

NabiKAZ commented 1 year ago

And what if my system has 946,080,000 members? If you don't say that this is an impossible and pointless assumption! 😉

Your math was wrong, using a different number doesn't change that.

If you don't understand something, ask questions instead of ignoring it altogether. I used your own assumption. You talked about uniform distribution. You said that 2,592,000 births should be divided by 365, which is about 7,101, which takes about 4 minutes. If you believe what you say and it is about uniform distribution throughout the year. This hypothesis fails for the number 946,080,000 when it is divided by 365 and the same previous number 2,592,000 is obtained per day! 😉

In any case, we both know that such a restriction on the part of Telegram is stupid. 😄

TrashAccount3424 commented 1 year ago

And what if my system has 946,080,000 members? If you don't say that this is an impossible and pointless assumption! 😉

Your math was wrong, using a different number doesn't change that.

If you don't understand something, ask questions instead of ignoring it altogether. I used your own assumption. You talked about uniform distribution. You said that 2,592,000 births should be divided by 365, which is about 7,101, which takes about 4 minutes. If you believe what you say and it is about uniform distribution throughout the year. This hypothesis fails for the number 946,080,000 when it is divided by 365 and the same previous number 2,592,000 is obtained per day! 😉

In any case, we both know that such a restriction on the part of Telegram is stupid. 😄

You really think I could not figure out how you came to that number? I didn't ask because its obvious. You used my way of calculating it - just backwards. So you just admitted that your first calculation was wrong.

Also 946,080,000 is still wrong if you think that would be the where such a bot would reach the limits. If the average is 2592000 which is exactly the daily limit. Then the limit would be reached before 946,080,000 users, because some days will have more and some less than the average + other messages the bot must send when people start it etc.

Also Telegram does not even have that many users so this is not an actual issue you just thought it was one because of your initial flawed math.

Anyway I wont reply any further, have a nice day.

NabiKAZ commented 1 year ago

And what if my system has 946,080,000 members? If you don't say that this is an impossible and pointless assumption! 😉

Your math was wrong, using a different number doesn't change that.

If you don't understand something, ask questions instead of ignoring it altogether. I used your own assumption. You talked about uniform distribution. You said that 2,592,000 births should be divided by 365, which is about 7,101, which takes about 4 minutes. If you believe what you say and it is about uniform distribution throughout the year. This hypothesis fails for the number 946,080,000 when it is divided by 365 and the same previous number 2,592,000 is obtained per day! 😉 In any case, we both know that such a restriction on the part of Telegram is stupid. 😄

You really think I could not figure out how you came to that number? I didn't ask because its obvious. You used my way of calculating it - just backwards. So you just admitted that your first calculation was wrong.

Also 946,080,000 is still wrong if you think that would be the where such a bot would reach the limits. If the average is 2592000 which is exactly the daily limit. Then the limit would be reached before 946,080,000 users, because some days will have more and some less than the average + other messages the bot must send when people start it etc.

Also Telegram does not even have that many users so this is not an actual issue you just thought it was one because of your initial flawed math.

Anyway I wont reply any further, have a nice day.

I did not accept anything. I just tried to talk to you in your own language. ((:

Before 946,080,000? Well then it's worse. Now imagine that I said the maximum number.

In the end, I may agree that the hypothesis and numbers are a bit idealistic and would not happen in reality, but they happen to be absolutely correct in terms of the math you insist is wrong. In other words, they may not happen in reality, but they are true on paper.

Thank you for your consensus. <3

Jacksonm93 commented 8 months ago

@graymind123 how did your overcome the limit?

micheleselea commented 5 months ago

I think the 30 messages per second it's not a real limit, the limit is much more less, because in my bot with thousand of users I never send more than 200.000 or 300.000 per day, so 10 times less the hypothetical rate limit, but I often get a "too many requests" retry after xxx seconds. In the last weeks I see sometimes 7000 seconds or more! I think it's not acceptable, probably telegram has some kind of issue here: I'm using this bot since 2015 and never had this retry after time......something is going wrong, I think probably I have to check for another solution :(

levlam commented 5 months ago

@micheleselea Likely, you are trying to send too many messages to the same user. Many more messages than one can read.

micheleselea commented 5 months ago

Ok so @levlam what is supposed to be the rate limit per user?

levlam commented 5 months ago

Some reasonable value that one can possibly read.

TrashAccount3424 commented 5 months ago

Ok so @levlam what is supposed to be the rate limit per user?

The limit is when you get an error from telegram. Until that happens you can send as much as you want but its probably a good ideas to always await successful sending before you send the next message to the same user. AKA no parallel sending to the same chat. This also prevents messages from being shown in the wrong order.

micheleselea commented 5 months ago

@levlam the "Some reasonable value that one can possibly read" it's not so easy to translate to software code: 1 per second can be "reasonable"? @TrashAccount3424 the problem is that when you hit the limit you got stuck for a long time, I would prevent reaching limit. I always wait for reply, and never send messages in parallel, but yesterday I got this at about 18:00 UTC

{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 10072","parameters":{"retry_after":10072}}

I think 10072 seconds is symptoms something is going wrong. After 20:00 / 21:00 UTC all seems to go better and I receive sometimes

{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 2","parameters":{"retry_after":2}} or similar, that is acceptable

I whish to have a way to make my sending more reliable, I need a way to programmatically code a sending procedure that prevent my bot from hitting limit, but without clear specification is not easy

micheleselea commented 5 months ago

it would be nice to have some kind of business pay bot (like the other business stuff) with some more specific or extended rate limit

levlam commented 5 months ago

Does the bot send the messages to your account?

micheleselea commented 5 months ago

I do not understand the question: I rarely receive messages from bot, it sends messages to my users, it send some "critical notification". I have about 10.000 users that receives alerts from their systems

jumoog commented 5 months ago

It makes a difference whether you send many messages to one person or a single message to several people.

micheleselea commented 5 months ago

@jumoog usually is one message to several different users. But can happen that system send some messages to the same users: think for example 10 different alarm in a minute that have to be sent to the same 10 users

ne0c0de commented 5 months ago

best way to avoid this ban, request each user to create their own bot from botfather and get the bot token from them, use that bot token to send messages to that user

micheleselea commented 5 months ago

@ne0c0de I know that is a nice way to get that result, but with my system and my users is not an option to have this kind of environment

TrashAccount3424 commented 5 months ago

@TrashAccount3424 the problem is that when you hit the limit you got stuck for a long time, I would prevent reaching limit. I always wait for reply, and never send messages in parallel, but yesterday I got this at about 18:00 UTC

{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 10072","parameters":{"retry_after":10072}}

I think 10072 seconds is symptoms something is going wrong. After 20:00 / 21:00 UTC all seems to go better and I receive sometimes

You can not prevent reaching the limits. The limits change based on multiple factors most of which are outside of your control. It could be that they had some issue at one of the data centers and thus send out large rety_after time instead of returning all request as failed (which would mean you send it again instantly). I don't know if that happend yesterday but it could be and there is no way you can prevent that or know in advance. You can keep sending messages to all other chats if this happens in one or a few chats.

micheleselea commented 5 months ago

ok, I thought that the limit hit was for bot not for specific chat, I keep this in consideration

levlam commented 5 months ago

@micheleselea All limits are for specific requests. It is incorrect to extend them to other requests.

micheleselea commented 5 months ago

@levlam so you think the mode should be:

-Send request 1 -got retry_after on request 1 -reschedule request 1 -send request 2...and so on?

because now I'm waiting on retry_after for all requests after the 1

TrashAccount3424 commented 5 months ago

@levlam so you think the mode should be:

-Send request 1 -got retry_after on request 1 -reschedule request 1 -send request 2...and so on?

because now I'm waiting on retry_after for all requests after the 1

You should queue requests based on the order they should be executed. If you have 3 messages to 1 user they probably should be send in order so if the first gets retry_after you pause the queue and all 3 requests are paused.

But the queue for another user is not affected by this.

potapoviv commented 3 months ago

Maybe you should contact Telegram Business about this issue, or use a mini app?