diff --git a/README.md b/README.md index 1811330..b8e8b11 100644 --- a/README.md +++ b/README.md @@ -211,8 +211,8 @@ TLSharp library should automatically handle these errors. If you see such errors You should create a Telegram session. See [configuration guide](#sending-messages-set-up) -#### Why I get FLOOD_WAIT error? -[It's Telegram restrictions](https://core.telegram.org/api/errors#420-flood) +#### Why do I get a FloodException/FLOOD_WAIT error? +It's likely [Telegram restrictions](https://core.telegram.org/api/errors#420-flood), or a bug in TLSharp (if you feel it's the latter, please open a Github issue). You can know the time to wait by accessing the FloodException::TimeToWait property. #### Why does TLSharp lacks feature XXXX? diff --git a/TLSharp.Core/Network/MtProtoSender.cs b/TLSharp.Core/Network/MtProtoSender.cs index 8929760..9cb7061 100644 --- a/TLSharp.Core/Network/MtProtoSender.cs +++ b/TLSharp.Core/Network/MtProtoSender.cs @@ -272,8 +272,7 @@ namespace TLSharp.Core.Network { var resultString = Regex.Match(errorMessage, @"\d+").Value; var seconds = int.Parse(resultString); - Debug.WriteLine($"Should wait {seconds} sec."); - Thread.Sleep(1000 * seconds); + throw new FloodException(TimeSpan.FromSeconds(seconds)); } else if (errorMessage.StartsWith("PHONE_MIGRATE_")) { @@ -499,6 +498,18 @@ namespace TLSharp.Core.Network } } + public class FloodException : Exception + { + public TimeSpan TimeToWait { get; private set; } + + internal FloodException(TimeSpan timeToWait) + : base($"Flood prevention. Telegram now requires your program to do requests again only after {timeToWait.TotalSeconds} seconds have passed ({nameof(TimeToWait)} property)." + + " If you think the culprit of this problem may lie in TLSharp's implementation, open a Github issue please.") + { + TimeToWait = timeToWait; + } + } + internal abstract class DataCenterMigrationException : Exception { internal int DC { get; private set; }