mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Merge d631c2fd89 into 133b9fdf6c
This commit is contained in:
commit
d94a10b8a0
|
|
@ -1,170 +1,170 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using TeleSharp.TL;
|
using TeleSharp.TL;
|
||||||
using TeleSharp.TL.Account;
|
using TeleSharp.TL.Account;
|
||||||
using TeleSharp.TL.Auth;
|
using TeleSharp.TL.Auth;
|
||||||
using TeleSharp.TL.Contacts;
|
using TeleSharp.TL.Contacts;
|
||||||
using TeleSharp.TL.Help;
|
using TeleSharp.TL.Help;
|
||||||
using TeleSharp.TL.Messages;
|
using TeleSharp.TL.Messages;
|
||||||
using TeleSharp.TL.Upload;
|
using TeleSharp.TL.Upload;
|
||||||
using TLSharp.Core.Auth;
|
using TLSharp.Core.Auth;
|
||||||
using TLSharp.Core.MTProto.Crypto;
|
using TLSharp.Core.MTProto.Crypto;
|
||||||
using TLSharp.Core.Network;
|
using TLSharp.Core.Network;
|
||||||
using TLSharp.Core.Utils;
|
using TLSharp.Core.Utils;
|
||||||
using TLAuthorization = TeleSharp.TL.Auth.TLAuthorization;
|
using TLAuthorization = TeleSharp.TL.Auth.TLAuthorization;
|
||||||
|
|
||||||
namespace TLSharp.Core
|
namespace TLSharp.Core
|
||||||
{
|
{
|
||||||
public class TelegramClient : IDisposable
|
public class TelegramClient : IDisposable
|
||||||
{
|
{
|
||||||
private MtProtoSender _sender;
|
private MtProtoSender _sender;
|
||||||
private AuthKey _key;
|
private AuthKey _key;
|
||||||
private TcpTransport _transport;
|
private TcpTransport _transport;
|
||||||
private string _apiHash = "";
|
private string _apiHash = "";
|
||||||
private int _apiId = 0;
|
private int _apiId = 0;
|
||||||
private Session _session;
|
private Session _session;
|
||||||
private List<TLDcOption> dcOptions;
|
private List<TLDcOption> dcOptions;
|
||||||
private TcpClientConnectionHandler _handler;
|
private TcpClientConnectionHandler _handler;
|
||||||
|
|
||||||
public TelegramClient(int apiId, string apiHash,
|
public TelegramClient(int apiId, string apiHash,
|
||||||
ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
|
ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
|
||||||
{
|
{
|
||||||
if (apiId == default(int))
|
if (apiId == default(int))
|
||||||
throw new MissingApiConfigurationException("API_ID");
|
throw new MissingApiConfigurationException("API_ID");
|
||||||
if (string.IsNullOrEmpty(apiHash))
|
if (string.IsNullOrEmpty(apiHash))
|
||||||
throw new MissingApiConfigurationException("API_HASH");
|
throw new MissingApiConfigurationException("API_HASH");
|
||||||
|
|
||||||
if (store == null)
|
if (store == null)
|
||||||
store = new FileSessionStore();
|
store = new FileSessionStore();
|
||||||
|
|
||||||
TLContext.Init();
|
TLContext.Init();
|
||||||
_apiHash = apiHash;
|
_apiHash = apiHash;
|
||||||
_apiId = apiId;
|
_apiId = apiId;
|
||||||
_handler = handler;
|
_handler = handler;
|
||||||
|
|
||||||
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
|
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
|
||||||
_transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
|
_transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> ConnectAsync(bool reconnect = false)
|
public async Task<bool> ConnectAsync(bool reconnect = false)
|
||||||
{
|
{
|
||||||
if (_session.AuthKey == null || reconnect)
|
if (_session.AuthKey == null || reconnect)
|
||||||
{
|
{
|
||||||
var result = await Authenticator.DoAuthentication(_transport);
|
var result = await Authenticator.DoAuthentication(_transport);
|
||||||
_session.AuthKey = result.AuthKey;
|
_session.AuthKey = result.AuthKey;
|
||||||
_session.TimeOffset = result.TimeOffset;
|
_session.TimeOffset = result.TimeOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
_sender = new MtProtoSender(_transport, _session);
|
_sender = new MtProtoSender(_transport, _session);
|
||||||
|
|
||||||
//set-up layer
|
//set-up layer
|
||||||
var config = new TLRequestGetConfig();
|
var config = new TLRequestGetConfig();
|
||||||
var request = new TLRequestInitConnection()
|
var request = new TLRequestInitConnection()
|
||||||
{
|
{
|
||||||
api_id = _apiId,
|
api_id = _apiId,
|
||||||
app_version = "1.0.0",
|
app_version = "1.0.0",
|
||||||
device_model = "PC",
|
device_model = "PC",
|
||||||
lang_code = "en",
|
lang_code = "en",
|
||||||
query = config,
|
query = config,
|
||||||
system_version = "Win 10.0"
|
system_version = "Win 10.0"
|
||||||
};
|
};
|
||||||
var invokewithLayer = new TLRequestInvokeWithLayer() { layer = 66, query = request };
|
var invokewithLayer = new TLRequestInvokeWithLayer() { layer = 66, query = request };
|
||||||
await _sender.Send(invokewithLayer);
|
await _sender.Send(invokewithLayer);
|
||||||
await _sender.Receive(invokewithLayer);
|
await _sender.Receive(invokewithLayer);
|
||||||
|
|
||||||
dcOptions = ((TLConfig)invokewithLayer.Response).dc_options.lists;
|
dcOptions = ((TLConfig)invokewithLayer.Response).dc_options.lists;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ReconnectToDcAsync(int dcId)
|
private async Task ReconnectToDcAsync(int dcId)
|
||||||
{
|
{
|
||||||
if (dcOptions == null || !dcOptions.Any())
|
if (dcOptions == null || !dcOptions.Any())
|
||||||
throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
|
throw new InvalidOperationException($"Can't reconnect. Establish initial connection first.");
|
||||||
|
|
||||||
var dc = dcOptions.First(d => d.id == dcId);
|
var dc = dcOptions.First(d => d.id == dcId);
|
||||||
|
|
||||||
_transport = new TcpTransport(dc.ip_address, dc.port, _handler);
|
_transport = new TcpTransport(dc.ip_address, dc.port, _handler);
|
||||||
_session.ServerAddress = dc.ip_address;
|
_session.ServerAddress = dc.ip_address;
|
||||||
_session.Port = dc.port;
|
_session.Port = dc.port;
|
||||||
|
|
||||||
await ConnectAsync(true);
|
await ConnectAsync(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsUserAuthorized()
|
public bool IsUserAuthorized()
|
||||||
{
|
{
|
||||||
return _session.TLUser != null;
|
return _session.TLUser != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> IsPhoneRegisteredAsync(string phoneNumber)
|
public async Task<bool> IsPhoneRegisteredAsync(string phoneNumber)
|
||||||
{
|
{
|
||||||
if (String.IsNullOrWhiteSpace(phoneNumber))
|
if (String.IsNullOrWhiteSpace(phoneNumber))
|
||||||
throw new ArgumentNullException(nameof(phoneNumber));
|
throw new ArgumentNullException(nameof(phoneNumber));
|
||||||
|
|
||||||
if (_sender == null)
|
if (_sender == null)
|
||||||
throw new InvalidOperationException("Not connected!");
|
throw new InvalidOperationException("Not connected!");
|
||||||
|
|
||||||
var authCheckPhoneRequest = new TLRequestCheckPhone() { phone_number = phoneNumber };
|
var authCheckPhoneRequest = new TLRequestCheckPhone() { phone_number = phoneNumber };
|
||||||
var completed = false;
|
var completed = false;
|
||||||
while(!completed)
|
while(!completed)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _sender.Send(authCheckPhoneRequest);
|
await _sender.Send(authCheckPhoneRequest);
|
||||||
await _sender.Receive(authCheckPhoneRequest);
|
await _sender.Receive(authCheckPhoneRequest);
|
||||||
completed = true;
|
completed = true;
|
||||||
}
|
}
|
||||||
catch(PhoneMigrationException e)
|
catch(PhoneMigrationException e)
|
||||||
{
|
{
|
||||||
await ReconnectToDcAsync(e.DC);
|
await ReconnectToDcAsync(e.DC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return authCheckPhoneRequest.Response.phone_registered;
|
return authCheckPhoneRequest.Response.phone_registered;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<string> SendCodeRequestAsync(string phoneNumber)
|
public async Task<string> SendCodeRequestAsync(string phoneNumber)
|
||||||
{
|
{
|
||||||
if (String.IsNullOrWhiteSpace(phoneNumber))
|
if (String.IsNullOrWhiteSpace(phoneNumber))
|
||||||
throw new ArgumentNullException(nameof(phoneNumber));
|
throw new ArgumentNullException(nameof(phoneNumber));
|
||||||
|
|
||||||
var completed = false;
|
var completed = false;
|
||||||
|
|
||||||
TLRequestSendCode request = null;
|
TLRequestSendCode request = null;
|
||||||
|
|
||||||
while (!completed)
|
while (!completed)
|
||||||
{
|
{
|
||||||
request = new TLRequestSendCode() { phone_number = phoneNumber, api_id = _apiId, api_hash = _apiHash };
|
request = new TLRequestSendCode() { phone_number = phoneNumber, api_id = _apiId, api_hash = _apiHash };
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _sender.Send(request);
|
await _sender.Send(request);
|
||||||
await _sender.Receive(request);
|
await _sender.Receive(request);
|
||||||
|
|
||||||
completed = true;
|
completed = true;
|
||||||
}
|
}
|
||||||
catch (PhoneMigrationException ex)
|
catch (PhoneMigrationException ex)
|
||||||
{
|
{
|
||||||
await ReconnectToDcAsync(ex.DC);
|
await ReconnectToDcAsync(ex.DC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return request.Response.phone_code_hash;
|
return request.Response.phone_code_hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLUser> MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code)
|
public async Task<TLUser> MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code)
|
||||||
{
|
{
|
||||||
if (String.IsNullOrWhiteSpace(phoneNumber))
|
if (String.IsNullOrWhiteSpace(phoneNumber))
|
||||||
throw new ArgumentNullException(nameof(phoneNumber));
|
throw new ArgumentNullException(nameof(phoneNumber));
|
||||||
|
|
||||||
if (String.IsNullOrWhiteSpace(phoneCodeHash))
|
if (String.IsNullOrWhiteSpace(phoneCodeHash))
|
||||||
throw new ArgumentNullException(nameof(phoneCodeHash));
|
throw new ArgumentNullException(nameof(phoneCodeHash));
|
||||||
|
|
||||||
if (String.IsNullOrWhiteSpace(code))
|
if (String.IsNullOrWhiteSpace(code))
|
||||||
throw new ArgumentNullException(nameof(code));
|
throw new ArgumentNullException(nameof(code));
|
||||||
|
|
||||||
var request = new TLRequestSignIn() { phone_number = phoneNumber, phone_code_hash = phoneCodeHash, phone_code = code };
|
var request = new TLRequestSignIn() { phone_number = phoneNumber, phone_code_hash = phoneCodeHash, phone_code = code };
|
||||||
|
|
||||||
var completed = false;
|
var completed = false;
|
||||||
|
|
@ -181,176 +181,280 @@ namespace TLSharp.Core
|
||||||
{
|
{
|
||||||
await ReconnectToDcAsync(e.DC);
|
await ReconnectToDcAsync(e.DC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
OnUserAuthenticated(((TLUser)request.Response.user));
|
OnUserAuthenticated(((TLUser)request.Response.user));
|
||||||
|
|
||||||
return ((TLUser)request.Response.user);
|
return ((TLUser)request.Response.user);
|
||||||
}
|
}
|
||||||
public async Task<TLPassword> GetPasswordSetting()
|
public async Task<TLPassword> GetPasswordSetting()
|
||||||
{
|
{
|
||||||
var request = new TLRequestGetPassword();
|
var request = new TLRequestGetPassword();
|
||||||
|
|
||||||
await _sender.Send(request);
|
await _sender.Send(request);
|
||||||
await _sender.Receive(request);
|
await _sender.Receive(request);
|
||||||
|
|
||||||
return ((TLPassword)request.Response);
|
return ((TLPassword)request.Response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLUser> MakeAuthWithPasswordAsync(TLPassword password, string password_str)
|
public async Task<TLUser> MakeAuthWithPasswordAsync(TLPassword password, string password_str)
|
||||||
{
|
{
|
||||||
|
|
||||||
byte[] password_bytes = Encoding.UTF8.GetBytes(password_str);
|
byte[] password_bytes = Encoding.UTF8.GetBytes(password_str);
|
||||||
IEnumerable<byte> rv = password.current_salt.Concat(password_bytes).Concat(password.current_salt);
|
IEnumerable<byte> rv = password.current_salt.Concat(password_bytes).Concat(password.current_salt);
|
||||||
|
|
||||||
SHA256Managed hashstring = new SHA256Managed();
|
SHA256Managed hashstring = new SHA256Managed();
|
||||||
var password_hash = hashstring.ComputeHash(rv.ToArray());
|
var password_hash = hashstring.ComputeHash(rv.ToArray());
|
||||||
|
|
||||||
var request = new TLRequestCheckPassword() { password_hash = password_hash };
|
var request = new TLRequestCheckPassword() { password_hash = password_hash };
|
||||||
await _sender.Send(request);
|
await _sender.Send(request);
|
||||||
await _sender.Receive(request);
|
await _sender.Receive(request);
|
||||||
|
|
||||||
OnUserAuthenticated(((TLUser)request.Response.user));
|
OnUserAuthenticated(((TLUser)request.Response.user));
|
||||||
|
|
||||||
return ((TLUser)request.Response.user);
|
return ((TLUser)request.Response.user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLUser> SignUpAsync(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 };
|
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.Send(request);
|
||||||
await _sender.Receive(request);
|
await _sender.Receive(request);
|
||||||
|
|
||||||
OnUserAuthenticated(((TLUser)request.Response.user));
|
OnUserAuthenticated(((TLUser)request.Response.user));
|
||||||
|
|
||||||
return ((TLUser)request.Response.user);
|
return ((TLUser)request.Response.user);
|
||||||
}
|
}
|
||||||
public async Task<T> SendRequestAsync<T>(TLMethod methodToExecute)
|
public async Task<T> SendRequestAsync<T>(TLMethod methodToExecute)
|
||||||
{
|
{
|
||||||
await _sender.Send(methodToExecute);
|
await _sender.Send(methodToExecute);
|
||||||
await _sender.Receive(methodToExecute);
|
await _sender.Receive(methodToExecute);
|
||||||
|
|
||||||
var result = methodToExecute.GetType().GetProperty("Response").GetValue(methodToExecute);
|
var result = methodToExecute.GetType().GetProperty("Response").GetValue(methodToExecute);
|
||||||
|
|
||||||
return (T)result;
|
return (T)result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLContacts> GetContactsAsync()
|
public async Task<TLContacts> GetContactsAsync()
|
||||||
{
|
{
|
||||||
if (!IsUserAuthorized())
|
if (!IsUserAuthorized())
|
||||||
throw new InvalidOperationException("Authorize user first!");
|
throw new InvalidOperationException("Authorize user first!");
|
||||||
|
|
||||||
var req = new TLRequestGetContacts() { hash = "" };
|
var req = new TLRequestGetContacts() { hash = "" };
|
||||||
|
|
||||||
return await SendRequestAsync<TLContacts>(req);
|
return await SendRequestAsync<TLContacts>(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLAbsUpdates> SendMessageAsync(TLAbsInputPeer peer, string message)
|
public async Task<TLAbsUpdates> SendMessageAsync(TLAbsInputPeer peer, string message)
|
||||||
{
|
{
|
||||||
if (!IsUserAuthorized())
|
if (!IsUserAuthorized())
|
||||||
throw new InvalidOperationException("Authorize user first!");
|
throw new InvalidOperationException("Authorize user first!");
|
||||||
|
|
||||||
return await SendRequestAsync<TLAbsUpdates>(
|
return await SendRequestAsync<TLAbsUpdates>(
|
||||||
new TLRequestSendMessage()
|
new TLRequestSendMessage()
|
||||||
{
|
{
|
||||||
peer = peer,
|
peer = peer,
|
||||||
message = message,
|
message = message,
|
||||||
random_id = Helpers.GenerateRandomLong()
|
random_id = Helpers.GenerateRandomLong()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Boolean> SendTypingAsync(TLAbsInputPeer peer)
|
public async Task<Boolean> SendTypingAsync(TLAbsInputPeer peer)
|
||||||
{
|
{
|
||||||
var req = new TLRequestSetTyping()
|
var req = new TLRequestSetTyping()
|
||||||
{
|
{
|
||||||
action = new TLSendMessageTypingAction(),
|
action = new TLSendMessageTypingAction(),
|
||||||
peer = peer
|
peer = peer
|
||||||
};
|
};
|
||||||
return await SendRequestAsync<Boolean>(req);
|
return await SendRequestAsync<Boolean>(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLAbsDialogs> GetUserDialogsAsync()
|
public async Task<TLAbsDialogs> GetUserDialogsAsync()
|
||||||
{
|
{
|
||||||
var peer = new TLInputPeerSelf();
|
var peer = new TLInputPeerSelf();
|
||||||
return await SendRequestAsync<TLAbsDialogs>(
|
return await SendRequestAsync<TLAbsDialogs>(
|
||||||
new TLRequestGetDialogs() { offset_date = 0, offset_peer = peer, limit = 100 });
|
new TLRequestGetDialogs() { offset_date = 0, offset_peer = peer, limit = 100 });
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLAbsUpdates> SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption)
|
public async Task<TLAbsUpdates> SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption)
|
||||||
{
|
{
|
||||||
return await SendRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
|
return await SendRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
|
||||||
{
|
{
|
||||||
random_id = Helpers.GenerateRandomLong(),
|
random_id = Helpers.GenerateRandomLong(),
|
||||||
background = false,
|
background = false,
|
||||||
clear_draft = false,
|
clear_draft = false,
|
||||||
media = new TLInputMediaUploadedPhoto() { file = file, caption = caption },
|
media = new TLInputMediaUploadedPhoto() { file = file, caption = caption },
|
||||||
peer = peer
|
peer = peer
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLAbsUpdates> SendUploadedDocument(
|
public async Task<TLAbsUpdates> SendUploadedDocument(
|
||||||
TLAbsInputPeer peer, TLAbsInputFile file, string caption, string mimeType, TLVector<TLAbsDocumentAttribute> attributes)
|
TLAbsInputPeer peer, TLAbsInputFile file, string caption, string mimeType, TLVector<TLAbsDocumentAttribute> attributes)
|
||||||
{
|
{
|
||||||
return await SendRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
|
return await SendRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
|
||||||
{
|
{
|
||||||
random_id = Helpers.GenerateRandomLong(),
|
random_id = Helpers.GenerateRandomLong(),
|
||||||
background = false,
|
background = false,
|
||||||
clear_draft = false,
|
clear_draft = false,
|
||||||
media = new TLInputMediaUploadedDocument()
|
media = new TLInputMediaUploadedDocument()
|
||||||
{
|
{
|
||||||
file = file,
|
file = file,
|
||||||
caption = caption,
|
caption = caption,
|
||||||
mime_type = mimeType,
|
mime_type = mimeType,
|
||||||
attributes = attributes
|
attributes = attributes
|
||||||
},
|
},
|
||||||
peer = peer
|
peer = peer
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLFile> GetFile(TLAbsInputFileLocation location, int filePartSize, int offset = 0)
|
public async Task<TLFile> GetFile(TLAbsInputFileLocation location, int filePartSize, int offset = 0)
|
||||||
{
|
{
|
||||||
TLFile result = null;
|
TLFile result = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
result = await SendRequestAsync<TLFile>(new TLRequestGetFile()
|
result = await SendRequestAsync<TLFile>(new TLRequestGetFile()
|
||||||
{
|
{
|
||||||
location = location,
|
location = location,
|
||||||
limit = filePartSize,
|
limit = filePartSize,
|
||||||
offset = offset
|
offset = offset
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (FileMigrationException ex)
|
catch (FileMigrationException ex)
|
||||||
{
|
{
|
||||||
var exportedAuth = await SendRequestAsync<TLExportedAuthorization>(new TLRequestExportAuthorization() { dc_id = ex.DC });
|
var exportedAuth = await SendRequestAsync<TLExportedAuthorization>(new TLRequestExportAuthorization() { dc_id = ex.DC });
|
||||||
|
|
||||||
var authKey = _session.AuthKey;
|
var authKey = _session.AuthKey;
|
||||||
var timeOffset = _session.TimeOffset;
|
var timeOffset = _session.TimeOffset;
|
||||||
var serverAddress = _session.ServerAddress;
|
var serverAddress = _session.ServerAddress;
|
||||||
var serverPort = _session.Port;
|
var serverPort = _session.Port;
|
||||||
|
|
||||||
await ReconnectToDcAsync(ex.DC);
|
await ReconnectToDcAsync(ex.DC);
|
||||||
var auth = await SendRequestAsync<TLAuthorization>(new TLRequestImportAuthorization
|
var auth = await SendRequestAsync<TLAuthorization>(new TLRequestImportAuthorization
|
||||||
{
|
{
|
||||||
bytes = exportedAuth.bytes,
|
bytes = exportedAuth.bytes,
|
||||||
id = exportedAuth.id
|
id = exportedAuth.id
|
||||||
});
|
});
|
||||||
result = await GetFile(location, filePartSize, offset);
|
result = await GetFile(location, filePartSize, offset);
|
||||||
|
|
||||||
_session.AuthKey = authKey;
|
_session.AuthKey = authKey;
|
||||||
_session.TimeOffset = timeOffset;
|
_session.TimeOffset = timeOffset;
|
||||||
_transport = new TcpTransport(serverAddress, serverPort);
|
_transport = new TcpTransport(serverAddress, serverPort);
|
||||||
_session.ServerAddress = serverAddress;
|
_session.ServerAddress = serverAddress;
|
||||||
_session.Port = serverPort;
|
_session.Port = serverPort;
|
||||||
await ConnectAsync();
|
await ConnectAsync();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SendPingAsync()
|
/// <summary>
|
||||||
{
|
/// Gets user avatar that is used now.
|
||||||
await _sender.SendPingAsync();
|
/// </summary>
|
||||||
|
/// <param name="limit">Provide a number of maximum possible profile images user may have</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<byte[]> GetUserProfileImage(TLUser inputUser, int limit)
|
||||||
|
{
|
||||||
|
byte[] byteImage = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TLInputUser inputUserTL = new TLInputUser();
|
||||||
|
inputUserTL.user_id = inputUser.id;
|
||||||
|
|
||||||
|
TeleSharp.TL.Photos.TLPhotos photo = new TeleSharp.TL.Photos.TLPhotos();
|
||||||
|
photo = await GetUserPhotos(inputUserTL, 0, 0, limit);
|
||||||
|
TeleSharp.TL.TLFileLocation fileLocation = new TLFileLocation();
|
||||||
|
List<TLAbsPhoto> photoList = photo.photos.lists;
|
||||||
|
TLPhoto photo1 = (TLPhoto)photoList[0];
|
||||||
|
TLVector<TLAbsPhotoSize> photoSizeList = photo1.sizes;
|
||||||
|
List<TLAbsPhotoSize> photoSizeList2 = photoSizeList.lists;
|
||||||
|
TLPhotoSize size1 = (TLPhotoSize)photoSizeList2[0];
|
||||||
|
TLFileLocation location1 = (TLFileLocation)size1.location;
|
||||||
|
|
||||||
|
TLInputFileLocation inputLocation = new TLInputFileLocation()
|
||||||
|
{
|
||||||
|
volume_id = location1.volume_id,
|
||||||
|
local_id = location1.local_id,
|
||||||
|
secret = location1.secret
|
||||||
|
};
|
||||||
|
|
||||||
|
TeleSharp.TL.Upload.TLFile recievedFile = await GetFile(inputLocation, Convert.ToInt32(inputLocation.volume_id));
|
||||||
|
byteImage = recievedFile.bytes;
|
||||||
|
}
|
||||||
|
catch (FileMigrationException ex)
|
||||||
|
{
|
||||||
|
var exportedAuth = await SendRequestAsync<TLExportedAuthorization>(new TLRequestExportAuthorization() { dc_id = ex.DC });
|
||||||
|
|
||||||
|
var authKey = _session.AuthKey;
|
||||||
|
var timeOffset = _session.TimeOffset;
|
||||||
|
var serverAddress = _session.ServerAddress;
|
||||||
|
var serverPort = _session.Port;
|
||||||
|
|
||||||
|
await ReconnectToDcAsync(ex.DC);
|
||||||
|
var auth = await SendRequestAsync<TLAuthorization>(new TLRequestImportAuthorization
|
||||||
|
{
|
||||||
|
bytes = exportedAuth.bytes,
|
||||||
|
id = exportedAuth.id
|
||||||
|
});
|
||||||
|
|
||||||
|
byteImage = await GetUserProfileImage(inputUser, limit);
|
||||||
|
|
||||||
|
_session.AuthKey = authKey;
|
||||||
|
_session.TimeOffset = timeOffset;
|
||||||
|
_transport = new TcpTransport(serverAddress, serverPort);
|
||||||
|
_session.ServerAddress = serverAddress;
|
||||||
|
_session.Port = serverPort;
|
||||||
|
await ConnectAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
return byteImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<TeleSharp.TL.Photos.TLPhotos> GetUserPhotos(TeleSharp.TL.TLInputUser user_id, int offset, long max_id, int limit)
|
||||||
|
{
|
||||||
|
TeleSharp.TL.Photos.TLPhotos resultPhoto = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
resultPhoto = await SendRequestAsync<TeleSharp.TL.Photos.TLPhotos>(new TeleSharp.TL.Photos.TLRequestGetUserPhotos()
|
||||||
|
{
|
||||||
|
user_id = user_id,
|
||||||
|
offset = offset,
|
||||||
|
max_id = max_id,
|
||||||
|
limit = limit
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (FileMigrationException ex)
|
||||||
|
{
|
||||||
|
var exportedAuth = await SendRequestAsync<TLExportedAuthorization>(new TLRequestExportAuthorization() { dc_id = ex.DC });
|
||||||
|
|
||||||
|
var authKey = _session.AuthKey;
|
||||||
|
var timeOffset = _session.TimeOffset;
|
||||||
|
var serverAddress = _session.ServerAddress;
|
||||||
|
var serverPort = _session.Port;
|
||||||
|
|
||||||
|
await ReconnectToDcAsync(ex.DC);
|
||||||
|
var auth = await SendRequestAsync<TLAuthorization>(new TLRequestImportAuthorization
|
||||||
|
{
|
||||||
|
bytes = exportedAuth.bytes,
|
||||||
|
id = exportedAuth.id
|
||||||
|
});
|
||||||
|
|
||||||
|
resultPhoto = await GetUserPhotos(user_id, offset, max_id, limit);
|
||||||
|
|
||||||
|
_session.AuthKey = authKey;
|
||||||
|
_session.TimeOffset = timeOffset;
|
||||||
|
_transport = new TcpTransport(serverAddress, serverPort);
|
||||||
|
_session.ServerAddress = serverAddress;
|
||||||
|
_session.Port = serverPort;
|
||||||
|
await ConnectAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
return resultPhoto;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task SendPingAsync()
|
||||||
|
{
|
||||||
|
await _sender.SendPingAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<TLAbsMessages> GetHistoryAsync(TLAbsInputPeer peer, int offset, int max_id, int limit)
|
public async Task<TLAbsMessages> GetHistoryAsync(TLAbsInputPeer peer, int offset, int max_id, int limit)
|
||||||
|
|
@ -366,31 +470,31 @@ namespace TLSharp.Core
|
||||||
limit = limit
|
limit = limit
|
||||||
};
|
};
|
||||||
return await SendRequestAsync<TLAbsMessages>(req);
|
return await SendRequestAsync<TLAbsMessages>(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Serch user or chat. API: contacts.search#11f812d8 q:string limit:int = contacts.Found;
|
/// Serch user or chat. API: contacts.search#11f812d8 q:string limit:int = contacts.Found;
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="q">User or chat name</param>
|
/// <param name="q">User or chat name</param>
|
||||||
/// <param name="limit">Max result count</param>
|
/// <param name="limit">Max result count</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<TLFound> SearchUserAsync(string q, int limit = 10)
|
public async Task<TLFound> SearchUserAsync(string q, int limit = 10)
|
||||||
{
|
{
|
||||||
var r = new TeleSharp.TL.Contacts.TLRequestSearch
|
var r = new TeleSharp.TL.Contacts.TLRequestSearch
|
||||||
{
|
{
|
||||||
q = q,
|
q = q,
|
||||||
limit = limit
|
limit = limit
|
||||||
};
|
};
|
||||||
|
|
||||||
return await SendRequestAsync<TLFound>(r);
|
return await SendRequestAsync<TLFound>(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnUserAuthenticated(TLUser TLUser)
|
private void OnUserAuthenticated(TLUser TLUser)
|
||||||
{
|
{
|
||||||
_session.TLUser = TLUser;
|
_session.TLUser = TLUser;
|
||||||
_session.SessionExpires = int.MaxValue;
|
_session.SessionExpires = int.MaxValue;
|
||||||
|
|
||||||
_session.Save();
|
_session.Save();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsConnected
|
public bool IsConnected
|
||||||
|
|
@ -403,32 +507,32 @@ namespace TLSharp.Core
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (_transport != null)
|
if (_transport != null)
|
||||||
{
|
{
|
||||||
_transport.Dispose();
|
_transport.Dispose();
|
||||||
_transport = null;
|
_transport = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MissingApiConfigurationException : Exception
|
public class MissingApiConfigurationException : Exception
|
||||||
{
|
{
|
||||||
public const string InfoUrl = "https://github.com/sochix/TLSharp#quick-configuration";
|
public const string InfoUrl = "https://github.com/sochix/TLSharp#quick-configuration";
|
||||||
|
|
||||||
internal MissingApiConfigurationException(string invalidParamName) :
|
internal MissingApiConfigurationException(string invalidParamName) :
|
||||||
base($"Your {invalidParamName} setting is missing. Adjust the configuration first, see {InfoUrl}")
|
base($"Your {invalidParamName} setting is missing. Adjust the configuration first, see {InfoUrl}")
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class InvalidPhoneCodeException : Exception
|
public class InvalidPhoneCodeException : Exception
|
||||||
{
|
{
|
||||||
internal InvalidPhoneCodeException(string msg) : base(msg) { }
|
internal InvalidPhoneCodeException(string msg) : base(msg) { }
|
||||||
}
|
}
|
||||||
public class CloudPasswordNeededException : Exception
|
public class CloudPasswordNeededException : Exception
|
||||||
{
|
{
|
||||||
internal CloudPasswordNeededException(string msg) : base(msg) { }
|
internal CloudPasswordNeededException(string msg) : base(msg) { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue