【问题标题】:How to send a local photo with telegram HTTP API in python?如何在 python 中使用电报 HTTP API 发送本地照片?
【发布时间】:2020-03-06 08:13:53
【问题描述】:

所以我上了这门课:

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

我想创建一个从 LOCAL LIBRARY 发送图像的方法 通过我的 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 页面上找到答案 有没有人做到或知道如何正确地做到这一点?有没有更好的办法?

谢谢

【问题讨论】:

    标签: python api http bots telegram


    【解决方案1】:

    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,不要重新发明轮子。

    【讨论】:

      猜你喜欢
      • 2019-12-05
      • 1970-01-01
      • 2019-03-28
      • 2019-05-14
      • 1970-01-01
      • 1970-01-01
      • 2016-07-18
      • 2020-07-09
      • 2016-08-15
      相关资源
      最近更新 更多