- added getFile method

This commit is contained in:
Ilya P 2016-10-23 13:29:18 +03:00
parent 1d6035aeb7
commit f6ad50ce94
2 changed files with 82 additions and 32 deletions

View file

@ -8,6 +8,7 @@ using TeleSharp.TL.Auth;
using TeleSharp.TL.Contacts; using TeleSharp.TL.Contacts;
using TeleSharp.TL.Help; using TeleSharp.TL.Help;
using TeleSharp.TL.Messages; using TeleSharp.TL.Messages;
using TeleSharp.TL.Upload;
using TLSharp.Core.Auth; using TLSharp.Core.Auth;
using TLSharp.Core.MTProto.Crypto; using TLSharp.Core.MTProto.Crypto;
using TLSharp.Core.Network; using TLSharp.Core.Network;
@ -233,6 +234,15 @@ namespace TLSharp.Core
}); });
} }
public async Task<TLFile> GetFile(TLAbsInputFileLocation location, int filePartSize)
{
return await SendRequestAsync<TLFile>(new TLRequestGetFile()
{
location = location,
limit = filePartSize
});
}
private void OnUserAuthenticated(TLUser TLUser) private void OnUserAuthenticated(TLUser TLUser)
{ {
_session.TLUser = TLUser; _session.TLUser = TLUser;

View file

@ -12,6 +12,7 @@ using TeleSharp.TL;
using TeleSharp.TL.Channels; using TeleSharp.TL.Channels;
using TeleSharp.TL.Contacts; using TeleSharp.TL.Contacts;
using TeleSharp.TL.Messages; using TeleSharp.TL.Messages;
using TeleSharp.TL.Upload;
using TLSharp.Core; using TLSharp.Core;
using TLSharp.Core.Auth; using TLSharp.Core.Auth;
using TLSharp.Core.MTProto; using TLSharp.Core.MTProto;
@ -40,7 +41,7 @@ namespace TLSharp.Tests
private int apiId = 0; private int apiId = 0;
[TestInitialize] [TestInitialize]
public void Init() public void Init()
{ {
// Setup your phone numbers in app.config // Setup your phone numbers in app.config
@ -85,40 +86,40 @@ namespace TLSharp.Tests
Assert.IsTrue(client.IsUserAuthorized()); Assert.IsTrue(client.IsUserAuthorized());
} }
[TestMethod] [TestMethod]
public async Task SendMessageTest() public async Task SendMessageTest()
{ {
var client = new TelegramClient(apiId, apiHash); var client = new TelegramClient(apiId, apiHash);
await client.ConnectAsync(); await client.ConnectAsync();
var result = await client.GetContactsAsync(); var result = await client.GetContactsAsync();
var user = result.users.lists var user = result.users.lists
.Where(x => x.GetType() == typeof (TLUser)) .Where(x => x.GetType() == typeof(TLUser))
.Cast<TLUser>() .Cast<TLUser>()
.FirstOrDefault(x => x.phone == NumberToSendMessage); .FirstOrDefault(x => x.phone == NumberToSendMessage);
await client.SendTypingAsync(new TLInputPeerUser() {user_id = user.id}); await client.SendTypingAsync(new TLInputPeerUser() { user_id = user.id });
Thread.Sleep(3000); Thread.Sleep(3000);
await client.SendMessageAsync(new TLInputPeerUser() {user_id = user.id}, "TEST"); await client.SendMessageAsync(new TLInputPeerUser() { user_id = user.id }, "TEST");
}
[TestMethod] }
public async Task SendMessageToChannelTest()
{
var client = new TelegramClient(apiId, apiHash);
await client.ConnectAsync(); [TestMethod]
public async Task SendMessageToChannelTest()
{
var client = new TelegramClient(apiId, apiHash);
var dialogs = await client.GetUserDialogsAsync(); await client.ConnectAsync();
var chat = dialogs.chats.lists
.Where(c => c.GetType() == typeof(TLChannel))
.Cast<TLChannel>()
.FirstOrDefault(c => c.title == "TestGroup");
await client.SendMessageAsync(new TLInputPeerChannel() { channel_id = chat.id, access_hash = chat.access_hash.Value }, "TEST MSG"); var dialogs = await client.GetUserDialogsAsync();
} var chat = dialogs.chats.lists
.Where(c => c.GetType() == typeof(TLChannel))
.Cast<TLChannel>()
.FirstOrDefault(c => c.title == "TestGroup");
await client.SendMessageAsync(new TLInputPeerChannel() { channel_id = chat.id, access_hash = chat.access_hash.Value }, "TEST MSG");
}
[TestMethod] [TestMethod]
public async Task SendPhotoToContactTest() public async Task SendPhotoToContactTest()
@ -135,7 +136,7 @@ namespace TLSharp.Tests
.FirstOrDefault(x => x.phone == NumberToSendMessage); .FirstOrDefault(x => x.phone == NumberToSendMessage);
var fileResult = (TLInputFile)await client.UploadFile("cat.jpg", new StreamReader("data/cat.jpg")); var fileResult = (TLInputFile)await client.UploadFile("cat.jpg", new StreamReader("data/cat.jpg"));
await client.SendUploadedPhoto(new TLInputPeerUser() {user_id = user.id}, fileResult, "kitty"); await client.SendUploadedPhoto(new TLInputPeerUser() { user_id = user.id }, fileResult, "kitty");
} }
[TestMethod] [TestMethod]
@ -152,14 +153,53 @@ namespace TLSharp.Tests
.Cast<TLUser>() .Cast<TLUser>()
.FirstOrDefault(x => x.phone == NumberToSendMessage); .FirstOrDefault(x => x.phone == NumberToSendMessage);
var fileResult = (TLInputFileBig)await client.UploadFile("some.zip", new StreamReader("C:\\PetProjects\\TelegramBotSample.zip")); var fileResult = (TLInputFileBig)await client.UploadFile("some.zip", new StreamReader("<some big file path>"));
await client.SendUploadedDocument( await client.SendUploadedDocument(
new TLInputPeerUser() {user_id = user.id}, new TLInputPeerUser() { user_id = user.id },
fileResult, fileResult,
"some zips", "some zips",
"application/zip", "application/zip",
new TLVector<TLAbsDocumentAttribute>()); new TLVector<TLAbsDocumentAttribute>());
}
[TestMethod]
public async Task DownloadFileFromContactTest()
{
var client = new TelegramClient(apiId, apiHash);
await client.ConnectAsync();
var result = await client.GetContactsAsync();
var user = result.users.lists
.Where(x => x.GetType() == typeof(TLUser))
.Cast<TLUser>()
.FirstOrDefault(x => x.phone == NumberToSendMessage);
var inputPeer = new TLInputPeerUser() { user_id = user.id };
var res = await client.SendRequestAsync<TLMessagesSlice>(new TLRequestGetHistory() { peer = inputPeer });
var document = res.messages.lists
.Where(m => m.GetType() == typeof(TLMessage))
.Cast<TLMessage>()
.Where(m => m.media != null && m.media.GetType() == typeof(TLMessageMediaDocument))
.Select(m => m.media)
.Cast<TLMessageMediaDocument>()
.Where(md => md.document.GetType() == typeof(TLDocument))
.Select(md => md.document)
.Cast<TLDocument>()
.First();
var resFile = await client.GetFile(
new TLInputDocumentFileLocation()
{
access_hash = document.access_hash,
id = document.id,
version = document.version
},
document.size);
Assert.IsTrue(resFile.bytes.Length > 0);
} }
[TestMethod] [TestMethod]