【问题标题】:Telegram Bot SendDocument pdfTelegram Bot SendDocument pdf
【发布时间】:2020-11-05 23:39:11
【问题描述】:

我对将 pdf 文件发送到 Telegram Bot 的方式感到非常头疼。 显然我正在关注文档,但从未将其发送。 我使用的是网址:https://api.telegram.org/botBOTID/sendDocument?chat_id=CHATID&document=/home/lix/Downloads/2.pdf

这是一个本地存储的 pdf 文件,但我认为这正是我呈现它的方式。 得到的错误是: {"ok":false,"error_code":400,"description":"错误请求:URL 主机为空"} 有人知道如何发送pdf本地文件吗? 非常感谢

【问题讨论】:

    标签: python bots telegram


    【解决方案1】:

    您应该使用Python Requests library 发送一个POST 请求,并将PDF 作为有效负载,您的代码应该如下所示:

    import requests
    
    # Url with bot token + user id
    url = "https://api.telegram.org/bot<MY-BOT-TOKEN>/sendDocument?chat_id=<MY_CHAT_ID>"
    
    # Create payload with PDF file
    payload = {}
    files = [
        ('document', open('/home/lix/Downloads/2.pdf','rb'))
    ]
    headers= {}
    
    # Request
    response = requests.request("POST", url, headers=headers, data = payload, files = files)
    
    # Log reponse as UTF-8
    print(response.text.encode('utf8'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 2020-06-10
      • 2018-11-30
      相关资源
      最近更新 更多