将驱动文件发送到 python 电报机器人

时间:2021-03-07 04:56:26

标签: python-3.x telegram telegram-bot python-telegram-bot

我试图让我的电报机器人下载 Google 驱动器上的 pdf 文档,但我无法让它工作。 我编写了以下代码以使其正常工作,但没有这种情况:

url = bot.get_File(link='link of google drive') down = url.download() bot.sendDocument(chat_id=ChatId, document=open(down,'rb'), filename="Lista de precios.pdf")

出于隐私原因,我没有放置谷歌驱动器链接,但显然它会放在那里。 我不知道是否可以像这样从驱动器下载文件。

1 个答案:

答案 0 :(得分:0)

看来您使用了错误的方法,get_file 用于 download 来自 Telegram 的文件。
通过 Telegram bot 发送文件时,您可以使用完整 URL 如果指向文件(即 file.pdf)

bot.send_document(chat_id=Chat_id, document='https://github.com/user/project/raw/master/data/pdf/07-03-2020.pdf'

在 Google Drive 的情况下,您有一个用于流式传输文件的 URL,因此您需要1) 下载并在本地保存文件 2) 打开本地文件并发送它作为文件类型

r = requests.get('googleDriveUrl', allow_redirects=True)
open('file.ppt', 'wb').write(r.content)

bot.send_document(chat_id=Chat_id, document=open('file.ppt', 'rb'), filename="file.pptx")
相关问题