簡體   English   中英

如何在 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 等。

我想創建一種方法,通過我的 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

但是沒有任何工作,我在 web 或電報 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)

我個人的建議:學習和使用圖書館,不要重新發明輪子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM