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

如何通过Telegram API使用本地硬盘文件路径发送文档?

使用Telegram Bot API发送本地文件(sendDocument接口)

你之前用GET请求传网络URL的方式没法直接用本地文件路径,必须通过POST请求+multipart/form-data格式来上传本地文件,以下是具体实现方式:

方法1:用curl命令直接上传

在终端或命令提示符里执行以下命令,记得替换占位符:

curl -X POST "https://api.telegram.org/bot<你的BotToken>/sendDocument" \
  -F "chat_id=<你的聊天ID>" \
  -F "document=@C:\Users\photo06b010.jpg"
  • 这里的@符号是curl的语法,用来指定要上传的本地文件路径
  • 如果路径包含空格,把路径用双引号包裹,比如@"C:\Users\My Photo.jpg"

方法2:用Python代码实现

用requests库发送POST请求,示例代码如下:

import requests

# 替换成你的实际信息
bot_token = "你的BotToken"
chat_id = "目标聊天ID"
local_file_path = r"C:\Users\photo06b010.jpg"

api_url = f"https://api.telegram.org/bot{bot_token}/sendDocument"

# 以二进制模式打开文件并上传
with open(local_file_path, "rb") as file:
    request_data = {"chat_id": chat_id}
    file_data = {"document": file}
    response = requests.post(api_url, data=request_data, files=file_data)
    
# 打印接口返回结果
print(response.json())

注意事项

  • BotToken是你从@BotFather那里获取的机器人令牌,格式类似123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
  • 聊天ID可以通过调用getUpdates接口获取:发送一条消息给机器人后,访问https://api.telegram.org/bot<你的BotToken>/getUpdates,返回结果里的message.chat.id就是聊天ID
  • 确保机器人有权限给目标聊天发送消息(比如如果是群组,要先把机器人拉进群组并赋予发送消息的权限)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 16:00:55