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

node-telegram-bot-api发送1.5GB文件失败:触发413错误求助

解决Telegram Bot发送大文件报错413的问题

问题原因

你直接用bot.sendDocument(id, 'test.zip')传本地大文件时,走的是Telegram Bot API的直接文件上传接口,这个接口的上限是50MB,并非你认知里的2GB。2GB的上限仅针对已经上传到Telegram服务器、用File ID发送的文件。

解决方案

需要先把文件上传到Telegram服务器获取File ID,再用这个ID发送文件,具体步骤如下:

  1. 使用bot.uploadDocument方法上传大文件,获取File ID
    针对1.5GB的大文件,建议用文件流读取,避免内存溢出:

    const fs = require('fs');
    const TelegramBot = require('node-telegram-bot-api');
    
    const bot = new TelegramBot('你的Bot Token', { polling: false });
    const chatId = '目标用户ID';
    
    // 创建文件流读取大文件
    const fileStream = fs.createReadStream('test.zip');
    
    // 上传文件到Telegram服务器
    bot.uploadDocument(chatId, fileStream)
      .then((file) => {
        // 提取返回结果中的File ID
        const fileId = file.document.file_id;
        console.log('文件上传成功,File ID:', fileId);
    
        // 使用获取到的File ID发送文件
        return bot.sendDocument(chatId, fileId);
      })
      .then(() => {
        console.log('文件发送成功');
      })
      .catch((error) => {
        console.error('操作失败:', error);
      });
    
  2. 额外注意事项

    • 确保你的Bot Token权限正常,未被限制大文件上传
    • 大文件上传耗时较长,保持网络稳定
    • 如果后续需要重复发送该文件,直接保存File ID复用即可,无需重复上传

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 23:45:58