import os
import base64
import logging
import telebot
from cryptography.fernet import Fernet

# Set up the Telegram bot
bot = telebot.TeleBot(os.getenv("BOT_TOKEN"))

# Define constants
START_MESSAGE = "I'm PENT. Please select an option:\nType /enctext to encrypt a message.\nType /dectext to decrypt a message.\n                Use /encfile to encrypt a file.\n               Use /decfile to decrypt a file using your key.\n                My Creator: @itsUndead"

ENCRYPT_MESSAGE = "Please enter the message you want to encrypt."
DECRYPT_KEY_MESSAGE = "Please enter the encryption key."
DECRYPT_MESSAGE_MESSAGE = "Please reply to the message you want to decrypt with the encryption key."
ERROR_MESSAGE = "Unable to perform the requested operation. Please try again later."

HELP_MESSAGE =  "Use /entext to encrypt a message.\n"\
                "Use /dectext to decrypt a message using your key.\n"\
                "Use /encfile to encrypt a file.\n"\
                "Use /decfile to decrypt a file using your key.\n"\
                "My Creator: @itsUndead"

FILE_ENCRYPTED_MESSAGE = "File Encrypted!"
FILE_ENCRYPTION_KEY_MESSAGE = "File Encryption Key (keep it private and secure!)"
FILE_ENCRYPTION_FAILED = "Error File Encryption!"
FILE_GET_MESSAGE = "Send the file to encode or decode, I’ll wait here…"
FILE_DECRYPTION_FAILED = "Error File Decryption!"

# Set up logging
logging.basicConfig(
  format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  level=logging.INFO)


# Define decorators
def command_handler(commands):

  def decorator(func):

    @bot.message_handler(commands=commands)
    def wrapper(message):
      try:
        func(message)
      except Exception as e:
        logging.error(f"Error in {func.name}: {e}")
        bot.send_message(message.chat.id, ERROR_MESSAGE)

    return wrapper

  return decorator


###########################
# Define command handlers #
###########################


@command_handler(['start'])
def start(message):
  bot.send_message(message.chat.id, START_MESSAGE)


@command_handler(['help'])
def help(message):
  bot.send_message(message.chat.id, HELP_MESSAGE)


@command_handler(['enctext'])
def encrypt(message):
  bot.send_message(message.chat.id, ENCRYPT_MESSAGE)
  bot.register_next_step_handler(message, encrypt_message)


@command_handler(['dectext'])
def decrypt(message):
  bot.send_message(message.chat.id, DECRYPT_KEY_MESSAGE)
  bot.register_next_step_handler(message, decrypt_message_key)


###############################################
# Define file handlers and encryption methods #
###############################################


def encrypt_file(file_path):
  try:
    key = Fernet.generate_key()
    cipher_suite = Fernet(key)
    with open(file_path, 'rb') as file:
      contents = file.read()
    encrypted_contents = cipher_suite.encrypt(contents)
    encrypted_file_path = f"{file_path}.encrypted"
    with open(encrypted_file_path, 'wb') as file:
      file.write(encrypted_contents)
    return key, encrypted_file_path
  except Exception as e:
    logging.error(f"Error in encrypt_file: {e}")
    return None, None


def decrypt_file(file_path, key):
  try:
    cipher_suite = Fernet(key)
    with open(file_path, 'rb') as file:
      contents = file.read()
    decrypted_contents = cipher_suite.decrypt(contents)
    decrypted_file_path = f"{file_path}.decrypted"
    with open(decrypted_file_path, 'wb') as file:
      file.write(decrypted_contents)
    return decrypted_file_path
  except Exception as e:
    logging.error(f"Error in decrypt_file: {e}")
    return None


