简体   繁体   English

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

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

How can I make my bot receive and save a file that I send it in a telegram?如何让我的机器人接收并保存我在电报中发送的文件? I want my bot to receive a pdf file that I send from my cell phone.我希望我的机器人接收我从手机发送的 pdf 文件。 I'm doing it in python but I'm half lost about it and I don't know how to solve it.我在 python 中这样做,但我对它迷失了一半,我不知道如何解决它。

I did this but I didn't get a result我这样做了,但我没有得到结果

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)

As you can see in this line I wait to receive the file, but I think I didn't do it right, since I don't receive the following message after that line正如您在这一行中看到的那样,我等待接收文件,但我认为我没有做对,因为在那一行之后我没有收到以下消息

file_content = list._get_file()

Keep in mind that to deal with files on Telegram Bot you must have the file_id of the file.请记住,要处理 Telegram Bot 上的文件,您必须拥有文件的file_id

Handling the document :处理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}')

and on def main() :def main()上:

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

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

相关问题 如何让我的 discord 机器人从 a.txt 文件(python)发送消息 - How can I make my discord bot send messages from a .txt file (python) 如何让我的 discord.py 机器人向我选择的频道发送我选择的消息? - how can i make my discord.py bot send a message i choose to a channel i choose? 我如何在 python-telegram-bot 中接收文件 - How can i receive file in python-telegram-bot 如何让我的 Discord Bot 在每次用户输入“验证”时发送一条消息 - How can I make my Discord Bot send a message every-time a user types 'verify' 如何让我的 discord.py 机器人删除在特定频道上发送的每条消息? - How can I make my discord.py bot delete every message send on a specific channel? 我可以让我的 discord 机器人向所有添加了机器人的服务器发送消息吗? - Can I make my discord bot send a message to all servers that has the bot added to it? 如何使用 python 通过我的 Discord 机器人发送嵌入? - How can I send an embed via my Discord bot, with python? 如何使用 python 机器人将本地 html 文件发送到 Telegram - How can I send local html file to Telegram with python bot 如何让我的不和谐机器人响应正确答案? - How can I make my discord bot respond to the correct answer? 如何在垃圾邮件机器人中设置停止按钮? - How can i make a stop button in my spam bot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM