【问题标题】:Telegram bot weird error : Bad Request: wrong file identifier/HTTP URL specified电报机器人奇怪的错误:错误的请求:指定了错误的文件标识符/HTTP URL
【发布时间】:2017-07-30 13:53:15
【问题描述】:

我正在使用机器人向电报频道发送消息。

使用 webhook 方法。

我正在通过链接发送 file_id。我从频道帖子中获得了 file_id。

对于某些文件,例如 GIF 和视频格式 (MP4),

当我使用此代码时:

$url = 'https://api.telegram.org/bot'.token.'/sendVideo?chat_id='.uid."&video=".$file."&caption="
.urlencode($caption);

file_get_contents($url);

我得到这样的错误:

{"ok":false,"error_code":400,"description":"Bad Request: wrong file identifier/HTTP URL specified"}

我真的不知道我为什么会得到这个, 这就像这是随机的错误,因为我猜代码不依赖于任何东西。

我使用从频道帖子中获得的 file_id。

该错误的原因是什么? 错误请求:指定了错误的文件标识符/HTTP URL

  • 我搜索了所有相关主题,但没有找到好的信息。

【问题讨论】:

  • 你是怎么得到file_id的?通过您尝试发送视频的同一机器人?

标签: php bots telegram telegram-bot


【解决方案1】:

在我的情况下,发生此错误是因为我尝试发送图像两次(如果第一次不起作用,我会将大小减小到 5mb 以下并再次发送)。 显然 Telegram 缓存了之前的请求,所以当一个新的请求发送另一个与第一个同名的图像时,它只是返回之前的错误而不尝试发送它。

为了解决这个问题,我给调整大小的图像一个新名称(使用 random + md5 来获得一个唯一的字符串)。

【讨论】:

    【解决方案2】:

    遇到同样的错误,这是因为:

    Bots can currently send video files of up to 50 MB in size, this limit may be changed in the future.
    

    https://core.telegram.org/bots/api#sendvideo

    请尝试发布另一个文件大小

    【讨论】:

      【解决方案3】:

      如果您的 url 未从电报服务器看到或您的 url 不正确,则已看到此错误。

      您也可以使用多部分 html 发布方法向此 url 发送数据(请填写 {YourBotToken} 和 {your_channel_name_with_Atsign} 值):

      <form action="https://api.telegram.org/bot{YourBotToken}/sendVideo" method="POST" enctype="application/x-www-form-urlencoded">
      <input type="file" name="video" />
      <input type="hidden" name="chat_id" value="{your_channel_name_with_Atsign}" />
      <button type="submit" >send</button>
      </form>

      在c#中的示例代码是:

       public bool sendVideo(string filename,string sendTo)
              {
                  try
                  {
                      var botToken="{YourBotToken}";
                      var sendTo="{your_channel_name_with_Atsign}";
                      var filePath = "C:\\sample\\" + filename;
      
                      var sendTo, ="@YourChannelNameWithAtSign";
                      var bytesOfFile = System.IO.File.ReadAllBytes(filePath);
                      var url = $"https://api.telegram.org/bot{botToken}/sendVideo";
                      var res =Upload(url, sendTo, "video", bytesOfFile, filename).Result;
      
                  }
                  catch (Exception ex)
                  {
                      return false;
                  }
                  return true;
              }
      
      
      
              private static async Task<string> Upload(string actionUrl,string chat_id,string fileParamName, byte[] paramFileStream, string filename)
              {
                  var formContent = new MultipartFormDataContent
                  {
                      {new StringContent(chat_id),"chat_id"},
                      {new StreamContent(new MemoryStream(paramFileStream)),fileParamName,filename}
                  };
                  var myHttpClient = new HttpClient();
                  var response = await myHttpClient.PostAsync(actionUrl.ToString(), formContent);
                  string stringContent = await response.Content.ReadAsStringAsync();
      
                  return stringContent;
              }
      

      这种方式不需要网站,您可以在独立系统中使用它。

      【讨论】:

        【解决方案4】:

        您的 mime 类型视频不正确,请更改它

        【讨论】:

          【解决方案5】:

          转到@webpagebot 并向他发送该文件的 URL。电报的缓存将失效,这应该可以工作。好像是服务器的bug。

          在我的情况下,我无法上传图片(作为贴纸),http://.../blabla.webp 不是通过电报应用程序,也不是通过电报机器人 API。

          【讨论】:

          【解决方案6】:

          您的 Awnser 是 here @farzad

          通过 file_id 发送
          file_id 对于每个单独的机器人都是唯一的,不能从一个机器人转移到另一个机器人。

          【讨论】:

          • 必须使用我自己的bot file_id!
          • 我完全错过了文档中的内容,快疯了.. 谢谢!
          【解决方案7】:

          如果您将文件(照片、音频...)转发给机器人,您将获得此文件(用于您的机器人)的有效file_id。使用此 ID 发送文件应该是安全的,但它似乎不适用于某些文件(音频,视频,...)! (可能是 Telegram API 错误)。

          您可以下载文件并将其重新上传到您的机器人,以获取新的file_id,此 ID 将起作用。

          【讨论】:

          • 如果文件太大,他们会给你一个 id,但不要存储它。有点烦人。
          【解决方案8】:

          documentation 中提到的原因有很多:

          • 通过 file_id 重新发送时无法更改文件类型。即视频不能作为照片发送,照片不能作为文档发送等。
          • 无法重新发送缩略图。
          • 通过 file_id 重新发送照片将发送其所有尺寸。
          • file_id 对于每个单独的 bot 都是唯一的,不能转移 从一个机器人到另一个机器人。

          【讨论】:

          • 你先生,是救生员
          猜你喜欢
          • 2018-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-13
          • 1970-01-01
          • 2020-12-12
          • 1970-01-01
          相关资源
          最近更新 更多