电报API sendPhoto返回“错误请求:文件类型不匹配”

2024-06-12 09:23:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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

谢谢你的帮助


Tags: pathimageidbytesrequest错误chatopen
1条回答
网友
1楼 · 发布于 2024-06-12 09:23:07

我以前有过这个问题,但在其他前提条件下。我试图在发送视频时指定fileId,但该fileId被识别为文档,而不是视频。我重新上传了文件作为视频,指定了新的文件ID,它工作了

相关问题 更多 >