mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Merge branch 'pr/198'
Conflicts: TLSharp.Tests/TLSharpTests.cs
This commit is contained in:
commit
148828edc1
64
README.md
64
README.md
|
|
@ -78,7 +78,12 @@ 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 for a Contact](#get-messages-history)
|
- [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)
|
||||||
|
|
||||||
####IsPhoneRegistered
|
####IsPhoneRegistered
|
||||||
Check if phone number registered to Telegram.
|
Check if phone number registered to Telegram.
|
||||||
|
|
@ -198,7 +203,7 @@ var hist = await client.GetMessagesHistoryForContact(userId, offset, limit);
|
||||||
|
|
||||||
**Returns**: **List\<Message\>**, message history
|
**Returns**: **List\<Message\>**, message history
|
||||||
|
|
||||||
####Get UserFull Request
|
####Get UserFull
|
||||||
Returns user's full information for specified userId.
|
Returns user's full information for specified userId.
|
||||||
|
|
||||||
_Example_:
|
_Example_:
|
||||||
|
|
@ -211,6 +216,61 @@ var userFull = await client.GetUserFull(userId);
|
||||||
|
|
||||||
**Returns**: **UserFull**, User's information
|
**Returns**: **UserFull**, User's information
|
||||||
|
|
||||||
|
####Create Chat
|
||||||
|
Creates a new chat.
|
||||||
|
|
||||||
|
_Example_:
|
||||||
|
|
||||||
|
```
|
||||||
|
var statedMessage = await client.CreateChat(title, new List<string> { userId1, userId2 });
|
||||||
|
```
|
||||||
|
|
||||||
|
* title - **string**, chat name
|
||||||
|
* 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.
|
||||||
|
|
||||||
|
####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.
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
Contributing is highly appreciated!
|
Contributing is highly appreciated!
|
||||||
|
|
|
||||||
|
|
@ -351,7 +351,7 @@ namespace TLSharp.Core.MTProto
|
||||||
{0x83e5de54, typeof (MessageEmptyConstructor)},
|
{0x83e5de54, typeof (MessageEmptyConstructor)},
|
||||||
{0x567699B3, typeof (MessageConstructor)},
|
{0x567699B3, typeof (MessageConstructor)},
|
||||||
{0xa367e716, typeof (MessageForwardedConstructor)},
|
{0xa367e716, typeof (MessageForwardedConstructor)},
|
||||||
{0x9f8d60bb, typeof (MessageServiceConstructor)},
|
{0x1d86f70e, typeof (MessageServiceConstructor)},
|
||||||
{0x3ded6320, typeof (MessageMediaEmptyConstructor)},
|
{0x3ded6320, typeof (MessageMediaEmptyConstructor)},
|
||||||
{0xc8c45a2a, typeof (MessageMediaPhotoConstructor)},
|
{0xc8c45a2a, typeof (MessageMediaPhotoConstructor)},
|
||||||
{0xa2d24290, typeof (MessageMediaVideoConstructor)},
|
{0xa2d24290, typeof (MessageMediaVideoConstructor)},
|
||||||
|
|
@ -967,10 +967,9 @@ namespace TLSharp.Core.MTProto
|
||||||
media);
|
media);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Message messageService(int id, int from_id, Peer to_id, bool output, bool unread, int date,
|
public static Message messageService(int flags, int id, int from_id, Peer to_id, int date, MessageAction action)
|
||||||
MessageAction action)
|
|
||||||
{
|
{
|
||||||
return new MessageServiceConstructor(id, from_id, to_id, output, unread, date, action);
|
return new MessageServiceConstructor(flags, id, from_id, to_id, date, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MessageMedia messageMediaEmpty()
|
public static MessageMedia messageMediaEmpty()
|
||||||
|
|
@ -5394,34 +5393,27 @@ namespace TLSharp.Core.MTProto
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class MessageServiceConstructor : Message
|
public class MessageServiceConstructor : Message // messageService#1d86f70e flags:int id:int from_id:int to_id:Peer date:int action:MessageAction = Message;
|
||||||
{
|
{
|
||||||
|
public int flags;
|
||||||
public int id;
|
public int id;
|
||||||
public int from_id;
|
public int from_id;
|
||||||
public Peer to_id;
|
public Peer to_id;
|
||||||
public bool output;
|
|
||||||
public bool unread;
|
|
||||||
public int date;
|
public int date;
|
||||||
public MessageAction action;
|
public MessageAction action;
|
||||||
|
|
||||||
public MessageServiceConstructor()
|
public MessageServiceConstructor() { }
|
||||||
{
|
|
||||||
|
|
||||||
}
|
public MessageServiceConstructor(int flags, int id, int from_id, Peer to_id, int date, MessageAction action)
|
||||||
|
|
||||||
public MessageServiceConstructor(int id, int from_id, Peer to_id, bool output, bool unread, int date,
|
|
||||||
MessageAction action)
|
|
||||||
{
|
{
|
||||||
|
this.flags = flags;
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.from_id = from_id;
|
this.from_id = from_id;
|
||||||
this.to_id = to_id;
|
this.to_id = to_id;
|
||||||
this.output = output;
|
|
||||||
this.unread = unread;
|
|
||||||
this.date = date;
|
this.date = date;
|
||||||
this.action = action;
|
this.action = action;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public override Constructor Constructor
|
public override Constructor Constructor
|
||||||
{
|
{
|
||||||
get { return Constructor.messageService; }
|
get { return Constructor.messageService; }
|
||||||
|
|
@ -5429,31 +5421,29 @@ namespace TLSharp.Core.MTProto
|
||||||
|
|
||||||
public override void Write(BinaryWriter writer)
|
public override void Write(BinaryWriter writer)
|
||||||
{
|
{
|
||||||
writer.Write(0x9f8d60bb);
|
writer.Write(0x1d86f70e);
|
||||||
|
writer.Write(this.flags);
|
||||||
writer.Write(this.id);
|
writer.Write(this.id);
|
||||||
writer.Write(this.from_id);
|
writer.Write(this.from_id);
|
||||||
this.to_id.Write(writer);
|
this.to_id.Write(writer);
|
||||||
writer.Write(this.output ? 0x997275b5 : 0xbc799737);
|
|
||||||
writer.Write(this.unread ? 0x997275b5 : 0xbc799737);
|
|
||||||
writer.Write(this.date);
|
writer.Write(this.date);
|
||||||
this.action.Write(writer);
|
this.action.Write(writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Read(BinaryReader reader)
|
public override void Read(BinaryReader reader)
|
||||||
{
|
{
|
||||||
|
this.flags = reader.ReadInt32();
|
||||||
this.id = reader.ReadInt32();
|
this.id = reader.ReadInt32();
|
||||||
this.from_id = reader.ReadInt32();
|
this.from_id = reader.ReadInt32();
|
||||||
this.to_id = TL.Parse<Peer>(reader);
|
this.to_id = TL.Parse<Peer>(reader);
|
||||||
this.output = reader.ReadUInt32() == 0x997275b5;
|
|
||||||
this.unread = reader.ReadUInt32() == 0x997275b5;
|
|
||||||
this.date = reader.ReadInt32();
|
this.date = reader.ReadInt32();
|
||||||
this.action = TL.Parse<MessageAction>(reader);
|
this.action = TL.Parse<MessageAction>(reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return String.Format("(messageService id:{0} from_id:{1} to_id:{2} out:{3} unread:{4} date:{5} action:{6})", id,
|
return String.Format("(messageService flags:{0} id:{1} from_id:{2} to_id:{3} date:{4} action:{5})",
|
||||||
from_id, to_id, output, unread, date, action);
|
flags, id, from_id, to_id, date, action);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -7146,7 +7136,7 @@ namespace TLSharp.Core.MTProto
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Read(BinaryReader reader)
|
public override void Read(BinaryReader reader)
|
||||||
{
|
{
|
||||||
if (reader.ReadUInt32() == 0x7007b451)
|
if (reader.ReadUInt32() == 0x7007b451)
|
||||||
{
|
{
|
||||||
this.user = new UserSelfConstructor();
|
this.user = new UserSelfConstructor();
|
||||||
|
|
|
||||||
|
|
@ -90,8 +90,8 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="packages.config" />
|
<None Include="packages.config" />
|
||||||
<Compile Include="Requests\CreateChatRequest.cs" />
|
<Compile Include="Requests\CreateChatRequest.cs" />
|
||||||
<None Include="Requests\AddChatUserRequest.cs" />
|
<Compile Include="Requests\AddChatUserRequest.cs" />
|
||||||
<None Include="Requests\DeleteChatUserRequest.cs" />
|
<Compile Include="Requests\DeleteChatUserRequest.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||||
|
|
|
||||||
|
|
@ -310,5 +310,54 @@ namespace TLSharp.Core
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<Messages_statedMessageConstructor> CreateChat(string title, List<string> userPhonesToInvite)
|
||||||
|
{
|
||||||
|
var userIdsToInvite = new List<int>();
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await CreateChat(title, userIdsToInvite);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Messages_statedMessageConstructor> CreateChat(string title, List<int> userIdsToInvite)
|
||||||
|
{
|
||||||
|
var request = new CreateChatRequest(userIdsToInvite.Select(uid => new InputUserContactConstructor(uid)).ToList(), title);
|
||||||
|
|
||||||
|
await _sender.Send(request);
|
||||||
|
await _sender.Receive(request);
|
||||||
|
|
||||||
|
return request.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Messages_statedMessageConstructor> AddChatUser(int chatId, int userId)
|
||||||
|
{
|
||||||
|
var request = new AddChatUserRequest(chatId, new InputUserContactConstructor(userId));
|
||||||
|
|
||||||
|
await _sender.Send(request);
|
||||||
|
await _sender.Receive(request);
|
||||||
|
|
||||||
|
return request.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Messages_statedMessageConstructor> DeleteChatUser(int chatId, int userId)
|
||||||
|
{
|
||||||
|
var request = new DeleteChatUserRequest(chatId, new InputUserContactConstructor(userId));
|
||||||
|
|
||||||
|
await _sender.Send(request);
|
||||||
|
await _sender.Receive(request);
|
||||||
|
|
||||||
|
return request.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Messages_statedMessageConstructor> LeaveChat(int chatId)
|
||||||
|
{
|
||||||
|
return await DeleteChatUser(chatId, ((UserSelfConstructor) _session.User).id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
using System.Configuration;
|
using System.Collections.Generic;
|
||||||
|
using System.Configuration;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
using TLSharp.Core;
|
using TLSharp.Core;
|
||||||
|
|
@ -226,17 +228,17 @@ namespace TLSharp.Tests
|
||||||
Assert.AreEqual(1, hist.Count);
|
Assert.AreEqual(1, hist.Count);
|
||||||
|
|
||||||
var message = (MessageConstructor) hist[0];
|
var message = (MessageConstructor) hist[0];
|
||||||
Assert.AreEqual(typeof(MessageMediaPhotoConstructor), message.media.GetType());
|
Assert.AreEqual(typeof (MessageMediaPhotoConstructor), message.media.GetType());
|
||||||
|
|
||||||
var media = (MessageMediaPhotoConstructor) message.media;
|
var media = (MessageMediaPhotoConstructor) message.media;
|
||||||
Assert.AreEqual(typeof(PhotoConstructor), media.photo.GetType());
|
Assert.AreEqual(typeof (PhotoConstructor), media.photo.GetType());
|
||||||
|
|
||||||
var photo = (PhotoConstructor) media.photo;
|
var photo = (PhotoConstructor) media.photo;
|
||||||
Assert.AreEqual(3, photo.sizes.Count);
|
Assert.AreEqual(3, photo.sizes.Count);
|
||||||
Assert.AreEqual(typeof(PhotoSizeConstructor), photo.sizes[2].GetType());
|
Assert.AreEqual(typeof (PhotoSizeConstructor), photo.sizes[2].GetType());
|
||||||
|
|
||||||
var photoSize = (PhotoSizeConstructor) photo.sizes[2];
|
var photoSize = (PhotoSizeConstructor) photo.sizes[2];
|
||||||
Assert.AreEqual(typeof(FileLocationConstructor), photoSize.location.GetType());
|
Assert.AreEqual(typeof (FileLocationConstructor), photoSize.location.GetType());
|
||||||
|
|
||||||
var fileLocation = (FileLocationConstructor) photoSize.location;
|
var fileLocation = (FileLocationConstructor) photoSize.location;
|
||||||
var file = await client.GetFile(fileLocation.volume_id, fileLocation.local_id, fileLocation.secret, 0, photoSize.size + 1024);
|
var file = await client.GetFile(fileLocation.volume_id, fileLocation.local_id, fileLocation.secret, 0, photoSize.size + 1024);
|
||||||
|
|
@ -244,11 +246,11 @@ namespace TLSharp.Tests
|
||||||
byte[] bytes = file.Item2;
|
byte[] bytes = file.Item2;
|
||||||
|
|
||||||
string name = "../../data/get_file.";
|
string name = "../../data/get_file.";
|
||||||
if (type.GetType() == typeof(Storage_fileJpegConstructor))
|
if (type.GetType() == typeof (Storage_fileJpegConstructor))
|
||||||
name += "jpg";
|
name += "jpg";
|
||||||
else if (type.GetType() == typeof(Storage_fileGifConstructor))
|
else if (type.GetType() == typeof (Storage_fileGifConstructor))
|
||||||
name += "gif";
|
name += "gif";
|
||||||
else if (type.GetType() == typeof(Storage_filePngConstructor))
|
else if (type.GetType() == typeof (Storage_filePngConstructor))
|
||||||
name += "png";
|
name += "png";
|
||||||
|
|
||||||
using (var fileStream = new FileStream(name, FileMode.Create, FileAccess.Write))
|
using (var fileStream = new FileStream(name, FileMode.Create, FileAccess.Write))
|
||||||
|
|
@ -294,5 +296,85 @@ namespace TLSharp.Tests
|
||||||
|
|
||||||
Assert.IsNotNull(userFull);
|
Assert.IsNotNull(userFull);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CreateChatRequest()
|
||||||
|
{
|
||||||
|
var client = await InitializeClient();
|
||||||
|
|
||||||
|
var chatName = Guid.NewGuid().ToString();
|
||||||
|
var statedMessage = await client.CreateChat(chatName, new List<string> {NumberToSendMessage});
|
||||||
|
|
||||||
|
var createdChat = GetChatFromStatedMessage(statedMessage);
|
||||||
|
|
||||||
|
Assert.AreEqual(chatName, createdChat.title);
|
||||||
|
Assert.AreEqual(2, createdChat.participants_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task AddChatUserRequest()
|
||||||
|
{
|
||||||
|
var client = await InitializeClient();
|
||||||
|
|
||||||
|
var chatName = Guid.NewGuid().ToString();
|
||||||
|
var statedMessageAfterCreation = await client.CreateChat(chatName, new List<string> { NumberToSendMessage });
|
||||||
|
|
||||||
|
var createdChat = GetChatFromStatedMessage(statedMessageAfterCreation);
|
||||||
|
|
||||||
|
var addUserId = await client.ImportContactByPhoneNumber("380685004559");
|
||||||
|
|
||||||
|
var statedMessageAfterAddUser = await client.AddChatUser(createdChat.id, addUserId.Value);
|
||||||
|
var modifiedChat = GetChatFromStatedMessage(statedMessageAfterAddUser);
|
||||||
|
|
||||||
|
Assert.AreEqual(createdChat.id, modifiedChat.id);
|
||||||
|
Assert.AreEqual(3, modifiedChat.participants_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task LeaveChatRequest()
|
||||||
|
{
|
||||||
|
var client = await InitializeClient();
|
||||||
|
|
||||||
|
var chatName = Guid.NewGuid().ToString();
|
||||||
|
var statedMessageAfterCreation = await client.CreateChat(chatName, new List<string> { NumberToSendMessage });
|
||||||
|
|
||||||
|
var createdChat = GetChatFromStatedMessage(statedMessageAfterCreation);
|
||||||
|
|
||||||
|
var statedMessageAfterLeave = await client.LeaveChat(createdChat.id);
|
||||||
|
var modifiedChat = GetChatFromStatedMessage(statedMessageAfterLeave);
|
||||||
|
|
||||||
|
Assert.AreEqual(createdChat.id, modifiedChat.id);
|
||||||
|
Assert.AreEqual(1, modifiedChat.participants_count);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ChatConstructor GetChatFromStatedMessage(Messages_statedMessageConstructor message)
|
||||||
|
{
|
||||||
|
var serviceMessage = message.message as MessageServiceConstructor;
|
||||||
|
var peerChat = serviceMessage.to_id as PeerChatConstructor;
|
||||||
|
|
||||||
|
var createdChatId = peerChat.chat_id;
|
||||||
|
return message.chats.OfType<ChatConstructor>().Single(c => c.id == createdChatId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<TelegramClient> InitializeClient()
|
||||||
|
{
|
||||||
|
var store = new FileSessionStore();
|
||||||
|
var client = new TelegramClient(store, "session", apiId, apiHash);
|
||||||
|
await client.Connect();
|
||||||
|
|
||||||
|
if (!client.IsUserAuthorized())
|
||||||
|
{
|
||||||
|
var hash = await client.SendCodeRequest(NumberToAuthenticate);
|
||||||
|
|
||||||
|
var code = ""; // you can change code in debugger
|
||||||
|
Debugger.Break();
|
||||||
|
|
||||||
|
await client.MakeAuth(NumberToAuthenticate, hash, code);
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.IsTrue(client.IsUserAuthorized());
|
||||||
|
|
||||||
|
return client;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue