【问题标题】:Telegram API sendPhoto gives back "Bad Request: type of file mismatch"Telegram API sendPhoto 返回“错误请求:文件类型不匹配”
【发布时间】:2020-08-11 11:32:57
【问题描述】:

我正在尝试在电报机器人中使用 Telepot python 库上传照片。

fd, path = tempfile.mkstemp()

image_bytes = requests.get(url=fileUrl, stream=True)
with open(path, 'wb') as strm:
   for chunk in image_bytes:
      strm.write(chunk)

photoFile = open(path, mode='rb') 
fileId = bot.sendPhoto(chat_id='chat_id', photo=photoFile)

这段代码给了我这个错误:

telepot.exception.TelegramError: ('Bad Request: type of file mismatch', 400, {'ok': False, 'error_code': 400, 'description': 'Bad Request: type of file mismatch'})

如果我尝试访问由 tempfile.mkstemp() 创建的本地文件,它会存在并提供正确的图像。 我也试过在图片名称中指定图片的扩展名,但还是一样的错误。

感谢您的帮助

【问题讨论】:

    标签: python image bots telegram telepot


    【解决方案1】:

    我以前遇到过这个问题,但在其他先决条件下。我在发送视频时尝试指定 fileId,但该 fileId 被识别为 Document,而不是 Video。我将文件重新上传为视频,指定了新的 fileId 并且它起作用了。

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 2016-09-11
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多