API通过URL向电报机器人发送照片
你好,我想通过API通过URL向电报机器人发送照片,api,telegram,telegram-bot,Api,Telegram,Telegram Bot,你好,我想通过电报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_
电报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
这是你的参考资料
请注意,您将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请求一起发送,如下所示:
导入请求
img=open(您的/本地/图像'rb')
令牌=
聊天室ID=
url=f'https://api.telegram.org/bot{TOKEN}/sendPhoto?chat_id={chat_id}'
打印(requests.post(url,files={'photo':img}))
输出:
这对我很有效,你的问题到底是什么?我更新了我的提问并改进了,请查克·阿吉安·tnxI更新了答案,看一看。你应该使用
sendPhoto
并在post请求中以dict的形式传递本地托管映像。兄弟,我确实将你的代码复制到了我的项目中,但我不知道为什么我的代码工作得很好,请确保你在img
中以img=open的形式传递本地映像(“C:\\Windows\\pic\\hello.jpg”,“rb”)
并将url
设置为url=”https://api.telegram.org/bot“+TOKEN+”/sendPhoto?chat_id=“+chat_id”)
如果您不想像我在回答中那样使用f字符串。`BRO VERY THX您的代码工作正常,TNX