mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Merge branch 'pr/203'
Conflicts: README.md TLSharp.Core/TelegramClient.cs TLSharp.Tests/TLSharpTests.cs
This commit is contained in:
commit
8411235fe1
62
README.md
62
README.md
|
|
@ -78,12 +78,9 @@ Currently supported methods:
|
||||||
- [Get Contact by Username](#get-contact-by-username)
|
- [Get Contact by Username](#get-contact-by-username)
|
||||||
- [Send Message to Contact](#send-message-to-contact)
|
- [Send Message to Contact](#send-message-to-contact)
|
||||||
- [Send Media to Contact](#send-media-to-contact)
|
- [Send Media to Contact](#send-media-to-contact)
|
||||||
- [Get Messages History](#get-messages-history)
|
- [Get Messages History for a Contact](#get-messages-history)
|
||||||
- [Get UserFull](#get-userfull)
|
- [Get Updates State](#get-updates-state)
|
||||||
- [Create Chat](#create-chat)
|
- [Get Updates Difference](#get-updates-difference)
|
||||||
- [Add Chat user](#add-chat-user)
|
|
||||||
- [Delete Chat user](#delete-chat-user)
|
|
||||||
- [Leave Chat](#leave-chat)
|
|
||||||
|
|
||||||
####IsPhoneRegistered
|
####IsPhoneRegistered
|
||||||
Check if phone number registered to Telegram.
|
Check if phone number registered to Telegram.
|
||||||
|
|
@ -203,7 +200,7 @@ var hist = await client.GetMessagesHistoryForContact(userId, offset, limit);
|
||||||
|
|
||||||
**Returns**: **List\<Message\>**, message history
|
**Returns**: **List\<Message\>**, message history
|
||||||
|
|
||||||
####Get UserFull
|
####Get UserFull Request
|
||||||
Returns user's full information for specified userId.
|
Returns user's full information for specified userId.
|
||||||
|
|
||||||
_Example_:
|
_Example_:
|
||||||
|
|
@ -216,60 +213,31 @@ var userFull = await client.GetUserFull(userId);
|
||||||
|
|
||||||
**Returns**: **UserFull**, User's information
|
**Returns**: **UserFull**, User's information
|
||||||
|
|
||||||
####Create Chat
|
####Get Updates State
|
||||||
Creates a new chat.
|
Returns a current state of updates.
|
||||||
|
|
||||||
_Example_:
|
_Example_:
|
||||||
|
|
||||||
```
|
```
|
||||||
var statedMessage = await client.CreateChat(title, new List<string> { userId1, userId2 });
|
var userFull = await client.GetUpdatesState();
|
||||||
```
|
```
|
||||||
|
|
||||||
* title - **string**, chat name
|
**Returns**: **UpdatesState**, Object contains info on state for further updates.
|
||||||
* userIdsToInvite - **List<int>**, 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.
|
####Get Updates Difference
|
||||||
|
Returns diffetence between the current state of updates and transmitted.
|
||||||
####Add Chat user
|
|
||||||
Adds a user to a chat and sends a service message on it.
|
|
||||||
|
|
||||||
_Example_:
|
_Example_:
|
||||||
|
|
||||||
```
|
```
|
||||||
var statedMessage = await client.AddChatUser(chatId, userId);
|
var userFull = await client.GetUpdatesDifference(currentState.pts, currentState.date, currentState.qts);
|
||||||
```
|
```
|
||||||
|
|
||||||
* chatId - **int**, Chat ID
|
* lastPts - **int**, The most relevant value of parameter pts of (updates.state)
|
||||||
* userId - **int**, User ID to be added
|
* 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.
|
**Returns**: **UpdatesDifference**, Occurred changes.
|
||||||
|
|
||||||
####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.
|
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
|
|
|
||||||
43
TLSharp.Core/Requests/GetUpdatesDifferenceRequest.cs
Normal file
43
TLSharp.Core/Requests/GetUpdatesDifferenceRequest.cs
Normal file
|
|
@ -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<updates_Difference>(reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnException(Exception exception)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Confirmed => true;
|
||||||
|
public override bool Responded { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
29
TLSharp.Core/Requests/GetUpdatesStateRequest.cs
Normal file
29
TLSharp.Core/Requests/GetUpdatesStateRequest.cs
Normal file
|
|
@ -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<updates_State>(reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnException(Exception exception)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Confirmed => true;
|
||||||
|
public override bool Responded { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -69,11 +69,13 @@
|
||||||
<Compile Include="Requests\AuthSendSmsRequest.cs" />
|
<Compile Include="Requests\AuthSendSmsRequest.cs" />
|
||||||
<Compile Include="Requests\AuthSignInRequest.cs" />
|
<Compile Include="Requests\AuthSignInRequest.cs" />
|
||||||
<Compile Include="Requests\AuthSignUpRequest.cs" />
|
<Compile Include="Requests\AuthSignUpRequest.cs" />
|
||||||
|
<Compile Include="Requests\GetUpdatesDifferenceRequest.cs" />
|
||||||
<Compile Include="Requests\GetContactsRequest.cs" />
|
<Compile Include="Requests\GetContactsRequest.cs" />
|
||||||
<Compile Include="Requests\GetDialogsRequest.cs" />
|
<Compile Include="Requests\GetDialogsRequest.cs" />
|
||||||
<Compile Include="Requests\GetFileRequest.cs" />
|
<Compile Include="Requests\GetFileRequest.cs" />
|
||||||
<Compile Include="Requests\GetHistoryRequest.cs" />
|
<Compile Include="Requests\GetHistoryRequest.cs" />
|
||||||
<Compile Include="Requests\GetNearestDcRequest.cs" />
|
<Compile Include="Requests\GetNearestDcRequest.cs" />
|
||||||
|
<Compile Include="Requests\GetUpdatesStateRequest.cs" />
|
||||||
<Compile Include="Requests\GetUserFullRequest.cs" />
|
<Compile Include="Requests\GetUserFullRequest.cs" />
|
||||||
<Compile Include="Requests\GetUsersRequest.cs" />
|
<Compile Include="Requests\GetUsersRequest.cs" />
|
||||||
<Compile Include="Requests\ImportByUserName.cs" />
|
<Compile Include="Requests\ImportByUserName.cs" />
|
||||||
|
|
|
||||||
|
|
@ -312,26 +312,26 @@ namespace TLSharp.Core
|
||||||
|
|
||||||
public async Task<Messages_statedMessageConstructor> CreateChat(string title, List<string> userPhonesToInvite)
|
public async Task<Messages_statedMessageConstructor> CreateChat(string title, List<string> userPhonesToInvite)
|
||||||
{
|
{
|
||||||
var userIdsToInvite = new List<int>();
|
var request = new GetUpdatesStateRequest();
|
||||||
foreach (var userPhone in userPhonesToInvite)
|
|
||||||
{
|
|
||||||
var uid = await ImportContactByPhoneNumber(userPhone);
|
|
||||||
if (!uid.HasValue)
|
|
||||||
throw new InvalidOperationException($"Failed to retrieve contact {userPhone}");
|
|
||||||
|
|
||||||
userIdsToInvite.Add(uid.Value);
|
await _sender.Send(request);
|
||||||
|
await _sender.Receive(request);
|
||||||
|
|
||||||
|
return request.updates;
|
||||||
}
|
}
|
||||||
|
|
||||||
return await CreateChat(title, userIdsToInvite);
|
public async Task<updates_Difference> GetUpdatesDifference(int lastPts, int lastDate, int lastQts)
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<Messages_statedMessageConstructor> CreateChat(string title, List<int> userIdsToInvite)
|
|
||||||
{
|
{
|
||||||
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.Send(request);
|
||||||
await _sender.Receive(request);
|
await _sender.Receive(request);
|
||||||
|
|
||||||
|
return request.updatesDifference;
|
||||||
|
}
|
||||||
|
await _sender.Send(request);
|
||||||
|
await _sender.Receive(request);
|
||||||
|
|
||||||
return request.message;
|
return request.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -376,5 +376,42 @@ namespace TLSharp.Tests
|
||||||
|
|
||||||
return client;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue