2015-09-28 04:01:17 +02:00
|
|
|
|
using System;
|
2015-10-14 18:16:27 +02:00
|
|
|
|
using System.Collections.Generic;
|
2015-09-28 04:01:17 +02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2016-09-24 15:38:26 +02:00
|
|
|
|
using TeleSharp.TL;
|
|
|
|
|
|
using TeleSharp.TL.Auth;
|
2016-10-11 15:28:57 +02:00
|
|
|
|
using TeleSharp.TL.Contacts;
|
2016-10-11 15:32:38 +02:00
|
|
|
|
using TeleSharp.TL.Help;
|
2016-10-11 15:28:57 +02:00
|
|
|
|
using TeleSharp.TL.Messages;
|
2016-10-11 15:32:38 +02:00
|
|
|
|
using TLSharp.Core.Auth;
|
|
|
|
|
|
using TLSharp.Core.MTProto.Crypto;
|
|
|
|
|
|
using TLSharp.Core.Network;
|
2015-09-28 04:01:17 +02:00
|
|
|
|
|
|
|
|
|
|
namespace TLSharp.Core
|
|
|
|
|
|
{
|
2016-04-18 12:50:57 +02:00
|
|
|
|
public class TelegramClient
|
|
|
|
|
|
{
|
|
|
|
|
|
private MtProtoSender _sender;
|
|
|
|
|
|
private AuthKey _key;
|
|
|
|
|
|
private TcpTransport _transport;
|
2016-04-18 13:05:30 +02:00
|
|
|
|
private string _apiHash = "";
|
|
|
|
|
|
private int _apiId = 0;
|
2016-04-18 12:50:57 +02:00
|
|
|
|
private Session _session;
|
2016-09-24 15:38:26 +02:00
|
|
|
|
private List<TLDcOption> dcOptions;
|
2016-07-20 08:26:55 +02:00
|
|
|
|
|
2016-10-11 16:31:30 +02:00
|
|
|
|
public TelegramClient(int apiId, string apiHash, ISessionStore store = null, string sessionUserId = "session")
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
2016-10-11 16:31:30 +02:00
|
|
|
|
if (store == null)
|
|
|
|
|
|
store = new FileSessionStore();
|
|
|
|
|
|
|
2016-09-24 15:38:26 +02:00
|
|
|
|
TLContext.Init();
|
2016-04-18 13:05:30 +02:00
|
|
|
|
_apiHash = apiHash;
|
|
|
|
|
|
_apiId = apiId;
|
2016-04-18 12:50:57 +02:00
|
|
|
|
if (_apiId == 0)
|
|
|
|
|
|
throw new InvalidOperationException("Your API_ID is invalid. Do a configuration first https://github.com/sochix/TLSharp#quick-configuration");
|
|
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(_apiHash))
|
|
|
|
|
|
throw new InvalidOperationException("Your API_ID is invalid. Do a configuration first https://github.com/sochix/TLSharp#quick-configuration");
|
|
|
|
|
|
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
|
|
|
|
|
|
_transport = new TcpTransport(_session.ServerAddress, _session.Port);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
public async Task<bool> ConnectAsync(bool reconnect = false)
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (_session.AuthKey == null || reconnect)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = await Authenticator.DoAuthentication(_transport);
|
|
|
|
|
|
_session.AuthKey = result.AuthKey;
|
|
|
|
|
|
_session.TimeOffset = result.TimeOffset;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_sender = new MtProtoSender(_transport, _session);
|
|
|
|
|
|
|
|
|
|
|
|
if (!reconnect)
|
|
|
|
|
|
{
|
2016-09-24 15:38:26 +02:00
|
|
|
|
var config = new TLRequestGetConfig() ;
|
|
|
|
|
|
var request = new TLRequestInitConnection() { api_id = _apiId, app_version = "1.0.0", device_model = "PC", lang_code = "en", query= config, system_version = "Win 10.0" };
|
|
|
|
|
|
var invokewithLayer = new TLRequestInvokeWithLayer() { layer = 53, query = request };
|
|
|
|
|
|
await _sender.Send(invokewithLayer);
|
|
|
|
|
|
await _sender.Receive(invokewithLayer);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2016-09-24 15:38:26 +02:00
|
|
|
|
dcOptions = ((TLConfig)invokewithLayer.Response).dc_options.lists;
|
2016-04-18 12:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
private async Task ReconnectToDcAsync(int dcId)
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (dcOptions == null || !dcOptions.Any())
|
|
|
|
|
|
throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
|
|
|
|
|
|
|
2016-09-24 15:38:26 +02:00
|
|
|
|
var dc = dcOptions.First(d => d.id == dcId);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2016-09-24 15:38:26 +02:00
|
|
|
|
_transport = new TcpTransport(dc.ip_address, dc.port);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
_session.ServerAddress = dc.ip_address;
|
2016-09-24 15:38:26 +02:00
|
|
|
|
_session.Port = dc.port;
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
await ConnectAsync(true);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsUserAuthorized()
|
|
|
|
|
|
{
|
2016-09-24 15:38:26 +02:00
|
|
|
|
return _session.TLUser != null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
public async Task<bool> IsPhoneRegisteredAsync(string phoneNumber)
|
2016-09-24 15:38:26 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (_sender == null)
|
|
|
|
|
|
throw new InvalidOperationException("Not connected!");
|
|
|
|
|
|
|
|
|
|
|
|
var authCheckPhoneRequest = new TLRequestCheckPhone() { phone_number = phoneNumber };
|
|
|
|
|
|
await _sender.Send(authCheckPhoneRequest);
|
|
|
|
|
|
await _sender.Receive(authCheckPhoneRequest);
|
|
|
|
|
|
|
|
|
|
|
|
return authCheckPhoneRequest.Response.phone_registered;
|
2016-04-18 12:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
public async Task<string> SendCodeRequestAsync(string phoneNumber)
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
|
|
|
|
|
var completed = false;
|
|
|
|
|
|
|
2016-09-24 15:38:26 +02:00
|
|
|
|
TLRequestSendCode request = null;
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
|
|
|
|
|
while (!completed)
|
|
|
|
|
|
{
|
2016-09-24 15:38:26 +02:00
|
|
|
|
request = new TLRequestSendCode() { phone_number = phoneNumber, api_id = _apiId, api_hash = _apiHash };
|
2016-04-18 12:50:57 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await _sender.Send(request);
|
2016-07-20 09:09:27 +02:00
|
|
|
|
await _sender.Receive(request);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
|
|
|
|
|
completed = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (InvalidOperationException ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ex.Message.StartsWith("Your phone number registered to") && ex.Data["dcId"] != null)
|
|
|
|
|
|
{
|
2016-10-11 15:32:38 +02:00
|
|
|
|
await ReconnectToDcAsync((int)ex.Data["dcId"]);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-24 15:38:26 +02:00
|
|
|
|
return request.Response.phone_code_hash;
|
2016-04-18 12:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
public async Task<TLUser> MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code)
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
2016-09-24 15:38:26 +02:00
|
|
|
|
var request = new TLRequestSignIn() { phone_number = phoneNumber, phone_code_hash = phoneCodeHash, phone_code = code };
|
2016-04-18 12:50:57 +02:00
|
|
|
|
await _sender.Send(request);
|
2016-07-20 09:09:27 +02:00
|
|
|
|
await _sender.Receive(request);
|
2016-01-17 10:16:44 +01:00
|
|
|
|
|
2016-09-24 15:38:26 +02:00
|
|
|
|
OnUserAuthenticated(((TLUser)request.Response.user));
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2016-09-24 15:38:26 +02:00
|
|
|
|
return ((TLUser)request.Response.user);
|
2016-09-06 17:37:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
public async Task<TLUser> SignUpAsync(string phoneNumber, string phoneCodeHash, string code, string firstName, string lastName)
|
2016-09-24 15:38:26 +02:00
|
|
|
|
{
|
|
|
|
|
|
var request = new TLRequestSignUp() { phone_number = phoneNumber, phone_code = code, phone_code_hash = phoneCodeHash, first_name = firstName, last_name = lastName };
|
|
|
|
|
|
await _sender.Send(request);
|
|
|
|
|
|
await _sender.Receive(request);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2016-09-24 15:38:26 +02:00
|
|
|
|
OnUserAuthenticated(((TLUser)request.Response.user));
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2016-09-24 15:38:26 +02:00
|
|
|
|
return ((TLUser)request.Response.user);
|
|
|
|
|
|
}
|
2016-10-11 15:32:38 +02:00
|
|
|
|
public async Task<T> SendRequestAsync<T>(TLMethod methodtoExceute)
|
2016-09-24 15:38:26 +02:00
|
|
|
|
{
|
|
|
|
|
|
await _sender.Send(methodtoExceute);
|
|
|
|
|
|
await _sender.Receive(methodtoExceute);
|
2016-10-11 15:28:57 +02:00
|
|
|
|
|
|
|
|
|
|
var result = methodtoExceute.GetType().GetProperty("Response").GetValue(methodtoExceute);
|
|
|
|
|
|
|
|
|
|
|
|
return (T)result;
|
2016-09-24 15:38:26 +02:00
|
|
|
|
}
|
2016-10-11 15:28:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
public async Task<TLContacts> GetContactsAsync()
|
2016-10-11 15:28:57 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (!IsUserAuthorized())
|
|
|
|
|
|
throw new InvalidOperationException("Authorize user first!");
|
|
|
|
|
|
|
|
|
|
|
|
var req = new TLRequestGetContacts() {hash = ""};
|
|
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
return await SendRequestAsync<TLContacts>(req);
|
2016-10-11 15:28:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
public async Task<TLAbsUpdates> SendMessageAsync(TLAbsInputPeer peer, string message)
|
2016-10-11 15:28:57 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (!IsUserAuthorized())
|
|
|
|
|
|
throw new InvalidOperationException("Authorize user first!");
|
|
|
|
|
|
|
|
|
|
|
|
long uniqueId = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds);
|
|
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
return await SendRequestAsync<TLAbsUpdates>(
|
2016-10-11 15:28:57 +02:00
|
|
|
|
new TLRequestSendMessage()
|
|
|
|
|
|
{
|
|
|
|
|
|
peer = peer,
|
|
|
|
|
|
message = message,
|
|
|
|
|
|
random_id = uniqueId
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
public async Task<Boolean> SendTypingAsync(TLAbsInputPeer peer)
|
2016-10-11 15:28:57 +02:00
|
|
|
|
{
|
|
|
|
|
|
var req = new TLRequestSetTyping()
|
|
|
|
|
|
{
|
|
|
|
|
|
action = new TLSendMessageTypingAction(),
|
|
|
|
|
|
peer = peer
|
|
|
|
|
|
};
|
2016-10-11 15:32:38 +02:00
|
|
|
|
return await SendRequestAsync<Boolean>(req);
|
2016-10-11 15:28:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-11 15:32:38 +02:00
|
|
|
|
public async Task<TLDialogs> GetUserDialogsAsync()
|
2016-10-11 15:28:57 +02:00
|
|
|
|
{
|
|
|
|
|
|
var peer = new TLInputPeerSelf();
|
2016-10-11 15:32:38 +02:00
|
|
|
|
return await SendRequestAsync<TLDialogs>(
|
2016-10-11 15:28:57 +02:00
|
|
|
|
new TLRequestGetDialogs() { offset_date = 0, offset_peer = peer, limit = 100 });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnUserAuthenticated(TLUser TLUser)
|
2016-09-06 17:37:05 +02:00
|
|
|
|
{
|
2016-09-24 15:38:26 +02:00
|
|
|
|
_session.TLUser = TLUser;
|
|
|
|
|
|
_session.SessionExpires = int.MaxValue;
|
2016-09-06 17:37:05 +02:00
|
|
|
|
|
|
|
|
|
|
_session.Save();
|
|
|
|
|
|
}
|
2016-09-24 15:38:26 +02:00
|
|
|
|
|
2016-04-18 12:50:57 +02:00
|
|
|
|
}
|
2015-09-28 04:01:17 +02:00
|
|
|
|
}
|