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;
|
2020-01-24 19:28:37 +01:00
|
|
|
|
using TLSharp.Core.Exceptions;
|
2016-10-11 15:32:38 +02:00
|
|
|
|
using TLSharp.Core.MTProto.Crypto;
|
|
|
|
|
|
using TLSharp.Core.Network;
|
2020-01-24 19:28:37 +01:00
|
|
|
|
using TLSharp.Core.Network.Exceptions;
|
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 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
|
|
|
|
|
2019-10-02 16:48:42 +02:00
|
|
|
|
public Session Session
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _session; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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-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);
|
2019-02-10 13:53:46 +01:00
|
|
|
|
_transport = new TcpTransport(_session.DataCenter.Address, _session.DataCenter.Port, _handler);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-01 16:41:35 +01:00
|
|
|
|
public async Task 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()
|
|
|
|
|
|
{
|
2017-11-09 11:35:15 +01:00
|
|
|
|
ApiId = _apiId,
|
|
|
|
|
|
AppVersion = "1.0.0",
|
|
|
|
|
|
DeviceModel = "PC",
|
|
|
|
|
|
LangCode = "en",
|
|
|
|
|
|
Query = config,
|
|
|
|
|
|
SystemVersion = "Win 10.0"
|
2016-10-23 12:02:44 +02:00
|
|
|
|
};
|
2017-11-09 11:35:15 +01:00
|
|
|
|
var invokewithLayer = new TLRequestInvokeWithLayer() { Layer = 66, Query = request };
|
2016-10-15 12:35:54 +02:00
|
|
|
|
await _sender.Send(invokewithLayer);
|
|
|
|
|
|
await _sender.Receive(invokewithLayer);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2017-11-09 10:52:29 +01:00
|
|
|
|
dcOptions = ((TLConfig)invokewithLayer.Response).DcOptions.ToList();
|
2016-04-18 12:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
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.");
|
2018-03-03 17:38:51 +01:00
|
|
|
|
|
|
|
|
|
|
TLExportedAuthorization exported = null;
|
|
|
|
|
|
if (_session.TLUser != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
TLRequestExportAuthorization exportAuthorization = new TLRequestExportAuthorization() { DcId = dcId };
|
|
|
|
|
|
exported = await SendRequestAsync<TLExportedAuthorization>(exportAuthorization);
|
|
|
|
|
|
}
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
var dc = dcOptions.First(d => d.Id == dcId);
|
2019-02-10 13:53:46 +01:00
|
|
|
|
var dataCenter = new DataCenter (dcId, dc.IpAddress, dc.Port);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
_transport = new TcpTransport(dc.IpAddress, dc.Port, _handler);
|
2019-02-10 13:53:46 +01:00
|
|
|
|
_session.DataCenter = dataCenter;
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2018-03-03 17:38:51 +01:00
|
|
|
|
await ConnectAsync(true);
|
|
|
|
|
|
|
|
|
|
|
|
if (_session.TLUser != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
TLRequestImportAuthorization importAuthorization = new TLRequestImportAuthorization() { Id = exported.Id, Bytes = exported.Bytes };
|
|
|
|
|
|
var imported = await SendRequestAsync<TLAuthorization>(importAuthorization);
|
|
|
|
|
|
OnUserAuthenticated(((TLUser)imported.User));
|
2017-11-09 13:21:10 +01:00
|
|
|
|
}
|
2016-04-18 12:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-03 17:38:51 +01:00
|
|
|
|
private async Task RequestWithDcMigration(TLMethod request)
|
|
|
|
|
|
{
|
2018-02-28 01:43:28 +01:00
|
|
|
|
if (_sender == null)
|
2018-03-03 17:38:51 +01:00
|
|
|
|
throw new InvalidOperationException("Not connected!");
|
|
|
|
|
|
|
2017-11-07 16:34:40 +01:00
|
|
|
|
var completed = false;
|
|
|
|
|
|
while(!completed)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await _sender.Send(request);
|
|
|
|
|
|
await _sender.Receive(request);
|
|
|
|
|
|
completed = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch(DataCenterMigrationException e)
|
|
|
|
|
|
{
|
2019-02-10 13:53:46 +01:00
|
|
|
|
if (_session.DataCenter.DataCenterId.HasValue &&
|
|
|
|
|
|
_session.DataCenter.DataCenterId.Value == e.DC)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"Telegram server replied requesting a migration to DataCenter {e.DC} when this connection was already using this DataCenter", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 16:34:40 +01:00
|
|
|
|
await ReconnectToDcAsync(e.DC);
|
2017-11-20 21:27:23 +01:00
|
|
|
|
// prepare the request for another try
|
|
|
|
|
|
request.ConfirmReceived = false;
|
2017-11-07 16:34:40 +01:00
|
|
|
|
}
|
2018-03-03 17:38:51 +01:00
|
|
|
|
}
|
2017-11-07 16:34:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
var authCheckPhoneRequest = new TLRequestCheckPhone() { PhoneNumber = phoneNumber };
|
2017-11-07 16:34:40 +01:00
|
|
|
|
|
|
|
|
|
|
await RequestWithDcMigration(authCheckPhoneRequest);
|
|
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
return authCheckPhoneRequest.Response.PhoneRegistered;
|
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));
|
|
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
var request = new TLRequestSendCode() { PhoneNumber = phoneNumber, ApiId = _apiId, ApiHash = _apiHash };
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2017-11-07 16:34:40 +01:00
|
|
|
|
await RequestWithDcMigration(request);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
return request.Response.PhoneCodeHash;
|
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));
|
2017-11-09 11:35:15 +01:00
|
|
|
|
|
2018-03-03 17:38:51 +01:00
|
|
|
|
var request = new TLRequestSignIn() { PhoneNumber = phoneNumber, PhoneCodeHash = phoneCodeHash, PhoneCode = code };
|
|
|
|
|
|
|
2017-11-07 16:34:40 +01:00
|
|
|
|
await RequestWithDcMigration(request);
|
2016-01-17 10:16:44 +01:00
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
OnUserAuthenticated(((TLUser)request.Response.User));
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
return ((TLUser)request.Response.User);
|
2016-09-06 17:37:05 +02:00
|
|
|
|
}
|
2017-11-07 16:34:40 +01:00
|
|
|
|
|
2016-11-16 14:31:00 +01:00
|
|
|
|
public async Task<TLPassword> GetPasswordSetting()
|
|
|
|
|
|
{
|
|
|
|
|
|
var request = new TLRequestGetPassword();
|
|
|
|
|
|
|
2017-11-07 16:34:40 +01:00
|
|
|
|
await RequestWithDcMigration(request);
|
2016-11-16 14:31:00 +01:00
|
|
|
|
|
|
|
|
|
|
return ((TLPassword)request.Response);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<TLUser> MakeAuthWithPasswordAsync(TLPassword password, string password_str)
|
|
|
|
|
|
{
|
2017-11-09 11:35:15 +01:00
|
|
|
|
byte[] password_Bytes = Encoding.UTF8.GetBytes(password_str);
|
|
|
|
|
|
IEnumerable<byte> rv = password.CurrentSalt.Concat(password_Bytes).Concat(password.CurrentSalt);
|
2016-11-16 14:31:00 +01:00
|
|
|
|
|
|
|
|
|
|
SHA256Managed hashstring = new SHA256Managed();
|
|
|
|
|
|
var password_hash = hashstring.ComputeHash(rv.ToArray());
|
|
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
var request = new TLRequestCheckPassword() { PasswordHash = password_hash };
|
2017-11-07 16:34:40 +01:00
|
|
|
|
|
|
|
|
|
|
await RequestWithDcMigration(request);
|
2016-11-16 14:31:00 +01:00
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
OnUserAuthenticated(((TLUser)request.Response.User));
|
2016-11-16 14:31:00 +01:00
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
return ((TLUser)request.Response.User);
|
2016-11-16 14:31:00 +01:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2017-11-09 11:35:15 +01:00
|
|
|
|
var request = new TLRequestSignUp() { PhoneNumber = phoneNumber, PhoneCode = code, PhoneCodeHash = phoneCodeHash, FirstName = firstName, LastName = lastName };
|
2017-11-07 16:34:40 +01:00
|
|
|
|
|
|
|
|
|
|
await RequestWithDcMigration(request);
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
OnUserAuthenticated(((TLUser)request.Response.User));
|
2016-04-18 12:50:57 +02:00
|
|
|
|
|
2017-11-09 11:35:15 +01:00
|
|
|
|
return ((TLUser)request.Response.User);
|
2016-09-24 15:38:26 +02:00
|
|
|
|
}
|
2020-01-25 09:16:14 +01:00
|
|
|
|
|
2016-10-29 10:47:18 +02:00
|
|
|
|
public async Task<T> SendRequestAsync<T>(TLMethod methodToExecute)
|
2016-09-24 15:38:26 +02:00
|
|
|
|
{
|
2017-11-07 16:34:40 +01:00
|
|
|
|
await RequestWithDcMigration(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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-25 09:16:14 +01:00
|
|
|
|
private async Task<T> SendAuthenticatedRequestAsync<T> (TLMethod methodToExecute)
|
2020-01-23 08:11:24 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (!IsUserAuthorized())
|
|
|
|
|
|
throw new InvalidOperationException("Authorize user first!");
|
|
|
|
|
|
|
2020-01-25 09:16:14 +01:00
|
|
|
|
return await SendRequestAsync<T>(methodToExecute);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<TLUser> UpdateUsernameAsync(string username)
|
|
|
|
|
|
{
|
2020-01-23 08:11:24 +01:00
|
|
|
|
var req = new TLRequestUpdateUsername { Username = username };
|
|
|
|
|
|
|
2020-01-25 09:16:14 +01:00
|
|
|
|
return await SendAuthenticatedRequestAsync<TLUser>(req);
|
2020-01-23 08:11:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<bool> CheckUsernameAsync(string username)
|
|
|
|
|
|
{
|
|
|
|
|
|
var req = new TLRequestCheckUsername { Username = username };
|
|
|
|
|
|
|
2020-01-25 09:16:14 +01:00
|
|
|
|
return await SendAuthenticatedRequestAsync<bool>(req);
|
2020-01-23 08:11:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-23 05:02:19 +01:00
|
|
|
|
public async Task<TLImportedContacts> ImportContactsAsync(IReadOnlyList<TLInputPhoneContact> contacts)
|
|
|
|
|
|
{
|
|
|
|
|
|
var req = new TLRequestImportContacts { Contacts = new TLVector<TLInputPhoneContact>(contacts)};
|
|
|
|
|
|
|
2020-01-25 09:16:14 +01:00
|
|
|
|
return await SendAuthenticatedRequestAsync<TLImportedContacts>(req);
|
2020-01-23 05:02:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<bool> DeleteContactsAsync(IReadOnlyList<TLAbsInputUser> users)
|
|
|
|
|
|
{
|
|
|
|
|
|
var req = new TLRequestDeleteContacts {Id = new TLVector<TLAbsInputUser>(users)};
|
|
|
|
|
|
|
2020-01-25 09:16:14 +01:00
|
|
|
|
return await SendAuthenticatedRequestAsync<bool>(req);
|
2020-01-23 05:02:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<TLLink> DeleteContactAsync(TLAbsInputUser user)
|
|
|
|
|
|
{
|
|
|
|
|
|
var req = new TLRequestDeleteContact {Id = user};
|
|
|
|
|
|
|
2020-01-25 09:16:14 +01:00
|
|
|
|
return await SendAuthenticatedRequestAsync<TLLink>(req);
|
2020-01-23 05:02:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-10-15 12:35:54 +02:00
|
|
|
|
public async Task<TLContacts> GetContactsAsync()
|
|
|
|
|
|
{
|
2017-11-09 11:35:15 +01:00
|
|
|
|
var req = new TLRequestGetContacts() { Hash = "" };
|
2016-10-15 12:35:54 +02:00
|
|
|
|
|
2020-01-25 09:16:14 +01:00
|
|
|
|
return await SendAuthenticatedRequestAsync<TLContacts>(req);
|
2016-10-15 12:35:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<TLAbsUpdates> SendMessageAsync(TLAbsInputPeer peer, string message)
|
|
|
|
|
|
{
|
2020-01-25 09:16:14 +01:00
|
|
|
|
return await SendAuthenticatedRequestAsync<TLAbsUpdates>(
|
2016-10-15 12:35:54 +02:00
|
|
|
|
new TLRequestSendMessage()
|
|
|
|
|
|
{
|
2017-11-09 11:35:15 +01:00
|
|
|
|
Peer = peer,
|
|
|
|
|
|
Message = message,
|
|
|
|
|
|
RandomId = 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()
|
|
|
|
|
|
{
|
2017-11-09 11:35:15 +01:00
|
|
|
|
Action = new TLSendMessageTypingAction(),
|
|
|
|
|
|
Peer = peer
|
2016-10-15 12:35:54 +02:00
|
|
|
|
};
|
|
|
|
|
|
return await SendRequestAsync<Boolean>(req);
|
|
|
|
|
|
}
|
2016-10-11 15:28:57 +02:00
|
|
|
|
|
2019-01-04 11:16:35 +01:00
|
|
|
|
public async Task<TLAbsDialogs> GetUserDialogsAsync(int offsetDate = 0, int offsetId = 0, TLAbsInputPeer offsetPeer = null, int limit = 100)
|
2016-10-15 12:35:54 +02:00
|
|
|
|
{
|
2019-01-04 11:16:35 +01:00
|
|
|
|
if (offsetPeer == null)
|
|
|
|
|
|
offsetPeer = new TLInputPeerSelf();
|
|
|
|
|
|
|
|
|
|
|
|
var req = new TLRequestGetDialogs()
|
|
|
|
|
|
{
|
|
|
|
|
|
OffsetDate = offsetDate,
|
|
|
|
|
|
OffsetId = offsetId,
|
|
|
|
|
|
OffsetPeer = offsetPeer,
|
|
|
|
|
|
Limit = limit
|
|
|
|
|
|
};
|
2020-01-25 09:16:14 +01:00
|
|
|
|
return await SendAuthenticatedRequestAsync<TLAbsDialogs>(req);
|
2016-10-15 12:35:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
{
|
2017-11-09 11:35:15 +01:00
|
|
|
|
RandomId = Helpers.GenerateRandomLong(),
|
|
|
|
|
|
Background = false,
|
|
|
|
|
|
ClearDraft = false,
|
|
|
|
|
|
Media = new TLInputMediaUploadedPhoto() { File = file, Caption = caption },
|
|
|
|
|
|
Peer = peer
|
2016-10-23 12:02:44 +02:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2017-11-09 11:35:15 +01:00
|
|
|
|
RandomId = Helpers.GenerateRandomLong(),
|
|
|
|
|
|
Background = false,
|
|
|
|
|
|
ClearDraft = false,
|
|
|
|
|
|
Media = new TLInputMediaUploadedDocument()
|
2016-10-23 12:02:44 +02:00
|
|
|
|
{
|
2017-11-09 11:35:15 +01:00
|
|
|
|
File = file,
|
|
|
|
|
|
Caption = caption,
|
|
|
|
|
|
MimeType = mimeType,
|
|
|
|
|
|
Attributes = attributes
|
2016-10-23 12:02:44 +02:00
|
|
|
|
},
|
2017-11-09 11:35:15 +01:00
|
|
|
|
Peer = peer
|
2016-10-23 12:02:44 +02:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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;
|
2017-11-15 22:04:01 +01:00
|
|
|
|
result = await SendRequestAsync<TLFile>(new TLRequestGetFile()
|
2016-10-23 12:29:18 +02:00
|
|
|
|
{
|
2017-11-15 22:04:01 +01:00
|
|
|
|
Location = location,
|
|
|
|
|
|
Limit = filePartSize,
|
|
|
|
|
|
Offset = offset
|
|
|
|
|
|
});
|
2016-10-29 10:47:18 +02:00
|
|
|
|
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();
|
2018-03-03 17:38:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-04 11:16:35 +01:00
|
|
|
|
public async Task<TLAbsMessages> GetHistoryAsync(TLAbsInputPeer peer, int offsetId = 0, int offsetDate = 0, int addOffset = 0, int limit = 100, int maxId = 0, int minId = 0)
|
2018-03-03 17:38:51 +01:00
|
|
|
|
{
|
|
|
|
|
|
var req = new TLRequestGetHistory()
|
|
|
|
|
|
{
|
|
|
|
|
|
Peer = peer,
|
2019-01-04 11:16:35 +01:00
|
|
|
|
OffsetId = offsetId,
|
|
|
|
|
|
OffsetDate = offsetDate,
|
|
|
|
|
|
AddOffset = addOffset,
|
|
|
|
|
|
Limit = limit,
|
|
|
|
|
|
MaxId = maxId,
|
|
|
|
|
|
MinId = minId
|
2018-03-03 17:38:51 +01:00
|
|
|
|
};
|
2020-01-25 09:16:14 +01:00
|
|
|
|
return await SendAuthenticatedRequestAsync<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
|
|
|
|
|
|
{
|
2017-11-09 11:35:15 +01:00
|
|
|
|
Q = q,
|
|
|
|
|
|
Limit = limit
|
2016-12-28 15:37:34 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
2018-03-03 17:38:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsConnected
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_transport == null)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
return _transport.IsConnected;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2015-09-28 04:01:17 +02:00
|
|
|
|
}
|