Telegram Bot下载图像文件

时间:2015-10-12 13:35:42

标签: c# bots telegram telegram-bot

我试图使用我的机器人下载文件(图像),但是当我使用getFile后下载图像(成功完成)时,我收到的图像非常小,仅为1.7 kb大于我手机上的那个

4 个答案:

答案 0 :(得分:3)

  1. getFile方法提供了一个JSON对象(1.7 KB响应),其中包含用于访问图像文件的数据。

  2. 还要注意,电报会为任何图像创建一个图像阵列。此数组的第一个元素包含原始图像的小缩略图,数组的最新元素包含原始图像。

答案 1 :(得分:2)

这是一篇旧帖子。但是由于没有关于如何在电报机器人中下载文件的良好文档,对于任何想知道的人,你应该如何做到这一点(其中一种方式):

DownloadFile(message.Photo[message.Photo.Length - 1].FileId, @"c:\photo.jpg");

其中:

    private static async void DownloadFile(string fileId, string path)
    {
        try
        {
            var file = await Bot.GetFileAsync(fileId);

            using (var saveImageStream = new FileStream(path, FileMode.Create))
            {
                await file.FileStream.CopyToAsync(saveImageStream);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error downloading: " + ex.Message);
        }
    }

message.Photo[message.Photo.Length - 1]message.Photo数组中的最后一个元素,其中包含最高质量的图像数据。显然,您也可以使用DownloadFile下载其他类型的文件(例如message.Document)。

答案 2 :(得分:1)

我使用telegram.bot v14.10.0,但是找不到file.FileStream,所以我找到了从电报中获取图像的另一种方法。我的方式是在这种情况下直接使用电报api。

                    var test =  _myBot.GetFileAsync(e.Message.Photo[e.Message.Photo.Count() - 1].FileId);
                    var download_url = @"https://api.telegram.org/file/bot<token>/" + test.Result.FilePath;
                    using (WebClient client = new WebClient())
                    {
                        client.DownloadFile(new Uri(download_url), @"c:\temp\NewCompanyPicure.png");
                    }
                    //then do what you want with it

答案 3 :(得分:0)

var file = await Bot.GetFileAsync(message.Document.FileId);
FileStream fs=new FileStream("Your Destination Path And File Name",FileMode.Create);
await Bot.DownloadFileAsync(file.FilePath, fs);
fs.Close();
fs.Dispose();