import os import base64 import logging import telebot from cryptography.fernet import…
- User
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 wrapperreturn 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
- Assistant
The issue seems to be with the code for handling file commands (/encfile and /decfile) in the handle_command() function. To fix it using the @command_handler() method, we can refactor it as follows:
@command_handler(['encfile']) def encrypt_file_command(message): bot.send_message(message.chat.id, FILE_GET_MESSAGE) @command_handler(['decfile']) def decrypt_file_command(message): bot.send_message( message.chat.id, "Please upload the file you want to decrypt and reply to the message containing the key obtained in the /encfile step." )Then we can remove the handle_command() function and update the file message handler with the command check:
@bot.message_handler(content_types=[ 'document', 'application', 'audio', 'video', 'voice', 'photo' ]) def handle_file(message): chat_id = message.chat.id command = message.text file_info = bot.get_file(message.document.file_id) if command == '/encfile': downloaded_file = bot.download_file(file_info.file_path) file_name = message.document.file_name ... elif command == '/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 ... else: bot.send_message( chat_id, "Invalid command. Please use /encfile or /decfile to encrypt or decrypt the file." ) - User
give me the complete code
- Assistant