You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Aiogram发送本地图片?解决BadRequest报错问题

解决Aiogram发送本地图片时的BadRequest错误

问题原因

直接将本地图片路径字符串传递给send_photo的photo参数时,Aiogram会默认将其当作HTTP URL解析,导致触发BadRequest('Wrong http url specified')错误。

解决方案

要发送本地图片,需明确告诉Aiogram这是本地文件,有两种常用方式:

方法1:使用InputFile类

导入InputFile后,将本地路径传入该类实例,再传递给photo参数:

from aiogram.types import InputFile

# 生成图片的代码保持不变
module.full_card(int(userid), tested_word[1], correct_word)
photo_path = f"userdata/{userid}/output.png"
await bot.send_photo(callback_query.from_user.id, photo=InputFile(photo_path))

方法2:传递文件对象

以二进制只读模式打开本地图片,将文件对象传递给photo参数:

# 生成图片的代码保持不变
module.full_card(int(userid), tested_word[1], correct_word)
photo_path = f"userdata/{userid}/output.png"
with open(photo_path, 'rb') as photo_file:
    await bot.send_photo(callback_query.from_user.id, photo=photo_file)

注意事项

  • 确保photo_path指向的文件确实存在,可通过os.path.exists(photo_path)做前置检查
  • 保证程序对该文件所在目录有读取权限

内容的提问来源于stack exchange,提问作者Vlad F

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 19:20:34