如何在Python中使用电报HTTP API发送本地照片?

所以我上了这堂课:

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等。

我想创建一个从本地库发送图像的方法 通过我的Bot进入我的电报频道。

我正在寻找看起来像这样的东西:

     def sendImage(self,chat_id,image_path,message)
         url = self.base_url + "sendPhoto?chat_id={}&image_path={}&text={}".format(chat_id,message)
         response = requests.post(url)
         return response

但是没有任何效果,我无法在网络上或电报API页面上找到答案 是否有人做过或知道如何正确做?有更好的方法吗?

谢谢

sdadaadfaaf 回答:如何在Python中使用电报HTTP API发送本地照片?

请注意here,它说明了通过 Telegram Bot API 发送文件的方法。

在Python中,您可以使用它上传照片并将其发送到特定的聊天室:

import requests

token = "your bot token"
chat_id = 1234567  # chat id
file = "photo.jpg"

url = f"https://api.telegram.org/bot{token}/sendPhoto"
files = {}
files["photo"] = open(file,"rb")
requests.get(url,params={"chat_id": chat_id},files=files)
  

我的个人建议:学习和使用library,不要重蹈覆辙。

本文链接:https://www.f2er.com/3129580.html

大家都在问