繁体   English   中英

如何让我的机器人接收并保存我发送的文件?

[英]How can I make my bot receive and save a file I send it?

如何让我的机器人接收并保存我在电报中发送的文件? 我希望我的机器人接收我从手机发送的 pdf 文件。 我在 python 中这样做,但我对它迷失了一半,我不知道如何解决它。

我这样做了,但我没有得到结果

list="filename.pdf"

def CambiarLista(update, context):
    bot = context.bot
    text = update.message.text
    ChatId = update.message.chat_id
    update.message.reply_text('Enviame el documento!')
    file_content = list._get_file()
    update.message.reply_text('Este es el documento que recibi:' + file_content + 'es correcto?')
    keyboard = []
    keyboard.append([KeyboardButton(f'Si, remplazar y guardar', callback_data='9'), KeyboardButton(f'No, eliminar y salir', callback_data='10')])
    reply_markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True, resize_keyboard=True)
    update.message.reply_text('Elige una de las siguientes opciones:',  reply_markup=reply_markup)

正如您在这一行中看到的那样,我等待接收文件,但我认为我没有做对,因为在那一行之后我没有收到以下消息

file_content = list._get_file()

请记住,要处理 Telegram Bot 上的文件,您必须拥有文件的file_id

处理document

def file_handler (update, context):
    chat_id = update.message.chat_id    
    
    ## bot geting the document and its file_name
    doc = update.message['document']['file_id'].get_file()
    fileName = update.message['document']['file_name']
    
    ##bot saving this file to a directory on your PC
    doc.download(f'{path_to_your_directory}\\{fileName}')

def main()上:

dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.document, file_handler))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM