如何在python中使用电报HTTP API发送本地照片?
所以我上了这门课:如何在python中使用电报HTTP API发送本地照片?,python,api,http,bots,telegram,Python,Api,Http,Bots,Telegram,所以我上了这门课: import os #for getting the directory location class telegramBot(object): def __init__(self, token = telegram_token, chat_id = telegram_chat_id): self.token = token self.chat_id = chat_id self.base_url = "https:
import os #for getting the directory location
class telegramBot(object):
def __init__(self, token = telegram_token, chat_id = telegram_chat_id):
self.token = token
self.chat_id = chat_id
self.base_url = "https://api.telegram.org/bot{}/".format(token)
它包含许多方法,如sendMessage、replyToMessage等
我想创建一个从本地库发送图像的方法
通过我的机器人连接到我的电报频道
我正在寻找这样的东西:
def sendImage(self, chat_id, image_path, message)
url = self.base_url + "sendPhoto?chat_id={}&image_path={}&text={}".format(chat_id, image_path, message)
response = requests.post(url)
return response
但是什么都不起作用,我在网上或电报API页面上找不到答案
有没有人做到了,或者知道如何正确地做到?有没有更好的办法
谢谢看,它解释了很多关于通过电报机器人API发送文件的内容
在Python中,您可以使用此选项上载照片并将其发送到特定聊天室:
导入请求
token=“您的机器人令牌”
聊天室id=1234567#聊天室id
file=“photo.jpg”
url=f“https://api.telegram.org/bot{token}/sendPhoto“
文件={}
文件[“照片”]=打开(文件“rb”)
获取(url,参数={“chat\u id”:chat\u id},files=files)
我个人的建议是:学习并使用一种新的方法,不要重新发明轮子