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

aiogram调用send_file报错缺少file与payload参数如何解决?

报错原因
  • 调用了不存在的公开方法:aiogram 2.x版本的Bot实例没有面向开发者的send_file()公开接口,你调用的是内部底层未适配的方法,触发参数校验错误。
  • 回调函数参数类型错误:消息触发的处理函数入参应为types.Message类型,你错误指定为types.File,会导致后续参数解析异常。
  • 基础语法错误:第一行导入语句的From首字母大写不符合Python语法要求,异步函数内的代码、主函数启动代码没有正确缩进。
  • 命令匹配规则问题:Telegram命令默认小写,commands=['Start']会匹配不到用户输入的/start指令。
修复方案

根据你要发送的文件类型选择对应方法:发送图片用send_photo(),发送音频用send_audio(),发送通用格式文件用send_document()即可。

修复后完整代码
from handlers import TOKEN
from aiogram import Bot, Dispatcher, executor, types
from aiogram.types import InputFile

bot = Bot(token=TOKEN)
dp = Dispatcher(bot)

# 可根据实际文件类型修改变量名,当前路径对应为jpg图片
media_file = InputFile(r'C:\Users\Acer\Desktop\файлы для примера\Apple-tv-iso-b-01-top.jpg')

@dp.message_handler(commands=['start'])
async def process_hello(message: types.Message):
    # 发送图片用send_photo,发音频换send_audio,发普通文件换send_document
    await bot.send_photo(message.from_user.id, media_file)

if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)
修改点说明
  • 将不存在的send_file()替换为aiogram提供的公开文件发送方法
  • 修正处理函数入参类型为types.Message
  • 修正导入语句关键字大小写、代码缩进问题
  • 命令改为小写start适配Telegram默认规则

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 19:54:03