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

How to send a local photo with telegram HTTP API in python?

所以我得到了这个 class :

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 页面上找到答案 有没有人做到或知道如何正确地做到这一点?有没有更好的方法呢?

谢谢

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)

My personal advice: learn and use a library, don't reinvent the wheel.