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;
|
2016-11-16 14:31:00 +01:00
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
|
using System.Text;
|
2015-09-28 04:01:17 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2016-09-24 15:38:26 +02:00
|
|
|
|
using TeleSharp.TL;
|
2016-10-29 10:47:18 +02:00
|
|
|
|
using TeleSharp.TL.Account;
|
2016-09-24 15:38:26 +02:00
|
|
|
|
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-23 12:29:18 +02:00
|
|
|
|
using TeleSharp.TL.Upload;
|
2016-10-11 15:32:38 +02:00
|
|
|
|
using TLSharp.Core.Auth;
|
|
|
|
|
|
using TLSharp.Core.MTProto.Crypto;
|
|
|
|
|
|
using TLSharp.Core.Network;
|
2016-10-23 12:02:44 +02:00
|
|
|
|
using TLSharp.Core.Utils;
|
2016-10-29 10:47:18 +02:00
|
|
|
|
using TLAuthorization = TeleSharp.TL.Auth.TLAuthorization;
|
2015-09-28 04:01:17 +02:00
|
|
|
|
|
|
|
|
|
|
namespace TLSharp.Core
|
|
|
|
|
|
{
|
2016-12-15 21:02:23 +01:00
|
|
|
|
public class TelegramClient : IDisposable
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
|
|
|
|
|
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;
|
2017-01-27 10:58:47 +01:00
|
|
|
|
private TcpClientConnectionHandler _handler;
|
2016-07-20 08:26:55 +02:00
|
|
|
|
|
2017-01-27 10:58:47 +01:00
|
|
|
|
public TelegramClient(int apiId, string apiHash,
|
|
|
|
|
|
ISessionStore store = null, string sessionUserId = "session", TcpClientConnectionHandler handler = null)
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
2016-10-30 15:31:00 +01:00
|
|
|
|
if (apiId == default(int))
|
|
|
|
|
|
throw new MissingApiConfigurationException("API_ID");
|
|
|
|
|
|
if (string.IsNullOrEmpty(apiHash))
|
|
|
|
|
|
throw new MissingApiConfigurationException("API_HASH");
|
|
|
|
|
|
|
2016-10-15 12:35:54 +02:00
|
|
|
|
if (store == null)
|
|
|
|
|
|
store = new FileSessionStore();
|
2016-10-11 16:31:30 +02:00
|
|
|
|
|
2016-09-24 15:38:26 +02:00
|
|
|
|
TLContext.Init();
|
2016-04-18 13:05:30 +02:00
|
|
|
|
_apiHash = apiHash;
|
|
|
|
|
|
_apiId = apiId;
|
2017-01-27 10:58:47 +01:00
|
|
|
|
_handler = handler;
|
2016-10-23 17:23:10 +02:00
|
|
|
|
|
2016-04-18 12:50:57 +02:00
|
|
|
|
_session = Session.TryLoadOrCreateNew(store, sessionUserId);
|
2017-01-27 10:58:47 +01:00
|
|
|
|
_transport = new TcpTransport(_session.ServerAddress, _session.Port, _handler);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
2016-10-15 12:35:54 +02:00
|
|
|
|
//set-up layer
|
|
|
|
|
|
var config = new TLRequestGetConfig();
|
2016-10-23 12:02:44 +02:00
|
|
|
|
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"
|
|
|
|
|
|
};
|
2016-10-15 12:35:54 +02:00
|
|
|
|
var invokewithLayer = new TLRequestInvokeWithLayer() { layer = 57, query = request };
|
|
|
|
|
|
await _sender.Send(invokewithLayer);
|
|
|
|
|
|
await _sender.Receive(invokewithLayer);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2016-10-15 12:35:54 +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
|
|
|
|
|
2017-01-27 10:58:47 +01:00
|
|
|
|
_transport = new TcpTransport(dc.ip_address, dc.port, _handler);
|
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
|
|
|
|
{
|
2016-10-30 16:34:12 +01:00
|
|
|
|
if (String.IsNullOrWhiteSpace(phoneNumber))
|
|
|
|
|
|
throw new ArgumentNullException(nameof(phoneNumber));
|
|
|
|
|
|
|
2016-09-24 15:38:26 +02:00
|
|
|
|
if (_sender == null)
|
|
|
|
|
|
throw new InvalidOperationException("Not connected!");
|
|
|
|
|
|
|
|
|
|
|
|
var authCheckPhoneRequest = new TLRequestCheckPhone() { phone_number = phoneNumber };
|
2016-11-27 10:37:15 +01:00
|
|
|
|
var completed = false;
|
|
|
|
|
|
while(!completed)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await _sender.Send(authCheckPhoneRequest);
|
|
|
|
|
|
await _sender.Receive(authCheckPhoneRequest);
|
|
|
|
|
|
completed = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch(PhoneMigrationException e)
|
|
|
|
|
|
{
|
|
|
|
|
|
await ReconnectToDcAsync(e.DC);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-09-24 15:38:26 +02:00
|
|
|
|
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
|
|
|
|
{
|
2016-10-30 16:34:12 +01:00
|
|
|
|
if (String.IsNullOrWhiteSpace(phoneNumber))
|
|
|
|
|
|
throw new ArgumentNullException(nameof(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;
|
|
|
|
|
|
}
|
2016-10-30 08:57:45 +01:00
|
|
|
|
catch (PhoneMigrationException ex)
|
2016-04-18 12:50:57 +02:00
|
|
|
|
{
|
2016-10-22 16:00:15 +02:00
|
|
|
|
await ReconnectToDcAsync(ex.DC);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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-10-30 16:34:12 +01:00
|
|
|
|
if (String.IsNullOrWhiteSpace(phoneNumber))
|
|
|
|
|
|
throw new ArgumentNullException(nameof(phoneNumber));
|
|
|
|
|
|
|
|
|
|
|
|
if (String.IsNullOrWhiteSpace(phoneCodeHash))
|
|
|
|
|
|
throw new ArgumentNullException(nameof(phoneCodeHash));
|
|
|
|
|
|
|
|
|
|
|
|
if (String.IsNullOrWhiteSpace(code))
|
|
|
|
|
|
throw new ArgumentNullException(nameof(code));
|
|
|
|
|
|
|
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-11-16 14:31:00 +01:00
|
|
|
|
public async Task<TLPassword> GetPasswordSetting()
|
|
|
|
|
|
{
|
|
|
|
|
|
var request = new TLRequestGetPassword();
|
|
|
|
|
|
|
|
|
|
|
|
await _sender.Send(request);
|
|
|
|
|
|
await _sender.Receive(request);
|
|
|
|
|
|
|
|
|
|
|
|
return ((TLPassword)request.Response);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<TLUser> MakeAuthWithPasswordAsync(TLPassword password, string password_str)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
byte[] password_bytes = Encoding.UTF8.GetBytes(password_str);
|
|
|
|
|
|
IEnumerable<byte> rv = password.current_salt.Concat(password_bytes).Concat(password.current_salt);
|
|
|
|
|
|
|
|
|
|
|
|
SHA256Managed hashstring = new SHA256Managed();
|
|
|
|
|
|
var password_hash = hashstring.ComputeHash(rv.ToArray());
|
|
|
|
|
|
|
|
|
|
|
|
var request = new TLRequestCheckPassword() { password_hash = password_hash };
|
|
|
|
|
|
await _sender.Send(request);
|
|
|
|
|
|
await _sender.Receive(request);
|
|
|
|
|
|
|
|
|
|
|
|
OnUserAuthenticated(((TLUser)request.Response.user));
|
|
|
|
|
|
|
|
|
|
|
|
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-29 10:47:18 +02:00
|
|
|
|
public async Task<T> SendRequestAsync<T>(TLMethod methodToExecute)
|
2016-09-24 15:38:26 +02:00
|
|
|
|
{
|
2016-10-29 10:47:18 +02:00
|
|
|
|
await _sender.Send(methodToExecute);
|
|
|
|
|
|
await _sender.Receive(methodToExecute);
|
2016-11-07 00:40:19 +01:00
|
|
|
|
|
2016-10-29 10:47:18 +02:00
|
|
|
|
var result = methodToExecute.GetType().GetProperty("Response").GetValue(methodToExecute);
|
2016-10-15 12:35:54 +02:00
|
|
|
|
|
|
|
|
|
|
return (T)result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<TLContacts> GetContactsAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!IsUserAuthorized())
|
|
|
|
|
|
throw new InvalidOperationException("Authorize user first!");
|
|
|
|
|
|
|
|
|
|
|
|
var req = new TLRequestGetContacts() { hash = "" };
|
|
|
|
|
|
|
|
|
|
|
|
return await SendRequestAsync<TLContacts>(req);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<TLAbsUpdates> SendMessageAsync(TLAbsInputPeer peer, string message)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!IsUserAuthorized())
|
|
|
|
|
|
throw new InvalidOperationException("Authorize user first!");
|
|
|
|
|
|
|
|
|
|
|
|
return await SendRequestAsync<TLAbsUpdates>(
|
|
|
|
|
|
new TLRequestSendMessage()
|
|
|
|
|
|
{
|
|
|
|
|
|
peer = peer,
|
|
|
|
|
|
message = message,
|
2016-10-23 12:02:44 +02:00
|
|
|
|
random_id = Helpers.GenerateRandomLong()
|
2016-10-15 12:35:54 +02:00
|
|
|
|
});
|
2016-09-24 15:38:26 +02:00
|
|
|
|
}
|
2016-10-11 15:28:57 +02:00
|
|
|
|
|
2016-10-15 12:35:54 +02:00
|
|
|
|
public async Task<Boolean> SendTypingAsync(TLAbsInputPeer peer)
|
|
|
|
|
|
{
|
|
|
|
|
|
var req = new TLRequestSetTyping()
|
|
|
|
|
|
{
|
|
|
|
|
|
action = new TLSendMessageTypingAction(),
|
|
|
|
|
|
peer = peer
|
|
|
|
|
|
};
|
|
|
|
|
|
return await SendRequestAsync<Boolean>(req);
|
|
|
|
|
|
}
|
2016-10-11 15:28:57 +02:00
|
|
|
|
|
2016-10-26 18:43:19 +02:00
|
|
|
|
public async Task<TLAbsDialogs> GetUserDialogsAsync()
|
2016-10-15 12:35:54 +02:00
|
|
|
|
{
|
|
|
|
|
|
var peer = new TLInputPeerSelf();
|
2016-10-26 18:43:19 +02:00
|
|
|
|
return await SendRequestAsync<TLAbsDialogs>(
|
2016-10-15 12:35:54 +02:00
|
|
|
|
new TLRequestGetDialogs() { offset_date = 0, offset_peer = peer, limit = 100 });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-23 12:02:44 +02:00
|
|
|
|
public async Task<TLAbsUpdates> SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption)
|
2016-10-29 10:47:18 +02:00
|
|
|
|
{
|
2016-10-23 12:02:44 +02:00
|
|
|
|
return await SendRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
|
|
|
|
|
|
{
|
|
|
|
|
|
random_id = Helpers.GenerateRandomLong(),
|
|
|
|
|
|
background = false,
|
|
|
|
|
|
clear_draft = false,
|
|
|
|
|
|
media = new TLInputMediaUploadedPhoto() { file = file, caption = caption },
|
|
|
|
|
|
peer = peer
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<TLAbsUpdates> SendUploadedDocument(
|
|
|
|
|
|
TLAbsInputPeer peer, TLAbsInputFile file, string caption, string mimeType, TLVector<TLAbsDocumentAttribute> attributes)
|
|
|
|
|
|
{
|
2016-10-29 10:47:18 +02:00
|
|
|
|
return await SendRequestAsync<TLAbsUpdates>(new TLRequestSendMedia()
|
2016-10-23 12:02:44 +02:00
|
|
|
|
{
|
|
|
|
|
|
random_id = Helpers.GenerateRandomLong(),
|
|
|
|
|
|
background = false,
|
|
|
|
|
|
clear_draft = false,
|
|
|
|
|
|
media = new TLInputMediaUploadedDocument()
|
|
|
|
|
|
{
|
|
|
|
|
|
file = file,
|
|
|
|
|
|
caption = caption,
|
|
|
|
|
|
mime_type = mimeType,
|
|
|
|
|
|
attributes = attributes
|
|
|
|
|
|
},
|
|
|
|
|
|
peer = peer
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-19 19:31:27 +01:00
|
|
|
|
public async Task<TLFile> GetFile(TLAbsInputFileLocation location, int filePartSize, int offset = 0)
|
2016-10-23 12:29:18 +02:00
|
|
|
|
{
|
2016-10-29 10:47:18 +02:00
|
|
|
|
TLFile result = null;
|
|
|
|
|
|
try
|
2016-10-23 12:29:18 +02:00
|
|
|
|
{
|
2016-10-29 10:47:18 +02:00
|
|
|
|
result = await SendRequestAsync<TLFile>(new TLRequestGetFile()
|
|
|
|
|
|
{
|
|
|
|
|
|
location = location,
|
2017-01-19 19:31:27 +01:00
|
|
|
|
limit = filePartSize,
|
|
|
|
|
|
offset = offset
|
2016-10-29 10:47:18 +02:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
});
|
|
|
|
|
|
result = await GetFile(location, filePartSize);
|
|
|
|
|
|
|
|
|
|
|
|
_session.AuthKey = authKey;
|
|
|
|
|
|
_session.TimeOffset = timeOffset;
|
|
|
|
|
|
_transport = new TcpTransport(serverAddress, serverPort);
|
2016-11-16 14:31:00 +01:00
|
|
|
|
_session.ServerAddress = serverAddress;
|
2016-10-29 10:47:18 +02:00
|
|
|
|
_session.Port = serverPort;
|
|
|
|
|
|
await ConnectAsync();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2016-10-23 12:29:18 +02:00
|
|
|
|
|
2016-11-07 00:40:19 +01:00
|
|
|
|
public async Task SendPingAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
await _sender.SendPingAsync();
|
2017-01-09 18:25:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<TLAbsMessages> GetHistoryAsync(TLAbsInputPeer peer, int offset, int max_id, int limit)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!IsUserAuthorized())
|
|
|
|
|
|
throw new InvalidOperationException("Authorize user first!");
|
|
|
|
|
|
|
|
|
|
|
|
var req = new TLRequestGetHistory()
|
|
|
|
|
|
{
|
|
|
|
|
|
peer = peer,
|
|
|
|
|
|
add_offset = offset,
|
|
|
|
|
|
max_id = max_id,
|
|
|
|
|
|
limit = limit
|
|
|
|
|
|
};
|
|
|
|
|
|
return await SendRequestAsync<TLAbsMessages>(req);
|
2016-11-07 00:40:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-28 15:37:34 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Serch user or chat. API: contacts.search#11f812d8 q:string limit:int = contacts.Found;
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="q">User or chat name</param>
|
|
|
|
|
|
/// <param name="limit">Max result count</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<TLFound> SearchUserAsync(string q, int limit = 10)
|
|
|
|
|
|
{
|
|
|
|
|
|
var r = new TeleSharp.TL.Contacts.TLRequestSearch
|
|
|
|
|
|
{
|
|
|
|
|
|
q = q,
|
|
|
|
|
|
limit = limit
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return await SendRequestAsync<TLFound>(r);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-15 12:35:54 +02:00
|
|
|
|
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-12-15 21:02:23 +01:00
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_transport != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_transport.Dispose();
|
|
|
|
|
|
_transport = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-10-23 17:23:10 +02:00
|
|
|
|
}
|
2016-10-15 12:35:54 +02:00
|
|
|
|
|
2016-10-23 17:23:10 +02:00
|
|
|
|
public class MissingApiConfigurationException : Exception
|
|
|
|
|
|
{
|
|
|
|
|
|
public const string InfoUrl = "https://github.com/sochix/TLSharp#quick-configuration";
|
|
|
|
|
|
|
2016-10-29 10:47:18 +02:00
|
|
|
|
internal MissingApiConfigurationException(string invalidParamName) :
|
2016-10-23 17:23:10 +02:00
|
|
|
|
base($"Your {invalidParamName} setting is missing. Adjust the configuration first, see {InfoUrl}")
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2016-04-18 12:50:57 +02:00
|
|
|
|
}
|
2016-10-30 17:15:10 +01:00
|
|
|
|
|
|
|
|
|
|
public class InvalidPhoneCodeException : Exception
|
|
|
|
|
|
{
|
|
|
|
|
|
internal InvalidPhoneCodeException(string msg) : base(msg) { }
|
|
|
|
|
|
}
|
2016-11-16 14:31:00 +01:00
|
|
|
|
public class CloudPasswordNeededException : Exception
|
|
|
|
|
|
{
|
|
|
|
|
|
internal CloudPasswordNeededException(string msg) : base(msg) { }
|
|
|
|
|
|
}
|
2015-09-28 04:01:17 +02:00
|
|
|
|
}
|