API通过URL将照片发送给电报机器人 [英] API send photo to telegram bot by URL
本文介绍了API通过URL将照片发送给电报机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨,我想通过telegram API
将照片发送到我的机器人,我必须使用API URL发送,不能使用库。
我已尝试:
token = " "
chat_id = " "
pic = ("C:\Windows\pic\hello.jpg")
url = requests.post("https://api.telegram.org/bot"+token+"/sendPhoto?chat_id="+chat_id+"&photo="+pic)
print(url)
和结果:
{"ok":false,"error_code":400,"description":"Bad Request: wrong HTTP URL specified"}
推荐答案
首先sendPhoto
不是sendphoto
。
这是link for docs供您参考。
请注意,您为POST请求提供的URL为:https://api.telegram.org/bot"+token+"/sendphoto?chat_id="+chat_id+"&photo="+pic
但实际应该是https://api.telegram.org/bot"+token+"/sendPhoto?chat_id="+chat_id+"&photo="+pic
其次您正在尝试发送本地托管的图像。发送本地托管图像与发送在线图像的URL略有不同。
您必须将图像文件作为词典与POST请求一起发送,如下所示:
import requests
img = open(your/local/image, 'rb')
TOKEN =
CHAT_ID =
url = f'https://api.telegram.org/bot{TOKEN}/sendPhoto?chat_id={CHAT_ID}'
print(requests.post(url, files={'photo': img}))
输出:
<Response [200]>
这篇关于API通过URL将照片发送给电报机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文