在这段代码中:
@bot.message_handler(content_types=["document"])
def sending_document(message):
if ".docx" in message.document.file_name:
bot.send_document(message.chat.id, message.document) #<- problem
我不知道从哪里获取消息中的文档。
我尝试了很多事情,但都没有成功。
免责声明:我熟悉
telebot
库,但熟悉 Telegram Bot API 本身。
sendDocument
需要参数 document
要么是 URL、文件 ID 要么是分段上传的文件。然而, message.document
是一个 Document
对象。该文档的 file_id 应该可以通过 message.document.file_id
获得,所以我希望 bot.send_document(message.chat.id, message.document.file_id
能够工作。
正如CallMeStag提到的,您可以使用
bot.send_document(message.chat.id, message.document.file_id
(这是正确的)。bot.send_document 接受 chat_id 和“document”作为参数。
正如Telebot文档中所述,文档可能是来自电报服务器的文件ID或互联网中任何文件的URL
但是我没有看到他们提到你可以传递文件描述符。示例:
import telebot
bot = telebot.TeleBot("token here")
f = open("any_existing_file")
bot.send_document(chat_id, f)
f.close()
如果您只想原封不动地向他们发送文档,您应该使用file_id,但如果您想在发送之前修改文档,您可以通过这种方式发送。
message
),请考虑使用 VS Code 并显式设置变量类型:# example
bot: telebot.TeleBot = telebot.TeleBot("token here")
#....
@bot.message_handler(...)
def funcName(message: telebot.types.Message):
# do stuff
#...
现在 VS Code 会知道变量的类型,并且会向您显示它们的所有属性,帮助您学习。
您不需要每次都显式设置变量类型,但这有助于编写代码。