From 78e3a7794f6b4111e09df7178c367f158f6d5278 Mon Sep 17 00:00:00 2001 From: steavy29 Date: Wed, 14 Sep 2016 14:57:07 +0300 Subject: [PATCH] Merge fix. --- README.md | 66 ++++++++++++++++++++++++++++++++-- TLSharp.Core/TelegramClient.cs | 42 ++++++++++++++++------ TLSharp.Tests/TLSharpTests.cs | 10 ++++-- TLSharp.Tests/app.config | 1 + 4 files changed, 103 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 354a7a2..3b431d1 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,12 @@ 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 for a Contact](#get-messages-history) + - [Get Messages History for Contact](#get-messages-history-for-contact) + - [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 Updates State](#get-updates-state) - [Get Updates Difference](#get-updates-difference) @@ -185,7 +190,7 @@ var res = await client.SendMediaMessage(userId, mediaFile); **Returns**: **bool**, file sent or not -####Get Messages History +####Get Messages History for Contact Returns messages history for specified userId. _Example_: @@ -200,7 +205,7 @@ var hist = await client.GetMessagesHistoryForContact(userId, offset, limit); **Returns**: **List\**, message history -####Get UserFull Request +####Get UserFull Returns user's full information for specified userId. _Example_: @@ -213,6 +218,61 @@ var userFull = await client.GetUserFull(userId); **Returns**: **UserFull**, User's information +####Create Chat +Creates a new chat. + +_Example_: + +``` +var statedMessage = await client.CreateChat(title, new List { userId1, userId2 }); +``` + +* title - **string**, chat name +* userIdsToInvite - **List**, list of userIds to invite to chat. Current user will be automatically added to this chat. + +**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. + +_Example_: + +``` +var statedMessage = await client.AddChatUser(chatId, userId); +``` + +* chatId - **int**, Chat ID +* userId - **int**, User ID to be added + +**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. + ####Get Updates State Returns a current state of updates. diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index cd33280..4253168 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -312,26 +312,26 @@ namespace TLSharp.Core public async Task CreateChat(string title, List userPhonesToInvite) { - var request = new GetUpdatesStateRequest(); + 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}"); - await _sender.Send(request); - await _sender.Receive(request); + userIdsToInvite.Add(uid.Value); + } - return request.updates; + return await CreateChat(title, userIdsToInvite); } - public async Task GetUpdatesDifference(int lastPts, int lastDate, int lastQts) + public async Task CreateChat(string title, List userIdsToInvite) { - var request = new GetUpdatesDifferenceRequest(lastPts, lastDate, lastQts); + var request = new CreateChatRequest(userIdsToInvite.Select(uid => new InputUserContactConstructor(uid)).ToList(), title); await _sender.Send(request); await _sender.Receive(request); - return request.updatesDifference; - } - await _sender.Send(request); - await _sender.Receive(request); - return request.message; } @@ -359,5 +359,25 @@ namespace TLSharp.Core { return await DeleteChatUser(chatId, ((UserSelfConstructor) _session.User).id); } + + public async Task GetUpdatesState() + { + var request = new GetUpdatesStateRequest(); + + await _sender.Send(request); + await _sender.Receive(request); + + return request.updates; + } + + public async Task GetUpdatesDifference(int lastPts, int lastDate, int lastQts) + { + var request = new GetUpdatesDifferenceRequest(lastPts, lastDate, lastQts); + + await _sender.Send(request); + await _sender.Receive(request); + + return request.updatesDifference; + } } } diff --git a/TLSharp.Tests/TLSharpTests.cs b/TLSharp.Tests/TLSharpTests.cs index 925347c..d5ed580 100644 --- a/TLSharp.Tests/TLSharpTests.cs +++ b/TLSharp.Tests/TLSharpTests.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.IO; @@ -25,6 +26,8 @@ namespace TLSharp.Tests private string NumberToGetUserFull { get; set; } + private string NumberToAddToChat { get; set; } + private string apiHash = ""; private int apiId = 0; @@ -53,6 +56,9 @@ namespace TLSharp.Tests if (string.IsNullOrEmpty(NumberToGetUserFull)) Debug.WriteLine("NumberToGetUserFull not configured in app.config! Some tests may fail."); + NumberToAddToChat = ConfigurationManager.AppSettings[nameof(NumberToAddToChat)]; + if (string.IsNullOrEmpty(NumberToAddToChat)) + Debug.WriteLine("NumberToAddToChat not configured in app.config! Some tests may fail."); } [TestMethod] @@ -321,7 +327,7 @@ namespace TLSharp.Tests var createdChat = GetChatFromStatedMessage(statedMessageAfterCreation); - var addUserId = await client.ImportContactByPhoneNumber("380685004559"); + var addUserId = await client.ImportContactByPhoneNumber(NumberToAddToChat); var statedMessageAfterAddUser = await client.AddChatUser(createdChat.id, addUserId.Value); var modifiedChat = GetChatFromStatedMessage(statedMessageAfterAddUser); diff --git a/TLSharp.Tests/app.config b/TLSharp.Tests/app.config index 896eeec..62aae65 100644 --- a/TLSharp.Tests/app.config +++ b/TLSharp.Tests/app.config @@ -6,5 +6,6 @@ +