mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
Added tests and fixes to SendMediaFunctionality and GetHistory
This commit is contained in:
parent
c77cfc064d
commit
9cc6942088
|
|
@ -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<Peer>(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<Peer>(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<Chat>(reader);
|
||||
this.chats.Add(chats_element);
|
||||
}
|
||||
/*
|
||||
reader.ReadInt32(); // vector code
|
||||
int users_len = reader.ReadInt32();
|
||||
this.users = new List<User>(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<PhotoSize>(reader);
|
||||
this.dc_id = reader.ReadInt32();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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<Message> messages;
|
||||
public List<Chat> chats;
|
||||
public List<User> users;
|
||||
public List<Message> messages;
|
||||
public List<Chat> chats;
|
||||
public List<User> users;
|
||||
|
||||
public GetHistoryRequest(InputPeer peer, int offset, int max_id, int limit)
|
||||
{
|
||||
_peer = peer;
|
||||
_offset = offset;
|
||||
_max_id = max_id;
|
||||
_limit = limit;
|
||||
}
|
||||
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 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 override void OnResponse(BinaryReader reader)
|
||||
{
|
||||
bool messagesSlice = reader.ReadUInt32() == 0xb446ae3; // else messages#8c718e87
|
||||
|
||||
if (messagesSlice) reader.ReadInt32(); // count
|
||||
if (messagesSlice) reader.ReadInt32(); // count
|
||||
|
||||
// messages
|
||||
var result = reader.ReadUInt32(); // vector#1cb5c415
|
||||
int messages_len = reader.ReadInt32();
|
||||
messages = new List<Message>(messages_len);
|
||||
for (int i = 0; i < messages_len; i++)
|
||||
messages.Add(TL.Parse<Message>(reader));
|
||||
// messages
|
||||
var result = reader.ReadUInt32(); // vector#1cb5c415
|
||||
int messages_len = reader.ReadInt32();
|
||||
messages = new List<Message>(messages_len);
|
||||
for (var i = 0; i < messages_len; i++)
|
||||
{
|
||||
var msgEl = TL.Parse<Message>(reader);
|
||||
|
||||
// chats
|
||||
reader.ReadUInt32();
|
||||
int chats_len = reader.ReadInt32();
|
||||
chats = new List<Chat>(chats_len);
|
||||
for (int i = 0; i < chats_len; i++)
|
||||
chats.Add(TL.Parse<Chat>(reader));
|
||||
messages.Add(msgEl);
|
||||
}
|
||||
|
||||
// users
|
||||
reader.ReadUInt32();
|
||||
int users_len = reader.ReadInt32();
|
||||
users = new List<User>(users_len);
|
||||
for (int i = 0; i < users_len; i++)
|
||||
users.Add(TL.Parse<User>(reader));
|
||||
}
|
||||
// chats
|
||||
reader.ReadUInt32();
|
||||
int chats_len = reader.ReadInt32();
|
||||
chats = new List<Chat>(chats_len);
|
||||
for (int i = 0; i < chats_len; i++)
|
||||
chats.Add(TL.Parse<Chat>(reader));
|
||||
|
||||
public override void OnException(Exception exception)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
/*
|
||||
// users
|
||||
reader.ReadUInt32();
|
||||
int users_len = reader.ReadInt32();
|
||||
users = new List<User>(users_len);
|
||||
for (int i = 0; i < users_len; i++)
|
||||
users.Add(TL.Parse<User>(reader));
|
||||
*/
|
||||
}
|
||||
|
||||
public override bool Confirmed => true;
|
||||
public override bool Responded { get; }
|
||||
}
|
||||
public override void OnException(Exception exception)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override bool Confirmed => true;
|
||||
public override bool Responded { get; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<messages_StatedMessage>(reader);
|
||||
}
|
||||
|
||||
public override void OnException(Exception exception)
|
||||
|
|
|
|||
|
|
@ -22,11 +22,6 @@ namespace TLSharp.Core
|
|||
private Session _session;
|
||||
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)
|
||||
{
|
||||
if (_apiId == 0)
|
||||
|
|
@ -39,7 +34,6 @@ namespace TLSharp.Core
|
|||
_transport = new TcpTransport(_session.ServerAddress, _session.Port);
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> Connect(bool reconnect = false)
|
||||
{
|
||||
if (_session.AuthKey == null || reconnect)
|
||||
|
|
@ -143,7 +137,6 @@ namespace TLSharp.Core
|
|||
return request.user;
|
||||
}
|
||||
|
||||
|
||||
public async Task<InputFile> UploadFile(string name, byte[] data)
|
||||
{
|
||||
var partSize = 65536;
|
||||
|
|
@ -190,13 +183,13 @@ namespace TLSharp.Core
|
|||
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);
|
||||
await _sender.Send(request);
|
||||
await _sender.Recieve(request);
|
||||
|
||||
return request.StatedMessage;
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<int?> 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<Tuple<List<Chat>, List<User>>> 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<Chat>, List<User>>(request.chats, request.users);
|
||||
}
|
||||
|
||||
public async Task<List<Message>> GetHistory(int user_id, int offset, int max_id, int limit)
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@
|
|||
</Otherwise>
|
||||
</Choose>
|
||||
<ItemGroup>
|
||||
<Compile Include="NotificatioClientTests.cs" />
|
||||
<Compile Include="TLSharpTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
TLSharp.Tests/data/cat.jpg
Normal file
BIN
TLSharp.Tests/data/cat.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 20 KiB |
Loading…
Reference in a new issue