gpt4 book ai didi

python - Telegram Bot SendDocument pdf

转载 作者:行者123 更新时间:2023-12-05 03:51:25 25 4
gpt4 key购买 nike

我对将 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 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'))

关于python - Telegram Bot SendDocument pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62932381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com