【问题标题】:How can I make my bot receive and save a file I send it?如何让我的机器人接收并保存我发送的文件?
【发布时间】:2021-01-13 23:49:07
【问题描述】:

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

【问题讨论】:

    标签: python telegram chatbot telegram-bot python-telegram-bot


    【解决方案1】:

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

    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()
    

    【讨论】:

      【解决方案2】:

      请记住,要处理 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))
      

      【讨论】:

        猜你喜欢
        • 2021-05-08
        • 2021-06-13
        • 2019-06-29
        • 2017-11-06
        • 2021-12-30
        • 2021-11-30
        • 2022-01-23
        • 1970-01-01
        • 2021-08-17
        相关资源
        最近更新 更多