【问题标题】:Telegram Bot download image fileTelegram Bot 下载图像文件
【发布时间】:2016-01-10 00:25:25
【问题描述】:

我正在尝试使用我的机器人下载文件(图像),但是当我在使用 getFile 后下载图像(已成功完成)时,我收到的图像非常小 1.7 kb 而它比我的手机大电话

【问题讨论】:

标签: c# bots telegram telegram-bot


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

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

【讨论】:

    【解决方案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)。

    【讨论】:

      【解决方案3】:

      我使用 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
      

      【讨论】:

        【解决方案4】:
        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();
        

        【讨论】:

        • 请描述一些工作流程或评论您的代码。
        【解决方案5】:

        您需要使用await botClient.DownloadFileAsync(file.FilePath, saveImageStream); 而不是await file.FileStream.CopyToAsync(saveImageStream); 您的代码应如下所示:

                    static async void DownloadFile(ITelegramBotClient botClient, string fileId, string path)
                {
                    try
                    {                   
                        var file = await botClient.GetFileAsync(fileId);
        
                        using (var saveImageStream = new FileStream(path, FileMode.Create))
                        {
                            await botClient.DownloadFileAsync(file.FilePath, saveImageStream);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error downloading: " + ex.Message);
                    }
                }
        

        来自 14.2.0 版的 Telegram.Bot 在示例中提交:https://github.com/TelegramBots/Telegram.Bot.Examples/commit/ff5a44133ad3b0d3c1e4a8b82edce959d0ee0d0e

        【讨论】:

          猜你喜欢
          • 2017-01-30
          • 2016-10-11
          • 2018-11-19
          • 1970-01-01
          • 2020-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-06
          相关资源
          最近更新 更多