mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Add AsyncSuffix to methods
This commit is contained in:
parent
2ffa954246
commit
0351c0f1df
|
|
@ -1,20 +1,15 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using TLSharp.Core.Auth;
|
||||
using TLSharp.Core.MTProto;
|
||||
using TLSharp.Core.MTProto.Crypto;
|
||||
using TLSharp.Core.Network;
|
||||
using TLSharp.Core.Requests;
|
||||
using TeleSharp.TL;
|
||||
using MD5 = System.Security.Cryptography.MD5;
|
||||
using TeleSharp.TL.Help;
|
||||
using TeleSharp.TL.Auth;
|
||||
using TeleSharp.TL.Contacts;
|
||||
using TeleSharp.TL.Help;
|
||||
using TeleSharp.TL.Messages;
|
||||
using TLSharp.Core.Auth;
|
||||
using TLSharp.Core.MTProto.Crypto;
|
||||
using TLSharp.Core.Network;
|
||||
|
||||
namespace TLSharp.Core
|
||||
{
|
||||
|
|
@ -42,7 +37,7 @@ namespace TLSharp.Core
|
|||
_transport = new TcpTransport(_session.ServerAddress, _session.Port);
|
||||
}
|
||||
|
||||
public async Task<bool> Connect(bool reconnect = false)
|
||||
public async Task<bool> ConnectAsync(bool reconnect = false)
|
||||
{
|
||||
if (_session.AuthKey == null || reconnect)
|
||||
{
|
||||
|
|
@ -67,7 +62,7 @@ namespace TLSharp.Core
|
|||
return true;
|
||||
}
|
||||
|
||||
private async Task ReconnectToDc(int dcId)
|
||||
private async Task ReconnectToDcAsync(int dcId)
|
||||
{
|
||||
if (dcOptions == null || !dcOptions.Any())
|
||||
throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
|
||||
|
|
@ -78,7 +73,7 @@ namespace TLSharp.Core
|
|||
_session.ServerAddress = dc.ip_address;
|
||||
_session.Port = dc.port;
|
||||
|
||||
await Connect(true);
|
||||
await ConnectAsync(true);
|
||||
}
|
||||
|
||||
public bool IsUserAuthorized()
|
||||
|
|
@ -86,7 +81,7 @@ namespace TLSharp.Core
|
|||
return _session.TLUser != null;
|
||||
}
|
||||
|
||||
public async Task<bool> IsPhoneRegistered(string phoneNumber)
|
||||
public async Task<bool> IsPhoneRegisteredAsync(string phoneNumber)
|
||||
{
|
||||
if (_sender == null)
|
||||
throw new InvalidOperationException("Not connected!");
|
||||
|
|
@ -98,7 +93,7 @@ namespace TLSharp.Core
|
|||
return authCheckPhoneRequest.Response.phone_registered;
|
||||
}
|
||||
|
||||
public async Task<string> SendCodeRequest(string phoneNumber)
|
||||
public async Task<string> SendCodeRequestAsync(string phoneNumber)
|
||||
{
|
||||
var completed = false;
|
||||
|
||||
|
|
@ -118,7 +113,7 @@ namespace TLSharp.Core
|
|||
{
|
||||
if (ex.Message.StartsWith("Your phone number registered to") && ex.Data["dcId"] != null)
|
||||
{
|
||||
await ReconnectToDc((int)ex.Data["dcId"]);
|
||||
await ReconnectToDcAsync((int)ex.Data["dcId"]);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -130,7 +125,7 @@ namespace TLSharp.Core
|
|||
return request.Response.phone_code_hash;
|
||||
}
|
||||
|
||||
public async Task<TLUser> MakeAuth(string phoneNumber, string phoneCodeHash, string code)
|
||||
public async Task<TLUser> MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code)
|
||||
{
|
||||
var request = new TLRequestSignIn() { phone_number = phoneNumber, phone_code_hash = phoneCodeHash, phone_code = code };
|
||||
await _sender.Send(request);
|
||||
|
|
@ -141,7 +136,7 @@ namespace TLSharp.Core
|
|||
return ((TLUser)request.Response.user);
|
||||
}
|
||||
|
||||
public async Task<TLUser> SignUp(string phoneNumber, string phoneCodeHash, string code, string firstName, string lastName)
|
||||
public async Task<TLUser> SignUpAsync(string phoneNumber, string phoneCodeHash, string code, string firstName, string lastName)
|
||||
{
|
||||
var request = new TLRequestSignUp() { phone_number = phoneNumber, phone_code = code, phone_code_hash = phoneCodeHash, first_name = firstName, last_name = lastName };
|
||||
await _sender.Send(request);
|
||||
|
|
@ -151,7 +146,7 @@ namespace TLSharp.Core
|
|||
|
||||
return ((TLUser)request.Response.user);
|
||||
}
|
||||
public async Task<T> SendRequest<T>(TLMethod methodtoExceute)
|
||||
public async Task<T> SendRequestAsync<T>(TLMethod methodtoExceute)
|
||||
{
|
||||
await _sender.Send(methodtoExceute);
|
||||
await _sender.Receive(methodtoExceute);
|
||||
|
|
@ -162,24 +157,24 @@ namespace TLSharp.Core
|
|||
}
|
||||
|
||||
|
||||
public async Task<TLContacts> GetContacts()
|
||||
public async Task<TLContacts> GetContactsAsync()
|
||||
{
|
||||
if (!IsUserAuthorized())
|
||||
throw new InvalidOperationException("Authorize user first!");
|
||||
|
||||
var req = new TLRequestGetContacts() {hash = ""};
|
||||
|
||||
return await SendRequest<TLContacts>(req);
|
||||
return await SendRequestAsync<TLContacts>(req);
|
||||
}
|
||||
|
||||
public async Task<TLAbsUpdates> SendMessage(TLAbsInputPeer peer, string message)
|
||||
public async Task<TLAbsUpdates> SendMessageAsync(TLAbsInputPeer peer, string message)
|
||||
{
|
||||
if (!IsUserAuthorized())
|
||||
throw new InvalidOperationException("Authorize user first!");
|
||||
|
||||
long uniqueId = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds);
|
||||
|
||||
return await SendRequest<TLAbsUpdates>(
|
||||
return await SendRequestAsync<TLAbsUpdates>(
|
||||
new TLRequestSendMessage()
|
||||
{
|
||||
peer = peer,
|
||||
|
|
@ -188,20 +183,20 @@ namespace TLSharp.Core
|
|||
});
|
||||
}
|
||||
|
||||
public async Task<Boolean> SendTyping(TLAbsInputPeer peer)
|
||||
public async Task<Boolean> SendTypingAsync(TLAbsInputPeer peer)
|
||||
{
|
||||
var req = new TLRequestSetTyping()
|
||||
{
|
||||
action = new TLSendMessageTypingAction(),
|
||||
peer = peer
|
||||
};
|
||||
return await SendRequest<Boolean>(req);
|
||||
return await SendRequestAsync<Boolean>(req);
|
||||
}
|
||||
|
||||
public async Task<TLDialogs> GetUserDialogs()
|
||||
public async Task<TLDialogs> GetUserDialogsAsync()
|
||||
{
|
||||
var peer = new TLInputPeerSelf();
|
||||
return await SendRequest<TLDialogs>(
|
||||
return await SendRequestAsync<TLDialogs>(
|
||||
new TLRequestGetDialogs() { offset_date = 0, offset_peer = peer, limit = 100 });
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,8 +4,13 @@ using System.Configuration;
|
|||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using TeleSharp.TL;
|
||||
using TeleSharp.TL.Channels;
|
||||
using TeleSharp.TL.Contacts;
|
||||
using TeleSharp.TL.Messages;
|
||||
using TLSharp.Core;
|
||||
using TLSharp.Core.Auth;
|
||||
using TLSharp.Core.MTProto;
|
||||
|
|
@ -28,9 +33,9 @@ namespace TLSharp.Tests
|
|||
|
||||
private string NumberToAddToChat { get; set; }
|
||||
|
||||
private string apiHash = "";
|
||||
private string apiHash = null;
|
||||
|
||||
private int apiId;
|
||||
private int apiId = 0;
|
||||
|
||||
[TestInitialize]
|
||||
public void Init()
|
||||
|
|
@ -67,32 +72,69 @@ namespace TLSharp.Tests
|
|||
var store = new FileSessionStore();
|
||||
var client = new TelegramClient(store, "session", apiId, apiHash);
|
||||
|
||||
await client.Connect();
|
||||
await client.ConnectAsync();
|
||||
|
||||
var hash = await client.SendCodeRequest(NumberToAuthenticate);
|
||||
var hash = await client.SendCodeRequestAsync(NumberToAuthenticate);
|
||||
var code = "93463"; // you can change code in debugger
|
||||
|
||||
var user = await client.MakeAuth(NumberToAuthenticate, hash, code);
|
||||
var user = await client.MakeAuthAsync(NumberToAuthenticate, hash, code);
|
||||
|
||||
Assert.IsNotNull(user);
|
||||
Assert.IsTrue(client.IsUserAuthorized());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestMethod]
|
||||
public async Task SendMessageTest()
|
||||
{
|
||||
var store = new FileSessionStore();
|
||||
var client = new TelegramClient(store, "session", apiId, apiHash);
|
||||
|
||||
await client.ConnectAsync();
|
||||
|
||||
var result = await client.GetContactsAsync();
|
||||
|
||||
var user = result.users.lists
|
||||
.Where(x => x.GetType() == typeof (TLUser))
|
||||
.Cast<TLUser>()
|
||||
.FirstOrDefault(x => x.phone == NumberToSendMessage);
|
||||
await client.SendTypingAsync(new TLInputPeerUser() {user_id = user.id});
|
||||
Thread.Sleep(3000);
|
||||
await client.SendMessageAsync(new TLInputPeerUser() {user_id = user.id}, "TEST");
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task SendMessageToChannelTest()
|
||||
{
|
||||
var store = new FileSessionStore();
|
||||
var client = new TelegramClient(store, "session", apiId, apiHash);
|
||||
|
||||
await client.ConnectAsync();
|
||||
|
||||
var dialogs = await client.GetUserDialogsAsync();
|
||||
var chat = dialogs.chats.lists
|
||||
.Where(c => c.GetType() == typeof(TLChannel))
|
||||
.Cast<TLChannel>()
|
||||
.FirstOrDefault(c => c.title == "TestGroup");
|
||||
|
||||
await client.SendMessageAsync(new TLInputPeerChannel() { channel_id = chat.id, access_hash = chat.access_hash.Value }, "TEST MSG");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task SignUpNewUser()
|
||||
{
|
||||
var store = new FileSessionStore();
|
||||
var client = new TelegramClient(store, "session", apiId, apiHash);
|
||||
await client.Connect();
|
||||
await client.ConnectAsync();
|
||||
|
||||
var hash = await client.SendCodeRequest(NotRegisteredNumberToSignUp);
|
||||
var hash = await client.SendCodeRequestAsync(NotRegisteredNumberToSignUp);
|
||||
var code = "";
|
||||
|
||||
var registeredUser = await client.SignUp(NotRegisteredNumberToSignUp, hash, code, "TLSharp", "User");
|
||||
var registeredUser = await client.SignUpAsync(NotRegisteredNumberToSignUp, hash, code, "TLSharp", "User");
|
||||
Assert.IsNotNull(registeredUser);
|
||||
Assert.IsTrue(client.IsUserAuthorized());
|
||||
|
||||
var loggedInUser = await client.MakeAuth(NotRegisteredNumberToSignUp, hash, code);
|
||||
var loggedInUser = await client.MakeAuthAsync(NotRegisteredNumberToSignUp, hash, code);
|
||||
Assert.IsNotNull(loggedInUser);
|
||||
}
|
||||
|
||||
|
|
@ -101,9 +143,9 @@ namespace TLSharp.Tests
|
|||
{
|
||||
var store = new FileSessionStore();
|
||||
var client = new TelegramClient(store, "session", apiId, apiHash);
|
||||
await client.Connect();
|
||||
await client.ConnectAsync();
|
||||
|
||||
var result = await client.IsPhoneRegistered(NumberToAuthenticate);
|
||||
var result = await client.IsPhoneRegisteredAsync(NumberToAuthenticate);
|
||||
Assert.IsTrue(result);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue