Added tests and fixes to SendMediaFunctionality and GetHistory

This commit is contained in:
Ilya Pirozhneko 2016-02-03 22:36:39 +03:00
parent c77cfc064d
commit 9cc6942088
8 changed files with 157 additions and 99 deletions

View file

@ -328,7 +328,8 @@ namespace TLSharp.Core.MTProto
{0x7c596b46, typeof (FileLocationUnavailableConstructor)}, {0x7c596b46, typeof (FileLocationUnavailableConstructor)},
{0x53d69076, typeof (FileLocationConstructor)}, {0x53d69076, typeof (FileLocationConstructor)},
{0x200250ba, typeof (UserEmptyConstructor)}, {0x200250ba, typeof (UserEmptyConstructor)},
{0x720535ec, typeof (UserSelfConstructor)}, {0x720535EC, typeof (UserSelfConstructor)},
{0x7007b451, typeof (UserSelfConstructor)},
{0xf2fb8319, typeof (UserContactConstructor)}, {0xf2fb8319, typeof (UserContactConstructor)},
{0x22e8ceb0, typeof (UserRequestConstructor)}, {0x22e8ceb0, typeof (UserRequestConstructor)},
{0x5214c89d, typeof (UserForeignConstructor)}, {0x5214c89d, typeof (UserForeignConstructor)},
@ -348,8 +349,8 @@ namespace TLSharp.Core.MTProto
{0x37c1011c, typeof (ChatPhotoEmptyConstructor)}, {0x37c1011c, typeof (ChatPhotoEmptyConstructor)},
{0x6153276a, typeof (ChatPhotoConstructor)}, {0x6153276a, typeof (ChatPhotoConstructor)},
{0x83e5de54, typeof (MessageEmptyConstructor)}, {0x83e5de54, typeof (MessageEmptyConstructor)},
{0x22eb6aba, typeof (MessageConstructor)}, {0x567699B3, typeof (MessageConstructor)},
{0x05f46804, typeof (MessageForwardedConstructor)}, {0xa367e716, typeof (MessageForwardedConstructor)},
{0x9f8d60bb, typeof (MessageServiceConstructor)}, {0x9f8d60bb, typeof (MessageServiceConstructor)},
{0x3ded6320, typeof (MessageMediaEmptyConstructor)}, {0x3ded6320, typeof (MessageMediaEmptyConstructor)},
{0xc8c45a2a, typeof (MessageMediaPhotoConstructor)}, {0xc8c45a2a, typeof (MessageMediaPhotoConstructor)},
@ -522,7 +523,7 @@ namespace TLSharp.Core.MTProto
{0x34e794bd, typeof (InputMediaUploadedDocumentConstructor)}, {0x34e794bd, typeof (InputMediaUploadedDocumentConstructor)},
{0x3e46de5d, typeof (InputMediaUploadedThumbDocumentConstructor)}, {0x3e46de5d, typeof (InputMediaUploadedThumbDocumentConstructor)},
{0xd184e841, typeof (InputMediaDocumentConstructor)}, {0xd184e841, typeof (InputMediaDocumentConstructor)},
{0x2fda2204, typeof (MessageMediaDocumentConstructor)}, {0x2fda2204 , typeof (MessageMediaDocumentConstructor)},
{0xc6b68300, typeof (MessageMediaAudioConstructor)}, {0xc6b68300, typeof (MessageMediaAudioConstructor)},
{0xd95adc84, typeof (InputAudioEmptyConstructor)}, {0xd95adc84, typeof (InputAudioEmptyConstructor)},
{0x77d440ff, typeof (InputAudioConstructor)}, {0x77d440ff, typeof (InputAudioConstructor)},
@ -535,7 +536,7 @@ namespace TLSharp.Core.MTProto
{0x586988d8, typeof (AudioEmptyConstructor)}, {0x586988d8, typeof (AudioEmptyConstructor)},
{0x427425e7, typeof (AudioConstructor)}, {0x427425e7, typeof (AudioConstructor)},
{0x36f8c871, typeof (DocumentEmptyConstructor)}, {0x36f8c871, typeof (DocumentEmptyConstructor)},
{0x9efc6326, typeof (DocumentConstructor)}, {0xf9a39f4f, typeof (DocumentConstructor)},
}; };
public static TLObject Parse(BinaryReader reader, uint code) public static TLObject Parse(BinaryReader reader, uint code)
@ -564,7 +565,7 @@ namespace TLSharp.Core.MTProto
if (!constructors.ContainsKey(dataCode)) 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]; Type constructorType = constructors[dataCode];
@ -950,13 +951,13 @@ namespace TLSharp.Core.MTProto
return new MessageEmptyConstructor(id); 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) MessageMedia media)
{ {
return new MessageConstructor(id, from_id, to_id, output, unread, date, message, 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) 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, 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 id;
public int from_id; public int from_id;
public Peer to_id; public int to_id;
public bool output; public bool output;
public bool unread; public bool unread;
public int date; 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) MessageMedia media)
{ {
this.id = id; this.id = id;
@ -5266,7 +5267,7 @@ namespace TLSharp.Core.MTProto
writer.Write(0x22eb6aba); writer.Write(0x22eb6aba);
writer.Write(this.id); writer.Write(this.id);
writer.Write(this.from_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.output ? 0x997275b5 : 0xbc799737);
writer.Write(this.unread ? 0x997275b5 : 0xbc799737); writer.Write(this.unread ? 0x997275b5 : 0xbc799737);
writer.Write(this.date); writer.Write(this.date);
@ -5278,7 +5279,7 @@ namespace TLSharp.Core.MTProto
{ {
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 = reader.ReadInt32();
this.output = reader.ReadUInt32() == 0x997275b5; this.output = reader.ReadUInt32() == 0x997275b5;
this.unread = reader.ReadUInt32() == 0x997275b5; this.unread = reader.ReadUInt32() == 0x997275b5;
this.date = reader.ReadInt32(); this.date = reader.ReadInt32();
@ -5300,7 +5301,7 @@ namespace TLSharp.Core.MTProto
public int fwd_from_id; public int fwd_from_id;
public int fwd_date; public int fwd_date;
public int from_id; public int from_id;
public Peer to_id; public int to_id;
public bool output; public bool output;
public bool unread; public bool unread;
public int date; 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) bool unread, int date, string message, MessageMedia media)
{ {
this.id = id; this.id = id;
@ -5340,7 +5341,7 @@ namespace TLSharp.Core.MTProto
writer.Write(this.fwd_from_id); writer.Write(this.fwd_from_id);
writer.Write(this.fwd_date); writer.Write(this.fwd_date);
writer.Write(this.from_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.output ? 0x997275b5 : 0xbc799737);
writer.Write(this.unread ? 0x997275b5 : 0xbc799737); writer.Write(this.unread ? 0x997275b5 : 0xbc799737);
writer.Write(this.date); writer.Write(this.date);
@ -5354,7 +5355,7 @@ namespace TLSharp.Core.MTProto
this.fwd_from_id = reader.ReadInt32(); this.fwd_from_id = reader.ReadInt32();
this.fwd_date = reader.ReadInt32(); this.fwd_date = reader.ReadInt32();
this.from_id = reader.ReadInt32(); this.from_id = reader.ReadInt32();
this.to_id = TL.Parse<Peer>(reader); this.to_id = reader.ReadInt32();
this.output = reader.ReadUInt32() == 0x997275b5; this.output = reader.ReadUInt32() == 0x997275b5;
this.unread = reader.ReadUInt32() == 0x997275b5; this.unread = reader.ReadUInt32() == 0x997275b5;
this.date = reader.ReadInt32(); this.date = reader.ReadInt32();
@ -8758,6 +8759,7 @@ namespace TLSharp.Core.MTProto
chats_element = TL.Parse<Chat>(reader); chats_element = TL.Parse<Chat>(reader);
this.chats.Add(chats_element); this.chats.Add(chats_element);
} }
/*
reader.ReadInt32(); // vector code reader.ReadInt32(); // vector code
int users_len = reader.ReadInt32(); int users_len = reader.ReadInt32();
this.users = new List<User>(users_len); this.users = new List<User>(users_len);
@ -8769,6 +8771,7 @@ namespace TLSharp.Core.MTProto
} }
this.pts = reader.ReadInt32(); this.pts = reader.ReadInt32();
this.seq = reader.ReadInt32(); this.seq = reader.ReadInt32();
*/
} }
public override string ToString() public override string ToString()
@ -14808,6 +14811,7 @@ namespace TLSharp.Core.MTProto
this.file_name = Serializers.String.read(reader); this.file_name = Serializers.String.read(reader);
this.mime_type = Serializers.String.read(reader); this.mime_type = Serializers.String.read(reader);
this.size = reader.ReadInt32(); this.size = reader.ReadInt32();
var tst = Serializers.String.read(reader);
this.thumb = TL.Parse<PhotoSize>(reader); this.thumb = TL.Parse<PhotoSize>(reader);
this.dc_id = reader.ReadInt32(); this.dc_id = reader.ReadInt32();
} }

View file

@ -290,16 +290,23 @@ namespace TLSharp.Core.Network
} }
else if (innerCode == 0x3072cfa1) else if (innerCode == 0x3072cfa1)
{
try
{ {
// gzip_packed // gzip_packed
byte[] packedData = Serializers.Bytes.read(messageReader); byte[] packedData = Serializers.Bytes.read(messageReader);
using (MemoryStream packedStream = new MemoryStream(packedData, false)) using (var packedStream = new MemoryStream(packedData, false))
using (GZipStream zipStream = new GZipStream(packedStream, CompressionMode.Decompress)) using (var zipStream = new GZipStream(packedStream, CompressionMode.Decompress))
using (BinaryReader compressedReader = new BinaryReader(zipStream)) using (var compressedReader = new BinaryReader(zipStream))
{ {
request.OnResponse(compressedReader); request.OnResponse(compressedReader);
} }
} }
catch (ZlibException ex)
{
}
}
else else
{ {
messageReader.BaseStream.Position -= 4; messageReader.BaseStream.Position -= 4;

View file

@ -43,8 +43,12 @@ namespace TLSharp.Core.Requests
var result = reader.ReadUInt32(); // vector#1cb5c415 var result = reader.ReadUInt32(); // vector#1cb5c415
int messages_len = reader.ReadInt32(); int messages_len = reader.ReadInt32();
messages = new List<Message>(messages_len); messages = new List<Message>(messages_len);
for (int i = 0; i < messages_len; i++) for (var i = 0; i < messages_len; i++)
messages.Add(TL.Parse<Message>(reader)); {
var msgEl = TL.Parse<Message>(reader);
messages.Add(msgEl);
}
// chats // chats
reader.ReadUInt32(); reader.ReadUInt32();
@ -53,12 +57,14 @@ namespace TLSharp.Core.Requests
for (int i = 0; i < chats_len; i++) for (int i = 0; i < chats_len; i++)
chats.Add(TL.Parse<Chat>(reader)); chats.Add(TL.Parse<Chat>(reader));
/*
// users // users
reader.ReadUInt32(); reader.ReadUInt32();
int users_len = reader.ReadInt32(); int users_len = reader.ReadInt32();
users = new List<User>(users_len); users = new List<User>(users_len);
for (int i = 0; i < users_len; i++) for (int i = 0; i < users_len; i++)
users.Add(TL.Parse<User>(reader)); users.Add(TL.Parse<User>(reader));
*/
} }
public override void OnException(Exception exception) public override void OnException(Exception exception)

View file

@ -34,12 +34,7 @@ namespace TLSharp.Core.Requests
public override void OnResponse(BinaryReader reader) 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<messages_StatedMessage>(reader);
} }
public override void OnException(Exception exception) public override void OnException(Exception exception)

View file

@ -22,11 +22,6 @@ namespace TLSharp.Core
private Session _session; private Session _session;
private List<DcOption> dcOptions; private List<DcOption> dcOptions;
public User loggedUser { get { return _session.User; } }
public List<Chat> chats;
public List<User> users;
public TelegramClient(ISessionStore store, string sessionUserId) public TelegramClient(ISessionStore store, string sessionUserId)
{ {
if (_apiId == 0) if (_apiId == 0)
@ -39,7 +34,6 @@ namespace TLSharp.Core
_transport = new TcpTransport(_session.ServerAddress, _session.Port); _transport = new TcpTransport(_session.ServerAddress, _session.Port);
} }
public async Task<bool> Connect(bool reconnect = false) public async Task<bool> Connect(bool reconnect = false)
{ {
if (_session.AuthKey == null || reconnect) if (_session.AuthKey == null || reconnect)
@ -143,7 +137,6 @@ namespace TLSharp.Core
return request.user; return request.user;
} }
public async Task<InputFile> UploadFile(string name, byte[] data) public async Task<InputFile> UploadFile(string name, byte[] data)
{ {
var partSize = 65536; var partSize = 65536;
@ -190,13 +183,13 @@ namespace TLSharp.Core
return inputFile; return inputFile;
} }
public async Task<messages_StatedMessage> SendMediaMessage(InputPeer inputPeer, InputMedia inputMedia) public async Task<Boolean> SendMediaMessage(InputPeer inputPeer, InputMedia inputMedia)
{ {
var request = new Message_SendMediaRequest(inputPeer, inputMedia); var request = new Message_SendMediaRequest(inputPeer, inputMedia);
await _sender.Send(request); await _sender.Send(request);
await _sender.Recieve(request); await _sender.Recieve(request);
return request.StatedMessage; return true;
} }
public async Task<int?> ImportContact(string phoneNumber) public async Task<int?> ImportContact(string phoneNumber)
@ -227,15 +220,14 @@ namespace TLSharp.Core
await _sender.Recieve(request); await _sender.Recieve(request);
} }
public async Task LoadChatsAndUsers(int offset, int max_id, int limit) public async Task<Tuple<List<Chat>, List<User>>> LoadChatsAndUsers(int offset, int max_id, int limit)
{ {
// GetDialogs // GetDialogs
var request = new GetDialogsRequest(offset, max_id, limit); var request = new GetDialogsRequest(offset, max_id, limit);
await _sender.Send(request); await _sender.Send(request);
await _sender.Recieve(request); await _sender.Recieve(request);
chats = request.chats; return new Tuple<List<Chat>, List<User>>(request.chats, request.users);
users = request.users;
} }
public async Task<List<Message>> GetHistory(int user_id, int offset, int max_id, int limit) public async Task<List<Message>> GetHistory(int user_id, int offset, int max_id, int limit)

View file

@ -51,7 +51,7 @@
</Otherwise> </Otherwise>
</Choose> </Choose>
<ItemGroup> <ItemGroup>
<Compile Include="NotificatioClientTests.cs" /> <Compile Include="TLSharpTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -1,15 +1,17 @@
using System; using System;
using System.Configuration; using System.Configuration;
using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using TLSharp.Core; using TLSharp.Core;
using TLSharp.Core.Auth; using TLSharp.Core.Auth;
using TLSharp.Core.MTProto;
using TLSharp.Core.Network; using TLSharp.Core.Network;
namespace TLSharp.Tests namespace TLSharp.Tests
{ {
[TestClass] [TestClass]
public class NotificatioClientTests public class TLSharpTests
{ {
private string NumberToSendMessage { get; set; } private string NumberToSendMessage { get; set; }
@ -44,7 +46,7 @@ namespace TLSharp.Tests
await client.Connect(); await client.Connect();
var hash = await client.SendCodeRequest(NumberToAuthenticate); 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); var user = await client.MakeAuth(NumberToAuthenticate, hash, code);
@ -129,6 +131,51 @@ namespace TLSharp.Tests
await client.SendMessage(res.Value, "Test message from TelegramClient"); 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] [TestMethod]
public async Task TestConnection() public async Task TestConnection()
{ {
@ -148,5 +195,12 @@ namespace TLSharp.Tests
Assert.IsNotNull(authKey.AuthKey.Data); 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;
}
} }
} }

BIN
TLSharp.Tests/data/cat.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB