Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Client: add call for resendCode request #1020

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions TLSharp.Core/TelegramClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ public bool IsUserAuthorized()
return request.Response.PhoneCodeHash;
}

public async Task<string> 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<TLUser> MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code, CancellationToken token = default(CancellationToken))
{
if (String.IsNullOrWhiteSpace(phoneNumber))
Expand Down
6 changes: 6 additions & 0 deletions TLSharp.Tests.NUnit/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public async override Task AuthUser()
await base.AuthUser();
}

[Test]
public async override Task AuthUserByResendCode()
{
await base.AuthUserByResendCode();
}

[Test]
public override async Task SendMessageTest()
{
Expand Down
6 changes: 6 additions & 0 deletions TLSharp.Tests.VS/TLSharpTestsVs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public override async Task AuthUser()
await base.AuthUser();
}

[TestMethod]
public override async Task AuthUserByResendCode()
{
await base.AuthUserByResendCode();
}

[TestMethod]
public override async Task SendMessageTest()
{
Expand Down
38 changes: 38 additions & 0 deletions TLSharp.Tests/TLSharpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,44 @@ public virtual async Task AuthUser()
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)];
Expand Down