From 75f96c881495254edb97d587b306c2a40253bca0 Mon Sep 17 00:00:00 2001 From: Julio Date: Fri, 16 Apr 2021 17:27:41 +0300 Subject: [PATCH] Client: add call for resendCode request --- TLSharp.Core/TelegramClient.cs | 15 ++++++++++++ TLSharp.Tests.NUnit/Test.cs | 6 +++++ TLSharp.Tests.VS/TLSharpTestsVs.cs | 6 +++++ TLSharp.Tests/TLSharpTests.cs | 38 ++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index e5b0819..98e0c5e 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -207,6 +207,21 @@ namespace TLSharp.Core return request.Response.PhoneCodeHash; } + public async Task ResendCodeRequestAsync(string phoneNumber, string phoneCodeHash, CancellationToken token = default(CancellationToken)) + { + if (String.IsNullOrWhiteSpace(phoneNumber)) + throw new ArgumentNullException(nameof(phoneNumber)); + + if(String.IsNullOrWhiteSpace(phoneCodeHash)) + throw new ArgumentNullException(nameof(phoneCodeHash)); + + var request = new TLRequestResendCode() { PhoneNumber = phoneNumber, PhoneCodeHash = phoneCodeHash }; + + await RequestWithDcMigration(request, token).ConfigureAwait(false); + + return request.Response.PhoneCodeHash; + } + public async Task MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code, CancellationToken token = default(CancellationToken)) { if (String.IsNullOrWhiteSpace(phoneNumber)) diff --git a/TLSharp.Tests.NUnit/Test.cs b/TLSharp.Tests.NUnit/Test.cs index 40e54ac..b208a0b 100644 --- a/TLSharp.Tests.NUnit/Test.cs +++ b/TLSharp.Tests.NUnit/Test.cs @@ -21,6 +21,12 @@ namespace TLSharp.Tests await base.AuthUser(); } + [Test] + public async override Task AuthUserByResendCode() + { + await base.AuthUserByResendCode(); + } + [Test] public override async Task SendMessageTest() { diff --git a/TLSharp.Tests.VS/TLSharpTestsVs.cs b/TLSharp.Tests.VS/TLSharpTestsVs.cs index 8f6c58d..b7877aa 100644 --- a/TLSharp.Tests.VS/TLSharpTestsVs.cs +++ b/TLSharp.Tests.VS/TLSharpTestsVs.cs @@ -20,6 +20,12 @@ namespace TLSharp.Tests await base.AuthUser(); } + [TestMethod] + public override async Task AuthUserByResendCode() + { + await base.AuthUserByResendCode(); + } + [TestMethod] public override async Task SendMessageTest() { diff --git a/TLSharp.Tests/TLSharpTests.cs b/TLSharp.Tests/TLSharpTests.cs index 6acde72..4e2c00f 100644 --- a/TLSharp.Tests/TLSharpTests.cs +++ b/TLSharp.Tests/TLSharpTests.cs @@ -155,6 +155,44 @@ namespace TLSharp.Tests Assert.IsTrue(client.IsUserAuthorized()); } + public virtual async Task AuthUserByResendCode() + { + var client = NewClient(); + + await client.ConnectAsync(); + + var hash = await client.SendCodeRequestAsync(NumberToAuthenticate); + + var resendHash = await client.ResendCodeRequestAsync(NumberToAuthenticate, hash); + + var code = CodeToAuthenticate; // you can change code in debugger too + + 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, resendHash, code); + } + catch (CloudPasswordNeededException ex) + { + var passwordSetting = await client.GetPasswordSetting(); + var password = PasswordToAuthenticate; + + user = await client.MakeAuthWithPasswordAsync(passwordSetting, password); + } + 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()); + } + public virtual async Task SendMessageTest() { NumberToSendMessage = ConfigurationManager.AppSettings[nameof(NumberToSendMessage)];