@bot.message_handler(content_types=[
  'document', 'application', 'audio', 'video', 'voice', 'photo'
])
def handle_file(message):
  chat_id = message.chat.id
  file_info = bot.get_file(message.document.file_id)
  if message.text == '/encfile':
    downloaded_file = bot.download_file(file_info.file_path)
    file_name = message.document.file_name
    with open(file_name, 'wb') as file:
      file.write(downloaded_file)
    key, encrypted_file_path = encrypt_file(
      file_name)  # Encrypt the file contents
    if key is not None:
      bot.send_message(chat_id, FILE_ENCRYPTED_MESSAGE)
      bot.send_document(chat_id,
                        open(encrypted_file_path,
                             'rb'))  # Send the encrypted file back to the user
      bot.send_message(chat_id, FILE_ENCRYPTION_KEY_MESSAGE)
      bot.send_message(chat_id, key)  # Send the key to the user
      os.remove(file_name)  # Remove the original file from the server
      os.remove(
        encrypted_file_path)  # Remove the encrypted file from the server
    else:
      bot.send_message(chat_id, FILE_ENCRYPTION_FAILED)
  elif message.text == '/decfile':
    key = message.reply_to_message.text  # Get the key from the previous message
    downloaded_file = bot.download_file(file_info.file_path)
    file_name = message.document.file_name
    with open(file_name, 'wb') as file:
      file.write(downloaded_file)
    decrypted_file_path = decrypt_file(file_name,
                                       key)  # Decrypt the file contents
    if decrypted_file_path is not None:
      bot.send_message(chat_id, "Here is your decrypted file:")
      bot.send_document(chat_id,
                        open(decrypted_file_path,
                             'rb'))  # Send the decrypted file back to the user
      os.remove(file_name)  # Remove the original file from the server
      os.remove(
        decrypted_file_path)  # Remove the decrypted file from the server
    else:
      bot.send_message(chat_id, FILE_DECRYPTION_FAILED)
      os.remove(file_name)  # Remove the original file from the server
  else:
    bot.send_message(
      chat_id,
      "Invalid command. Please use /encfile or /decfile to encrypt or decrypt the file."
    )


@bot.message_handler(commands=['encfile', 'decfile'])
def handle_command(message):
  chat_id = message.chat.id
  if message.text == '/encfile':
    bot.send_message(chat_id, FILE_GET_MESSAGE)
  elif message.text == '/decfile':
    bot.send_message(
      chat_id,
      "Please upload the file you want to decrypt and reply to the message containing the key obtained in the /encfile step."
    )


######################################
# Define message encryption handlers #
######################################
def encrypt_message(message):
  try:
    key = Fernet.generate_key()
    cipher_suite = Fernet(key)
    key_encoded = base64.urlsafe_b64encode(key)
    encrypted_message = cipher_suite.encrypt(message.text.encode())
    encoded_message = base64.urlsafe_b64encode(encrypted_message)
    response = f"<b>Encrypted message:</b>\n<code>{encoded_message.decode()}</code>\n\n<b>Encryption key:</b>\n<code>{key_encoded.decode()}</code>\n\nPlease keep the encryption key secure as it’s needed to decrypt the message."
    bot.send_message(message.chat.id, response, parse_mode="HTML")
  except Exception as e:
    logging.error(f"Error in encrypt_message: {e}")
    bot.send_message(message.chat.id, ERROR_MESSAGE)


def decrypt_message_key(message):
  try:
    key = base64.urlsafe_b64decode(message.text.encode())
    bot.send_message(message.chat.id, DECRYPT_MESSAGE_MESSAGE)
    bot.register_next_step_handler(message, decrypt_message, key)
  except:
    bot.send_message(message.chat.id,
                     "Invalid encryption key. Please try again.")


def decrypt_message(message, key):
  try:
    cipher_suite = Fernet(key)
    encrypted_message = base64.urlsafe_b64decode(message.text.encode())
    decrypted_message = cipher_suite.decrypt(encrypted_message)
    response = f"<b>Decrypted message:</b>\n<code>{decrypted_message.decode()}</code>"
    bot.send_message(message.chat.id, response, parse_mode="HTML")
  except:
    bot.send_message(
      message.chat.id,
      "Unable to decrypt message. Please make sure the message and encryption key are correct."
    )


#################
# Start the bot #
#################

bot.polling()


my bot sends me this response but its not correct: Invalid command. Please use /encfile or /decfile to encrypt or decrypt the file.

fix it for me please using @command_handler() method