Пельмень Asked:2022-08-16 02:05:57 +0000 UTC2022-08-16 02:05:57 +0000 UTC 2022-08-16 02:05:57 +0000 UTC 如何从用户发送给 Telegram bot 的 .txt 文件中获取文本?(远程机器人库) 772 用户发送一个 .txt 文件。在检查文件是否为 .txt 格式后,您需要从那里获取文本并将其保存为字符串 我编写了一个处理程序来获取文件,但我无法从中获取文本,而且我不知道如何检查文件类型。 远程机器人库 @bot.message_handler(content_types=['document']) def get_new_credentials(message): file_info = bot.get_file(message.document.file_id) python telebot 1 个回答 Voted Best Answer Dr Proger 2022-08-16T16:37:16Z2022-08-16T16:37:16Z 我们保存用户发送的文件: @bot.message_handler(content_types=['document']) def handle_file(message): try: chat_id = message.chat.id file_info = bot.get_file(message.document.file_id) downloaded_file = bot.download_file(file_info.file_path) src = 'C:/Python/Project/bot/files/received/' + message.document.file_name; with open(src, 'wb') as new_file: new_file.write(downloaded_file) except Exception as e: bot.reply_to(message, e) log(message) 检查文件扩展名: if 'txt' == message.document.file_name.split('.')[1]: return True
我们保存用户发送的文件:
检查文件扩展名: