diff --git a/TLSharp.Core/MTProto/TL.cs b/TLSharp.Core/MTProto/TL.cs index 9a40399..31c40b1 100644 --- a/TLSharp.Core/MTProto/TL.cs +++ b/TLSharp.Core/MTProto/TL.cs @@ -328,7 +328,8 @@ namespace TLSharp.Core.MTProto {0x7c596b46, typeof (FileLocationUnavailableConstructor)}, {0x53d69076, typeof (FileLocationConstructor)}, {0x200250ba, typeof (UserEmptyConstructor)}, - {0x720535ec, typeof (UserSelfConstructor)}, + {0x720535EC, typeof (UserSelfConstructor)}, + {0x7007b451, typeof (UserSelfConstructor)}, {0xf2fb8319, typeof (UserContactConstructor)}, {0x22e8ceb0, typeof (UserRequestConstructor)}, {0x5214c89d, typeof (UserForeignConstructor)}, @@ -348,8 +349,8 @@ namespace TLSharp.Core.MTProto {0x37c1011c, typeof (ChatPhotoEmptyConstructor)}, {0x6153276a, typeof (ChatPhotoConstructor)}, {0x83e5de54, typeof (MessageEmptyConstructor)}, - {0x22eb6aba, typeof (MessageConstructor)}, - {0x05f46804, typeof (MessageForwardedConstructor)}, + {0x567699B3, typeof (MessageConstructor)}, + {0xa367e716, typeof (MessageForwardedConstructor)}, {0x9f8d60bb, typeof (MessageServiceConstructor)}, {0x3ded6320, typeof (MessageMediaEmptyConstructor)}, {0xc8c45a2a, typeof (MessageMediaPhotoConstructor)}, @@ -522,7 +523,7 @@ namespace TLSharp.Core.MTProto {0x34e794bd, typeof (InputMediaUploadedDocumentConstructor)}, {0x3e46de5d, typeof (InputMediaUploadedThumbDocumentConstructor)}, {0xd184e841, typeof (InputMediaDocumentConstructor)}, - {0x2fda2204, typeof (MessageMediaDocumentConstructor)}, + {0x2fda2204 , typeof (MessageMediaDocumentConstructor)}, {0xc6b68300, typeof (MessageMediaAudioConstructor)}, {0xd95adc84, typeof (InputAudioEmptyConstructor)}, {0x77d440ff, typeof (InputAudioConstructor)}, @@ -535,7 +536,7 @@ namespace TLSharp.Core.MTProto {0x586988d8, typeof (AudioEmptyConstructor)}, {0x427425e7, typeof (AudioConstructor)}, {0x36f8c871, typeof (DocumentEmptyConstructor)}, - {0x9efc6326, typeof (DocumentConstructor)}, + {0xf9a39f4f, typeof (DocumentConstructor)}, }; public static TLObject Parse(BinaryReader reader, uint code) @@ -564,7 +565,7 @@ namespace TLSharp.Core.MTProto if (!constructors.ContainsKey(dataCode)) { - throw new Exception(String.Format("invalid constructor code {0}", dataCode)); + throw new Exception(String.Format("invalid constructor code {0}", dataCode.ToString("X"))); } Type constructorType = constructors[dataCode]; @@ -950,13 +951,13 @@ namespace TLSharp.Core.MTProto return new MessageEmptyConstructor(id); } - public static Message message(int id, int from_id, Peer to_id, bool output, bool unread, int date, string message, + public static Message message(int id, int from_id, int to_id, bool output, bool unread, int date, string message, MessageMedia media) { return new MessageConstructor(id, from_id, to_id, output, unread, date, message, media); } - public static Message messageForwarded(int id, int fwd_from_id, int fwd_date, int from_id, Peer to_id, bool output, + public static Message messageForwarded(int id, int fwd_from_id, int fwd_date, int from_id, int to_id, bool output, bool unread, int date, string message, MessageMedia media) { return new MessageForwardedConstructor(id, fwd_from_id, fwd_date, from_id, to_id, output, unread, date, message, @@ -5230,7 +5231,7 @@ namespace TLSharp.Core.MTProto { public int id; public int from_id; - public Peer to_id; + public int to_id; public bool output; public bool unread; public int date; @@ -5242,7 +5243,7 @@ namespace TLSharp.Core.MTProto } - public MessageConstructor(int id, int from_id, Peer to_id, bool output, bool unread, int date, string message, + public MessageConstructor(int id, int from_id, int to_id, bool output, bool unread, int date, string message, MessageMedia media) { this.id = id; @@ -5266,7 +5267,7 @@ namespace TLSharp.Core.MTProto writer.Write(0x22eb6aba); writer.Write(this.id); writer.Write(this.from_id); - this.to_id.Write(writer); + writer.Write(this.to_id); writer.Write(this.output ? 0x997275b5 : 0xbc799737); writer.Write(this.unread ? 0x997275b5 : 0xbc799737); writer.Write(this.date); @@ -5278,7 +5279,7 @@ namespace TLSharp.Core.MTProto { this.id = reader.ReadInt32(); this.from_id = reader.ReadInt32(); - this.to_id = TL.Parse(reader); + this.to_id = reader.ReadInt32(); this.output = reader.ReadUInt32() == 0x997275b5; this.unread = reader.ReadUInt32() == 0x997275b5; this.date = reader.ReadInt32(); @@ -5300,7 +5301,7 @@ namespace TLSharp.Core.MTProto public int fwd_from_id; public int fwd_date; public int from_id; - public Peer to_id; + public int to_id; public bool output; public bool unread; public int date; @@ -5312,7 +5313,7 @@ namespace TLSharp.Core.MTProto } - public MessageForwardedConstructor(int id, int fwd_from_id, int fwd_date, int from_id, Peer to_id, bool output, + public MessageForwardedConstructor(int id, int fwd_from_id, int fwd_date, int from_id, int to_id, bool output, bool unread, int date, string message, MessageMedia media) { this.id = id; @@ -5340,7 +5341,7 @@ namespace TLSharp.Core.MTProto writer.Write(this.fwd_from_id); writer.Write(this.fwd_date); writer.Write(this.from_id); - this.to_id.Write(writer); + writer.Write(this.to_id); writer.Write(this.output ? 0x997275b5 : 0xbc799737); writer.Write(this.unread ? 0x997275b5 : 0xbc799737); writer.Write(this.date); @@ -5354,7 +5355,7 @@ namespace TLSharp.Core.MTProto this.fwd_from_id = reader.ReadInt32(); this.fwd_date = reader.ReadInt32(); this.from_id = reader.ReadInt32(); - this.to_id = TL.Parse(reader); + this.to_id = reader.ReadInt32(); this.output = reader.ReadUInt32() == 0x997275b5; this.unread = reader.ReadUInt32() == 0x997275b5; this.date = reader.ReadInt32(); @@ -8758,6 +8759,7 @@ namespace TLSharp.Core.MTProto chats_element = TL.Parse(reader); this.chats.Add(chats_element); } + /* reader.ReadInt32(); // vector code int users_len = reader.ReadInt32(); this.users = new List(users_len); @@ -8769,6 +8771,7 @@ namespace TLSharp.Core.MTProto } this.pts = reader.ReadInt32(); this.seq = reader.ReadInt32(); + */ } public override string ToString() @@ -14808,6 +14811,7 @@ namespace TLSharp.Core.MTProto this.file_name = Serializers.String.read(reader); this.mime_type = Serializers.String.read(reader); this.size = reader.ReadInt32(); + var tst = Serializers.String.read(reader); this.thumb = TL.Parse(reader); this.dc_id = reader.ReadInt32(); } diff --git a/TLSharp.Core/Network/MtProtoSender.cs b/TLSharp.Core/Network/MtProtoSender.cs index fe2ead7..c022d0c 100644 --- a/TLSharp.Core/Network/MtProtoSender.cs +++ b/TLSharp.Core/Network/MtProtoSender.cs @@ -291,13 +291,20 @@ namespace TLSharp.Core.Network } else if (innerCode == 0x3072cfa1) { - // gzip_packed - byte[] packedData = Serializers.Bytes.read(messageReader); - using (MemoryStream packedStream = new MemoryStream(packedData, false)) - using (GZipStream zipStream = new GZipStream(packedStream, CompressionMode.Decompress)) - using (BinaryReader compressedReader = new BinaryReader(zipStream)) + try { - request.OnResponse(compressedReader); + // gzip_packed + byte[] packedData = Serializers.Bytes.read(messageReader); + using (var packedStream = new MemoryStream(packedData, false)) + using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress)) + using (var compressedReader = new BinaryReader(zipStream)) + { + request.OnResponse(compressedReader); + } + } + catch (ZlibException ex) + { + } } else diff --git a/TLSharp.Core/Requests/GetHistoryRequest.cs b/TLSharp.Core/Requests/GetHistoryRequest.cs index 98e85df..02e4798 100644 --- a/TLSharp.Core/Requests/GetHistoryRequest.cs +++ b/TLSharp.Core/Requests/GetHistoryRequest.cs @@ -5,68 +5,74 @@ using TLSharp.Core.MTProto; namespace TLSharp.Core.Requests { - class GetHistoryRequest : MTProtoRequest - { - InputPeer _peer; - int _offset; - int _max_id; - int _limit; + class GetHistoryRequest : MTProtoRequest + { + InputPeer _peer; + int _offset; + int _max_id; + int _limit; - public List messages; - public List chats; - public List users; + public List messages; + public List chats; + public List users; - public GetHistoryRequest(InputPeer peer, int offset, int max_id, int limit) - { - _peer = peer; - _offset = offset; - _max_id = max_id; - _limit = limit; - } - - public override void OnSend(BinaryWriter writer) - { - writer.Write(0x92a1df2f); - _peer.Write(writer); - writer.Write(_offset); - writer.Write(_max_id); - writer.Write(_limit); - } - - public override void OnResponse(BinaryReader reader) - { - bool messagesSlice = reader.ReadUInt32() == 0xb446ae3; // else messages#8c718e87 + public GetHistoryRequest(InputPeer peer, int offset, int max_id, int limit) + { + _peer = peer; + _offset = offset; + _max_id = max_id; + _limit = limit; + } - if (messagesSlice) reader.ReadInt32(); // count + public override void OnSend(BinaryWriter writer) + { + writer.Write(0x92a1df2f); + _peer.Write(writer); + writer.Write(_offset); + writer.Write(_max_id); + writer.Write(_limit); + } - // messages - var result = reader.ReadUInt32(); // vector#1cb5c415 - int messages_len = reader.ReadInt32(); - messages = new List(messages_len); - for (int i = 0; i < messages_len; i++) - messages.Add(TL.Parse(reader)); + public override void OnResponse(BinaryReader reader) + { + bool messagesSlice = reader.ReadUInt32() == 0xb446ae3; // else messages#8c718e87 - // chats - reader.ReadUInt32(); - int chats_len = reader.ReadInt32(); - chats = new List(chats_len); - for (int i = 0; i < chats_len; i++) - chats.Add(TL.Parse(reader)); + if (messagesSlice) reader.ReadInt32(); // count - // users - reader.ReadUInt32(); - int users_len = reader.ReadInt32(); - users = new List(users_len); - for (int i = 0; i < users_len; i++) - users.Add(TL.Parse(reader)); - } + // messages + var result = reader.ReadUInt32(); // vector#1cb5c415 + int messages_len = reader.ReadInt32(); + messages = new List(messages_len); + for (var i = 0; i < messages_len; i++) + { + var msgEl = TL.Parse(reader); - public override void OnException(Exception exception) - { - throw new NotImplementedException(); - } + messages.Add(msgEl); + } - public override bool Confirmed => true; - public override bool Responded { get; } - } + // chats + reader.ReadUInt32(); + int chats_len = reader.ReadInt32(); + chats = new List(chats_len); + for (int i = 0; i < chats_len; i++) + chats.Add(TL.Parse(reader)); + + /* + // users + reader.ReadUInt32(); + int users_len = reader.ReadInt32(); + users = new List(users_len); + for (int i = 0; i < users_len; i++) + users.Add(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/Message_SendMediaRequest.cs b/TLSharp.Core/Requests/Message_SendMediaRequest.cs index e3b7bad..bd2f826 100644 --- a/TLSharp.Core/Requests/Message_SendMediaRequest.cs +++ b/TLSharp.Core/Requests/Message_SendMediaRequest.cs @@ -34,12 +34,7 @@ namespace TLSharp.Core.Requests public override void OnResponse(BinaryReader reader) { - var code = reader.ReadUInt32(); - - if (code != 0xd07ae726 && code != 0xa9af2881) - throw new InvalidOperationException($"Expected Tl messages_StatedMessage type"); - - StatedMessage = TL.Parse(reader); + } public override void OnException(Exception exception) diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 3b32fb0..d2a7160 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -22,11 +22,6 @@ namespace TLSharp.Core private Session _session; private List dcOptions; - public User loggedUser { get { return _session.User; } } - - public List chats; - public List users; - public TelegramClient(ISessionStore store, string sessionUserId) { if (_apiId == 0) @@ -38,7 +33,6 @@ namespace TLSharp.Core _session = Session.TryLoadOrCreateNew(store, sessionUserId); _transport = new TcpTransport(_session.ServerAddress, _session.Port); } - public async Task Connect(bool reconnect = false) { @@ -142,7 +136,6 @@ namespace TLSharp.Core return request.user; } - public async Task UploadFile(string name, byte[] data) { @@ -190,13 +183,13 @@ namespace TLSharp.Core return inputFile; } - public async Task SendMediaMessage(InputPeer inputPeer, InputMedia inputMedia) + public async Task SendMediaMessage(InputPeer inputPeer, InputMedia inputMedia) { var request = new Message_SendMediaRequest(inputPeer, inputMedia); await _sender.Send(request); await _sender.Recieve(request); - return request.StatedMessage; + return true; } public async Task ImportContact(string phoneNumber) @@ -227,15 +220,14 @@ namespace TLSharp.Core await _sender.Recieve(request); } - public async Task LoadChatsAndUsers(int offset, int max_id, int limit) + public async Task, List>> LoadChatsAndUsers(int offset, int max_id, int limit) { // GetDialogs var request = new GetDialogsRequest(offset, max_id, limit); await _sender.Send(request); await _sender.Recieve(request); - chats = request.chats; - users = request.users; + return new Tuple, List>(request.chats, request.users); } public async Task> GetHistory(int user_id, int offset, int max_id, int limit) diff --git a/TLSharp.Tests/TLSharp.Tests.csproj b/TLSharp.Tests/TLSharp.Tests.csproj index edaf702..a2463ad 100644 --- a/TLSharp.Tests/TLSharp.Tests.csproj +++ b/TLSharp.Tests/TLSharp.Tests.csproj @@ -51,7 +51,7 @@ - + diff --git a/TLSharp.Tests/NotificatioClientTests.cs b/TLSharp.Tests/TLSharpTests.cs similarity index 72% rename from TLSharp.Tests/NotificatioClientTests.cs rename to TLSharp.Tests/TLSharpTests.cs index 40692f2..7b100ba 100644 --- a/TLSharp.Tests/NotificatioClientTests.cs +++ b/TLSharp.Tests/TLSharpTests.cs @@ -1,15 +1,17 @@ using System; using System.Configuration; +using System.IO; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using TLSharp.Core; using TLSharp.Core.Auth; +using TLSharp.Core.MTProto; using TLSharp.Core.Network; namespace TLSharp.Tests { [TestClass] - public class NotificatioClientTests + public class TLSharpTests { private string NumberToSendMessage { get; set; } @@ -44,7 +46,7 @@ namespace TLSharp.Tests await client.Connect(); var hash = await client.SendCodeRequest(NumberToAuthenticate); - var code = "86474"; // you can change code in debugger + var code = "64443"; // you can change code in debugger var user = await client.MakeAuth(NumberToAuthenticate, hash, code); @@ -129,6 +131,51 @@ namespace TLSharp.Tests await client.SendMessage(res.Value, "Test message from TelegramClient"); } + [TestMethod] + public async Task GetHistory() + { + var store = new FileSessionStore(); + var client = new TelegramClient(store, "session"); + await client.Connect(); + + Assert.IsTrue(client.IsUserAuthorized()); + + var res = await client.ImportContact(NumberToSendMessage); + + Assert.IsNotNull(res); + + var hist = await client.GetHistory(res.Value, 0, -1, 5); + + Assert.IsNotNull(hist); + } + + [TestMethod] + public async Task UploadAndSendMedia() + { + var store = new FileSessionStore(); + var client = new TelegramClient(store, "session"); + await client.Connect(); + + Assert.IsTrue(client.IsUserAuthorized()); + + var res = await client.ImportContact(NumberToSendMessage); + + Assert.IsNotNull(res); + const string testFile = "TEST"; + + var file = File.ReadAllBytes("../../data/cat.jpg"); + + var mediaFile = await client.UploadFile("test_file.jpg", file); + + Assert.IsNotNull(mediaFile); + + var state = await client.SendMediaMessage( + new InputPeerContactConstructor(res.Value), + new InputMediaUploadedPhotoConstructor(mediaFile)); + + Assert.IsTrue(state); + } + [TestMethod] public async Task TestConnection() { @@ -148,5 +195,12 @@ namespace TLSharp.Tests Assert.IsNotNull(authKey.AuthKey.Data); } } + + static byte[] GetBytes(string str) + { + byte[] bytes = new byte[str.Length * sizeof(char)]; + System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); + return bytes; + } } } diff --git a/TLSharp.Tests/data/cat.jpg b/TLSharp.Tests/data/cat.jpg new file mode 100644 index 0000000..f64b383 Binary files /dev/null and b/TLSharp.Tests/data/cat.jpg differ