Python 3.x 如何使用电报api在我的机器人上发送图像?
谁能告诉我如何使用python中的请求库从internet发送照片 我目前正在尝试以以下方式发送照片:Python 3.x 如何使用电报api在我的机器人上发送图像?,python-3.x,telegram-bot,Python 3.x,Telegram Bot,谁能告诉我如何使用python中的请求库从internet发送照片 我目前正在尝试以以下方式发送照片: https://api.telegram.org/bot<token>/sendPhoto-Fchat_id=<MY-ID>-Fphoto=https://random.dog/7e1527a4-9c07-48df-a877-4c6e4681adc1.jpg https://api.telegram.org/bot/sendPhoto-Fchat_id=-Fphoto
https://api.telegram.org/bot<token>/sendPhoto-Fchat_id=<MY-ID>-Fphoto=https://random.dog/7e1527a4-9c07-48df-a877-4c6e4681adc1.jpg
https://api.telegram.org/bot/sendPhoto-Fchat_id=-Fphoto=https://random.dog/7e1527a4-9c07-48df-a877-4c6e4681adc1.jpg
提前感谢您您传递了错误的url参数 使用
?
和&
符号,而不是关闭sendPhoto-Fchat\u id
。如果要添加多个参数;看看
sendPhoto?chat_id=123&photo=somelink
要回答此问题,您可以使用如下请求库:
import requests
from requests.exceptions import HTTPError
url="https://api.telegram.org/bot<MY-TOKEN>/sendPhoto?chat_id=<MY-CHAT-ID>&photo=http://via.placeholder.com/100x100"
try:
response = requests.get(url)
# If the response was successful, no Exception will be raised
response.raise_for_status()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}') # Python 3.6
except Exception as err:
print(f'Other error occurred: {err}') # Python 3.6
else:
print('Success!')
导入请求
从requests.exceptions导入HTTPError
url=”https://api.telegram.org/bot/sendPhoto?chat_id=&photo=http://via.placeholder.com/100x100"
尝试:
response=requests.get(url)
#如果响应成功,则不会引发异常
响应。针对_状态()提出_
除了HTTPError作为http_err之外:
打印(发生了f'HTTP错误:{HTTP_err}')#Python 3.6
除异常作为错误外:
打印(f'发生其他错误:{err}')#Python 3.6
其他:
打印('Success!')