mirror of
https://github.com/sochix/TLSharp.git
synced 2025-12-06 08:02:00 +01:00
UploadFile and SendMediaMessage
This commit is contained in:
parent
5427983429
commit
7e18b68d72
53
TLSharp.Core/Requests/Message_SendMediaRequest.cs
Normal file
53
TLSharp.Core/Requests/Message_SendMediaRequest.cs
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TLSharp.Core.MTProto;
|
||||||
|
using TLSharp.Core.Utils;
|
||||||
|
|
||||||
|
namespace TLSharp.Core.Requests
|
||||||
|
{
|
||||||
|
//messages.sendMedia#a3c85d76 peer:InputPeer media:InputMedia random_id:long = messages.StatedMessage;
|
||||||
|
public class Message_SendMediaRequest : MTProtoRequest
|
||||||
|
{
|
||||||
|
InputPeer inputPeer;
|
||||||
|
InputMedia inputMedia;
|
||||||
|
|
||||||
|
public messages_StatedMessage StatedMessage { get; set; }
|
||||||
|
|
||||||
|
public Message_SendMediaRequest(InputPeer inputPeer, InputMedia inputMedia)
|
||||||
|
{
|
||||||
|
this.inputPeer = inputPeer;
|
||||||
|
this.inputMedia = inputMedia;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnSend(BinaryWriter writer)
|
||||||
|
{
|
||||||
|
writer.Write(0xa3c85d76);
|
||||||
|
inputPeer.Write(writer);
|
||||||
|
inputMedia.Write(writer);
|
||||||
|
long random_id = Helpers.GenerateRandomLong();
|
||||||
|
writer.Write(random_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Confirmed => true;
|
||||||
|
public override bool Responded { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
53
TLSharp.Core/Requests/Upload_SaveFilePartRequest.cs
Normal file
53
TLSharp.Core/Requests/Upload_SaveFilePartRequest.cs
Normal file
|
|
@ -0,0 +1,53 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TLSharp.Core.MTProto;
|
||||||
|
|
||||||
|
namespace TLSharp.Core.Requests
|
||||||
|
{
|
||||||
|
//upload.saveFilePart#b304a621 file_id:long file_part:int bytes:bytes = Bool;
|
||||||
|
public class Upload_SaveFilePartRequest : MTProtoRequest
|
||||||
|
{
|
||||||
|
long file_id;
|
||||||
|
int file_part;
|
||||||
|
byte[] bytes;
|
||||||
|
|
||||||
|
public bool Done { get; set; }
|
||||||
|
|
||||||
|
public Upload_SaveFilePartRequest(long file_id, int file_part, byte[] bytes)
|
||||||
|
{
|
||||||
|
this.file_id = file_id;
|
||||||
|
this.file_part = file_part;
|
||||||
|
this.bytes = bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnResponse(BinaryReader reader)
|
||||||
|
{
|
||||||
|
var code = reader.ReadUInt32();
|
||||||
|
|
||||||
|
if (code != 0xbc799737 && code != 0x997275b5)
|
||||||
|
throw new InvalidOperationException($"Expected Tl Bool type");
|
||||||
|
|
||||||
|
Done = code == 0x997275b5 ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnSend(BinaryWriter writer)
|
||||||
|
{
|
||||||
|
writer.Write(0xb304a621);
|
||||||
|
writer.Write(file_id);
|
||||||
|
writer.Write(file_part);
|
||||||
|
Serializers.Bytes.write(writer, bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void OnException(Exception exception)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Confirmed => true;
|
||||||
|
public override bool Responded { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -76,6 +76,8 @@
|
||||||
<Compile Include="Requests\InitConnectionRequest.cs" />
|
<Compile Include="Requests\InitConnectionRequest.cs" />
|
||||||
<Compile Include="Requests\MTProtoRequest.cs" />
|
<Compile Include="Requests\MTProtoRequest.cs" />
|
||||||
<Compile Include="Requests\SendMessageRequest.cs" />
|
<Compile Include="Requests\SendMessageRequest.cs" />
|
||||||
|
<Compile Include="Requests\Message_SendMediaRequest.cs" />
|
||||||
|
<Compile Include="Requests\Upload_SaveFilePartRequest.cs" />
|
||||||
<Compile Include="Session.cs" />
|
<Compile Include="Session.cs" />
|
||||||
<Compile Include="TelegramClient.cs" />
|
<Compile Include="TelegramClient.cs" />
|
||||||
<Compile Include="Utils\Helpers.cs" />
|
<Compile Include="Utils\Helpers.cs" />
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using TLSharp.Core.Auth;
|
using TLSharp.Core.Auth;
|
||||||
using TLSharp.Core.MTProto;
|
using TLSharp.Core.MTProto;
|
||||||
|
|
@ -96,6 +97,63 @@ namespace TLSharp.Core
|
||||||
return request.user;
|
return request.user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public async Task<InputFile> UploadFile(string name, byte[] data)
|
||||||
|
{
|
||||||
|
var partSize = 65536;
|
||||||
|
|
||||||
|
var file_id = DateTime.Now.Ticks;
|
||||||
|
|
||||||
|
var partedData = new Dictionary<int, byte[]>();
|
||||||
|
var parts = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(data.Length) / Convert.ToDouble(partSize)));
|
||||||
|
var remainBytes = data.Length;
|
||||||
|
for (int i = 0; i < parts; i++)
|
||||||
|
{
|
||||||
|
partedData.Add(i, data
|
||||||
|
.Skip(i * partSize)
|
||||||
|
.Take(remainBytes < partSize ? remainBytes : partSize)
|
||||||
|
.ToArray());
|
||||||
|
|
||||||
|
remainBytes -= partSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i =0;i<parts;i++)
|
||||||
|
{
|
||||||
|
var saveFilePartRequest = new Upload_SaveFilePartRequest(file_id, i, partedData[i]);
|
||||||
|
await _sender.Send(saveFilePartRequest);
|
||||||
|
await _sender.Recieve(saveFilePartRequest);
|
||||||
|
|
||||||
|
if (saveFilePartRequest.Done == false)
|
||||||
|
throw new InvalidOperationException($"File part {i} does not uploaded");
|
||||||
|
}
|
||||||
|
|
||||||
|
string md5_checksum;
|
||||||
|
using (var md5 = System.Security.Cryptography.MD5.Create())
|
||||||
|
{
|
||||||
|
var hash = md5.ComputeHash(data);
|
||||||
|
var hashResult = new StringBuilder(hash.Length * 2);
|
||||||
|
|
||||||
|
for (int i = 0; i < hash.Length; i++)
|
||||||
|
hashResult.Append(hash[i].ToString("x2"));
|
||||||
|
|
||||||
|
md5_checksum = hashResult.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
var inputFile = new InputFileConstructor(file_id, parts, name, md5_checksum);
|
||||||
|
|
||||||
|
return inputFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<messages_StatedMessage> SendMediaMessage(InputPeer inputPeer, InputMedia inputMedia)
|
||||||
|
{
|
||||||
|
var request = new Message_SendMediaRequest(inputPeer, inputMedia);
|
||||||
|
await _sender.Send(request);
|
||||||
|
await _sender.Recieve(request);
|
||||||
|
|
||||||
|
return request.StatedMessage;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<int?> ImportContact(string phoneNumber)
|
public async Task<int?> ImportContact(string phoneNumber)
|
||||||
{
|
{
|
||||||
var request = new ImportContactRequest(new InputPhoneContactConstructor(0, phoneNumber, "My Test Name", string.Empty));
|
var request = new ImportContactRequest(new InputPhoneContactConstructor(0, phoneNumber, "My Test Name", string.Empty));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue