diff --git a/TLSharp.Core/Network/MtProtoSender.cs b/TLSharp.Core/Network/MtProtoSender.cs index d46a68e..8929760 100644 --- a/TLSharp.Core/Network/MtProtoSender.cs +++ b/TLSharp.Core/Network/MtProtoSender.cs @@ -293,6 +293,10 @@ namespace TLSharp.Core.Network var dcIdx = int.Parse(resultString); throw new UserMigrationException(dcIdx); } + else if (errorMessage == "PHONE_CODE_INVALID") + { + throw new InvalidPhoneCodeException("The numeric code used to authenticate does not match the numeric code sent by SMS/Telegram"); + } else { throw new InvalidOperationException(errorMessage); diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 85561ad..49a78ae 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -310,4 +310,9 @@ namespace TLSharp.Core { } } + + public class InvalidPhoneCodeException : Exception + { + internal InvalidPhoneCodeException(string msg) : base(msg) { } + } } diff --git a/TLSharp.Tests/TLSharpTests.cs b/TLSharp.Tests/TLSharpTests.cs index bb3284d..2e2ba0e 100644 --- a/TLSharp.Tests/TLSharpTests.cs +++ b/TLSharp.Tests/TLSharpTests.cs @@ -24,6 +24,8 @@ namespace TLSharp.Tests private string NumberToAuthenticate { get; set; } + private string CodeToAuthenticate { get; set; } + private string NotRegisteredNumberToSignUp { get; set; } private string UserNameToSendMessage { get; set; } @@ -72,6 +74,10 @@ namespace TLSharp.Tests if (string.IsNullOrEmpty(NumberToAuthenticate)) Debug.WriteLine("NumberToAuthenticate not configured in app.config! Some tests may fail."); + CodeToAuthenticate = ConfigurationManager.AppSettings[nameof(CodeToAuthenticate)]; + if (string.IsNullOrEmpty(CodeToAuthenticate)) + Debug.WriteLine("CpdeToAuthenticate not configured in app.config! Some tests may fail."); + NotRegisteredNumberToSignUp = ConfigurationManager.AppSettings[nameof(NotRegisteredNumberToSignUp)]; if (string.IsNullOrEmpty(NotRegisteredNumberToSignUp)) Debug.WriteLine("NotRegisteredNumberToSignUp not configured in app.config! Some tests may fail."); @@ -101,9 +107,23 @@ namespace TLSharp.Tests await client.ConnectAsync(); var hash = await client.SendCodeRequestAsync(NumberToAuthenticate); - var code = "93463"; // you can change code in debugger + var code = CodeToAuthenticate; // you can change code in debugger too - var user = await client.MakeAuthAsync(NumberToAuthenticate, hash, code); + if (String.IsNullOrWhiteSpace(code)) + { + throw new Exception("CodeToAuthenticate is empty in the app.config file, fill it with the code you just got now by SMS/Telegram"); + } + + TLUser user = null; + try + { + user = await client.MakeAuthAsync(NumberToAuthenticate, hash, code); + } + catch (InvalidPhoneCodeException ex) + { + throw new Exception("CodeToAuthenticate is wrong in the app.config file, fill it with the code you just got now by SMS/Telegram", + ex); + } Assert.IsNotNull(user); Assert.IsTrue(client.IsUserAuthorized()); diff --git a/TLSharp.Tests/app.config b/TLSharp.Tests/app.config index 6fc79b3..941fdda 100644 --- a/TLSharp.Tests/app.config +++ b/TLSharp.Tests/app.config @@ -4,6 +4,7 @@ +