2

我正在尝试在电报机器人中使用 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() 创建的本地文件,它会存在并提供正确的图像。我还尝试在图像名称中指定图像的扩展名,但仍然存在相同的错误。

谢谢你的帮助

4

1 回答 1

1

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

于 2021-08-01T20:03:05.867 回答