如何在python中用telegram 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, image_path, message)
response = requests.post(url)
return response

但什么都不起作用,我在网上或电报API页面上找不到答案是否有人制作或知道如何正确制作?有更好的方法吗?

感谢

看这里,它解释了许多关于通过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)

我的个人建议:学习和使用图书馆,不要重新发明轮子。

最新更新