Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
API通过URL向电报机器人发送照片_Api_Telegram_Telegram Bot - Fatal编程技术网

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