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

如何用Telegram Bot发送JSON文件?调用sendDocument遇报错求助

解决Grammy sendDocument报"invalid file HTTP URL specified: Wrong port number"错误

错误原因

你在调用fs.createReadStream时指定了utf8编码,这会让流输出文本字符串而非原始二进制数据。Grammy的InputFile在接收到字符串时,会默认将其当作HTTP URL解析,但你的文件路径不符合URL规范,因此触发了端口错误。

修正方案

去掉createReadStream的编码参数,保持文件流为原始二进制格式,即可让InputFile正确识别为本地文件进行发送。

修正后的代码

const file = fs.createReadStream(path.join(__dirname, "files", "history_988386602_2.json"))
const document = new InputFile(file)

await Bot.api.sendDocument(msg.chatId, document)

简化写法(可选)

部分版本的Grammy支持直接传入文件路径给InputFile,无需手动创建流,代码更简洁:

const document = new InputFile(path.join(__dirname, "files", "history_988386602_2.json"))

await Bot.api.sendDocument(msg.chatId, document)

额外检查

  • 确认path.join拼接后的路径指向的JSON文件确实存在
  • 确保Bot拥有读取该文件的权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 10:33:12