diff --git a/README.md b/README.md index 043beb7..354a7a2 100644 --- a/README.md +++ b/README.md @@ -78,12 +78,9 @@ Currently supported methods: - [Get Contact by Username](#get-contact-by-username) - [Send Message to Contact](#send-message-to-contact) - [Send Media to Contact](#send-media-to-contact) - - [Get Messages History](#get-messages-history) - - [Get UserFull](#get-userfull) - - [Create Chat](#create-chat) - - [Add Chat user](#add-chat-user) - - [Delete Chat user](#delete-chat-user) - - [Leave Chat](#leave-chat) + - [Get Messages History for a Contact](#get-messages-history) + - [Get Updates State](#get-updates-state) + - [Get Updates Difference](#get-updates-difference) ####IsPhoneRegistered Check if phone number registered to Telegram. @@ -203,7 +200,7 @@ var hist = await client.GetMessagesHistoryForContact(userId, offset, limit); **Returns**: **List\**, message history -####Get UserFull +####Get UserFull Request Returns user's full information for specified userId. _Example_: @@ -216,60 +213,31 @@ var userFull = await client.GetUserFull(userId); **Returns**: **UserFull**, User's information -####Create Chat -Creates a new chat. +####Get Updates State +Returns a current state of updates. _Example_: ``` -var statedMessage = await client.CreateChat(title, new List { userId1, userId2 }); +var userFull = await client.GetUpdatesState(); ``` -* title - **string**, chat name -* userIdsToInvite - **List**, list of userIds to invite to chat. Current user will be automatically added to this chat. +**Returns**: **UpdatesState**, Object contains info on state for further updates. -**Returns**: **Messages_statedMessageConstructor**, Message that contains information about created chat. - -####Add Chat user -Adds a user to a chat and sends a service message on it. +####Get Updates Difference +Returns diffetence between the current state of updates and transmitted. _Example_: ``` -var statedMessage = await client.AddChatUser(chatId, userId); +var userFull = await client.GetUpdatesDifference(currentState.pts, currentState.date, currentState.qts); ``` -* chatId - **int**, Chat ID -* userId - **int**, User ID to be added +* lastPts - **int**, The most relevant value of parameter pts of (updates.state) +* lastDate - **int**, The most relevant value of parameter date of (updates.state) +* lastQts - **int**, The most relevant value of parameter qts of (updates.state) -**Returns**: **Messages_statedMessageConstructor**, Message that contains information about modified chat. - -####Delete Chat user -Deletes a user from a chat and sends a service message on it. - -_Example_: - -``` -var statedMessage = await client.DeleteChatUser(chatId, userId); -``` - -* chatId - **int**, Chat ID -* userId - **int**, User ID to be deleted - -**Returns**: **Messages_statedMessageConstructor**, Message that contains information about modified chat. - -####Leave Chat -Leaves the chat by deleting currently authenticated user from it. - -_Example_: - -``` -var statedMessage = await client.LeaveChat(chatId); -``` - -* chatId - **int**, Chat ID - -**Returns**: **Messages_statedMessageConstructor**, Message that contains information about modified chat. +**Returns**: **UpdatesDifference**, Occurred changes. ## Contributing diff --git a/TLSharp.Core/Requests/GetUpdatesDifferenceRequest.cs b/TLSharp.Core/Requests/GetUpdatesDifferenceRequest.cs new file mode 100644 index 0000000..ac02f4b --- /dev/null +++ b/TLSharp.Core/Requests/GetUpdatesDifferenceRequest.cs @@ -0,0 +1,43 @@ +using System; +using System.IO; +using TLSharp.Core.MTProto; + +namespace TLSharp.Core.Requests +{ + public class GetUpdatesDifferenceRequest : MTProtoRequest + { + public readonly int pts; + public readonly int date; + public readonly int qts; + + public GetUpdatesDifferenceRequest(int pts, int date, int qts) + { + this.pts = pts; + this.date = date; + this.qts = qts; + } + + public updates_Difference updatesDifference { get; private set; } + + public override void OnSend(BinaryWriter writer) + { + writer.Write(0xa041495); + writer.Write(pts); + writer.Write(date); + writer.Write(qts); + } + + public override void OnResponse(BinaryReader reader) + { + updatesDifference = TL.Parse(reader); + } + + public override void OnException(Exception exception) + { + throw new NotImplementedException(); + } + + public override bool Confirmed => true; + public override bool Responded { get; } + } +} diff --git a/TLSharp.Core/Requests/GetUpdatesStateRequest.cs b/TLSharp.Core/Requests/GetUpdatesStateRequest.cs new file mode 100644 index 0000000..48c9289 --- /dev/null +++ b/TLSharp.Core/Requests/GetUpdatesStateRequest.cs @@ -0,0 +1,29 @@ +using System; +using System.IO; +using TLSharp.Core.MTProto; + +namespace TLSharp.Core.Requests +{ + public class GetUpdatesStateRequest : MTProtoRequest + { + public updates_State updates { get; private set; } + + public override void OnSend(BinaryWriter writer) + { + writer.Write(0xedd4882a); + } + + public override void OnResponse(BinaryReader reader) + { + updates = TL.Parse(reader); + } + + public override void OnException(Exception exception) + { + throw new NotImplementedException(); + } + + public override bool Confirmed => true; + public override bool Responded { get; } + } +} \ No newline at end of file diff --git a/TLSharp.Core/TLSharp.Core.csproj b/TLSharp.Core/TLSharp.Core.csproj index f52c911..eb52c27 100644 --- a/TLSharp.Core/TLSharp.Core.csproj +++ b/TLSharp.Core/TLSharp.Core.csproj @@ -69,11 +69,13 @@ + + diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 9e741f2..cd33280 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -278,7 +278,7 @@ namespace TLSharp.Core Messages = request.messages, Chats = request.chats, Users = request.users, - }; + }; } public async Task GetUserFull(int user_id) @@ -312,26 +312,26 @@ namespace TLSharp.Core public async Task CreateChat(string title, List userPhonesToInvite) { - var userIdsToInvite = new List(); - foreach (var userPhone in userPhonesToInvite) - { - var uid = await ImportContactByPhoneNumber(userPhone); - if (!uid.HasValue) - throw new InvalidOperationException($"Failed to retrieve contact {userPhone}"); + var request = new GetUpdatesStateRequest(); - userIdsToInvite.Add(uid.Value); - } + await _sender.Send(request); + await _sender.Receive(request); - return await CreateChat(title, userIdsToInvite); + return request.updates; } - public async Task CreateChat(string title, List userIdsToInvite) + public async Task GetUpdatesDifference(int lastPts, int lastDate, int lastQts) { - var request = new CreateChatRequest(userIdsToInvite.Select(uid => new InputUserContactConstructor(uid)).ToList(), title); + var request = new GetUpdatesDifferenceRequest(lastPts, lastDate, lastQts); await _sender.Send(request); await _sender.Receive(request); + return request.updatesDifference; + } + await _sender.Send(request); + await _sender.Receive(request); + return request.message; } diff --git a/TLSharp.Tests/TLSharpTests.cs b/TLSharp.Tests/TLSharpTests.cs index 7012cbc..925347c 100644 --- a/TLSharp.Tests/TLSharpTests.cs +++ b/TLSharp.Tests/TLSharpTests.cs @@ -376,5 +376,42 @@ namespace TLSharp.Tests return client; } + + [TestMethod] + public async Task GetUpdates() + { + var store = new FileSessionStore(); + var client = new TelegramClient(store, "session", apiId, apiHash); + await client.Connect(); + + Assert.IsTrue(client.IsUserAuthorized()); + + var updatesState = await client.GetUpdatesState(); + var initialState = updatesState as Updates_stateConstructor; + + Assert.IsNotNull(initialState); + + var difference = await client.GetUpdatesDifference(initialState.pts, initialState.date, initialState.qts); + Assert.IsNotNull(difference); + Assert.AreEqual(difference.Constructor, Constructor.updates_differenceEmpty); + + var userIdToSendMessage = await client.ImportContactByPhoneNumber(NumberToSendMessage); + + await client.SendMessage(userIdToSendMessage.Value, "test"); + + var differenceAfterMessage = await client.GetUpdatesDifference(initialState.pts, initialState.date, initialState.qts); + + Assert.IsNotNull(differenceAfterMessage); + Assert.AreEqual(differenceAfterMessage.Constructor, Constructor.updates_difference); + + var differenceUpdate = differenceAfterMessage as Updates_differenceConstructor; + Assert.IsNotNull(differenceUpdate); + Assert.AreEqual(1, differenceUpdate.new_messages.Count); + + var messageUpdate = differenceUpdate.new_messages[0] as MessageConstructor; + Assert.IsNotNull(messageUpdate); + + Assert.AreEqual("test", messageUpdate.message); + } } }