Проблема с телеграм ботом (Python)
Хотел создать бота, который принимает сначала текст - закодируй/разкодируй, а потом шифрует или разшифровывает файл. Но столкнулся с проблемой, что бот принимает только первое сообщение и будет постоянно зашифровывать или разшифровывать. Я мало что понимаю, поэтому прошу помощи.
Вот код:
import telebot
from Crypto.Cipher import AES
import hashlib
bot = telebot.TeleBot(удалено)
def pad_message(message):
while len(message) % 16 != 0:
message += ' '
return message
def encode_AES(message):
password = 'cum'.encode()
key = hashlib.sha256(password).digest()
mode = AES.MODE_CBC
IV = 'This is an IV456'
cipher = AES.new(key, mode, IV)
padded_message = pad_message(message)
encrypted_message = cipher.encrypt(padded_message)
return encrypted_message
def decode_AES(message):
mode = AES.MODE_CBC
IV = 'This is an IV456'
d_password = b'cum'
d_key = hashlib.sha256(d_password).digest()
d_cipher = AES.new(d_key, mode, IV)
decrypted_message = d_cipher.decrypt(message)
return decrypted_message
@bot.message_handler(commands=['start'])
def welcome(message):
bot.send_message(message.chat.id, 'Привет, я могу зашифровать любой файл!')
@bot.message_handler(content_types=['text'])
def talk_about(message):
if message.chat.type == 'private':
if message.text == 'Закодируй':
@bot.message_handler(content_types=['document'])
def handle_docs_photo(message):
try:
file_info = bot.get_file(message.document.file_id)
downloaded_file = bot.download_file(file_info.file_path)
src = '/home/user/PycharmProjects/CodeHuffman/' + message.document.file_name
encode_downloaded_file = encode_AES(downloaded_file)
with open(src, 'wb') as new_file:
new_file.write(encode_downloaded_file)
bot.send_document(message.chat.id, open(src, 'rb'))
bot.reply_to(message, "Закодировано, сэр!")
except Exception as e:
bot.reply_to(message, e)
elif message.text == 'Разкодируй':
@bot.message_handler(content_types=['document'])
def handle_docs_photo(message):
try:
file_info = bot.get_file(message.document.file_id)
downloaded_file = bot.download_file(file_info.file_path)
src = '/home/user/PycharmProjects/CodeHuffman/' + message.document.file_name
encode_downloaded_file = decode_AES(downloaded_file)
with open(src, 'wb') as new_file:
new_file.write(encode_downloaded_file)
bot.send_document(message.chat.id, open(src, 'rb'))
bot.reply_to(message, "Разкодировано, сэр!")
except Exception as e:
bot.reply_to(message, e)
bot.polling(none_stop=False)
