发送照片URL时,此错误将显示错误的文件标识符/HTTP URL指定
为了带来一个简单的案例来分析与其他现有问题不同的情况,我带来了一个真正的URL,其中包含一个可公开访问的图像,并且我在其他几件事中使用它进行了测试:
import requests
import json
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36"
}
def send_photo(id_chat,bot_token,text_msg,file_photo):
address = f'https://api.telegram.org/bot{bot_token}/sendPhoto'
data = {"chat_id": id_chat, "photo": file_photo, "caption": text_msg}
response = requests.post(address, data=data, headers=headers)
print(json.loads(response.text))
send_photo('AAA', 'BBB', 'test', 'https://redecanais.to/imgs-videos/Series/Anos-Luz%201.jpg')
所交付的错误是:
{'ok': False, 'error_code': 400, 'description': 'Bad Request: wrong file identifier/HTTP URL specified'}
我应该如何继续这样做URL作为价值工作?
注意:我知道下载照片并将其用作发送的文件是一个选项,但是我想了解为什么发生错误以及如何直接使用URL进行。
如果您想查看文档:
https://core.telegram.telegram.org/bots/api#sendphoto
To bring an easy case to analyze different from the other existing questions, I brought a real URL that contains an image publicly accessible to everyone and that I tested using it in several other things:
import requests
import json
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36"
}
def send_photo(id_chat,bot_token,text_msg,file_photo):
address = f'https://api.telegram.org/bot{bot_token}/sendPhoto'
data = {"chat_id": id_chat, "photo": file_photo, "caption": text_msg}
response = requests.post(address, data=data, headers=headers)
print(json.loads(response.text))
send_photo('AAA', 'BBB', 'test', 'https://redecanais.to/imgs-videos/Series/Anos-Luz%201.jpg')
The error delivered is:
{'ok': False, 'error_code': 400, 'description': 'Bad Request: wrong file identifier/HTTP URL specified'}
How should I proceed to make this URL work as a value?
Note: I understand that downloading the photo and using it as a file to send would be an option, but I want to understand why the error occurs and how I should proceed using URL's directly.
If you want to review the documentation:
https://core.telegram.org/bots/api#sendphoto
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要通过照片URL,则应通过电报服务器访问URL。
显然没有正确的标头,我们会收到访问拒绝的错误页面。
因此,您应该在本地下载图像,然后将文件上传为Multipart/form-data
If you are trying to pass the photo url, the url should be accessible by Telegram servers.
Apparently without the correct headers we receive a Access Denied error page.
Hence, you should download the image locally and upload the file as multipart/form-data