Telegram Bot调用sendPhoto报错:错误的文件标识符/HTTP URL指定
Telegram Bot sendPhoto 调用失败:群晖监控缩略图URL问题解决
问题根源
是的,你遇到的错误确实和这个URL触发下载有关。Telegram的sendPhoto接口要求传入的图片URL必须直接返回可在浏览器中直接渲染的图片资源,而你的群晖缩略图URL返回的响应头里包含Content-Disposition: attachment,会触发文件下载而非直接显示图片,导致Telegram无法正确识别这是一张图片资源,从而返回400错误。
你可以通过以下命令验证响应头:
curl -I "https://mystation.synology.me:5001/webapi/SurveillanceStation/Webhook/GetThumbnail/v1/<some-token>/thumbnail.jpg"
如果输出中存在Content-Disposition: attachment; filename="thumbnail.jpg",就可以确认是这个原因。
解决方法
方法1:先下载图片再上传(通用方案)
先将群晖的缩略图下载到本地/服务器,再通过multipart/form-data格式直接上传文件给Telegram接口,绕过URL识别的限制。
示例(curl命令)
# 先下载图片 curl -o thumbnail.jpg "https://mystation.synology.me:5001/webapi/SurveillanceStation/Webhook/GetThumbnail/v1/<some-token>/thumbnail.jpg" # 上传到Telegram curl -X POST "https://api.telegram.org/bot<你的Bot令牌>/sendPhoto" \ -F "chat_id=<目标Chat ID>" \ -F "photo=@thumbnail.jpg"
示例(Python requests库)
import requests # 下载群晖缩略图 syno_thumbnail_url = "https://mystation.synology.me:5001/webapi/SurveillanceStation/Webhook/GetThumbnail/v1/<some-token>/thumbnail.jpg" image_content = requests.get(syno_thumbnail_url).content # 发送到Telegram telegram_api_url = "https://api.telegram.org/bot<你的Bot令牌>/sendPhoto" request_data = {"chat_id": "<目标Chat ID>"} files = {"photo": ("thumbnail.jpg", image_content, "image/jpeg")} response = requests.post(telegram_api_url, data=request_data, files=files) print(response.json())
方法2:修改群晖响应头(可选,需权限)
如果你的群晖监控站允许配置Webhook接口的响应头,可以将Content-Disposition改为inline,这样URL会直接返回可显示的图片,Telegram接口就能正常识别。不过群晖的官方Webhook接口通常没有开放这个配置项,所以这个方法可行性较低。
内容的提问来源于stack exchange,提问作者CrazyEight
相关产品推荐
相关产品推荐

