gpt4 book ai didi

python - 如何使用python电报机器人将pdf文件发送回用户?

转载 作者:行者123 更新时间:2023-12-04 12:35:10 25 4
gpt4 key购买 nike

我尝试使用 update.sendDocument(chat_id = update.message.chat_id, document = open(something.pdf, 'b'))但它没有返回给我一个 pdf 文件。有什么帮助吗?

最佳答案

作为引用,这是一个完整的工作示例,对我有用。
如果它仍然不起作用,请确保您发送的 pdf 文档低于 20 MB
根据 https://core.telegram.org/bots/api#sending-files

#!/usr/bin/python
import sys
import time
import telepot
import cookie
from telepot.loop import MessageLoop
import pdb

def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
print(content_type, chat_type, chat_id)
if content_type == 'text' and msg["text"].lower() == "news":
# let the human know that the pdf is on its way
bot.sendMessage(chat_id, "preparing pdf of fresh news, pls wait..")
file="/home/ubuntu/web/news.pdf"

# send the pdf doc
bot.sendDocument(chat_id=chat_id, document=open(file, 'rb'))
elif content_type == 'text':
bot.sendMessage(chat_id, "sorry, I can only deliver news")

# replace XXXX.. with your token
TOKEN = 'XXXXXXXXXXXXXXXXXXXX'
bot = telepot.Bot(TOKEN)
MessageLoop(bot, handle).run_as_thread()
print ('Listening ...')
# Keep the program running.
while 1:
time.sleep(10)

关于python - 如何使用python电报机器人将pdf文件发送回用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62445753/

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