Refactor: Make reboot file paths configurable in TelegramHelper

This commit is contained in:
cyclop-bot
2025-06-02 16:35:52 -05:00
parent 75381f5f11
commit 53d1ec8923
+18 -12
View File
@@ -8,17 +8,20 @@ from telegram.ext import Application, CommandHandler, MessageHandler, filters, C
from browse_command import browse_command, button_callback from browse_command import browse_command, button_callback
class TelegramHelper: class TelegramHelper:
# --- Constants for configurable paths and magic strings --- # --- Constants for magic strings (paths are now instance vars) ---
REBOOT_CLAUDE_FILE = '.reboot_claude'
REBOOT_FILE = '.doreboot'
CLAUDE_REBOOT_TARGET = 'claude' CLAUDE_REBOOT_TARGET = 'claude'
HTML_QUOTE_BLOCK_START = '<blockquote expandable><b>Thinking...</b>' HTML_QUOTE_BLOCK_START = '<blockquote expandable><b>Thinking...</b>'
HTML_QUOTE_BLOCK_END = '</blockquote>' HTML_QUOTE_BLOCK_END = '</blockquote>'
DEFAULT_REBOOT_CLAUDE_FILE = '.reboot_claude' # Default value
DEFAULT_REBOOT_FILE = '.doreboot' # Default value
def __init__(self, bot): def __init__(self, bot, reboot_claude_file_path: str | None = None, reboot_file_path: str | None = None): # MODIFIED
self.bot = bot self.bot = bot
self.telegram_bot_token = os.getenv('TELEGRAM_BOT_TOKEN') self.telegram_bot_token = os.getenv('TELEGRAM_BOT_TOKEN')
self.start_time = time.time() self.start_time = time.time()
# MODIFIED: Store configurable paths
self.reboot_claude_file = reboot_claude_file_path or self.DEFAULT_REBOOT_CLAUDE_FILE
self.reboot_file = reboot_file_path or self.DEFAULT_REBOOT_FILE
async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await self.bot.start() await self.bot.start()
@@ -94,25 +97,28 @@ class TelegramHelper:
async def reboot(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: async def reboot(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
user_message = update.message.text.split() user_message = update.message.text.split()
if len(user_message) > 1 and user_message[1].lower() == self.CLAUDE_REBOOT_TARGET: if len(user_message) > 1 and user_message[1].lower() == self.CLAUDE_REBOOT_TARGET:
open(self.REBOOT_CLAUDE_FILE, 'w').close() # MODIFIED: Use instance variable
open(self.reboot_claude_file, 'w').close()
if update: if update:
await update.message.reply_text("Rebooting the bot...") await update.message.reply_text("Rebooting the bot...")
logging.info("Received reboot command. Exiting process...") logging.info("Received reboot command. Exiting process...")
reboot_file_path = self.REBOOT_FILE # MODIFIED: Use instance variable
if not os.path.exists(reboot_file_path): reboot_f_path = self.reboot_file
with open(reboot_file_path, 'w') as f: if not os.path.exists(reboot_f_path):
with open(reboot_f_path, 'w') as f:
f.write(str(update.effective_chat.id) if update else "") f.write(str(update.effective_chat.id) if update else "")
sys.exit(0) sys.exit(0)
async def check_doreboot_file(self, application: Application): async def check_doreboot_file(self, application: Application):
reboot_file_path = self.REBOOT_FILE # MODIFIED: Use instance variable
if os.path.exists(reboot_file_path): reboot_f_path = self.reboot_file
with open(reboot_file_path, 'r') as f: if os.path.exists(reboot_f_path):
with open(reboot_f_path, 'r') as f:
chat_id = f.read().strip() chat_id = f.read().strip()
if chat_id: if chat_id:
await application.bot.send_message(chat_id=chat_id, text="The application has finished initializing.") await application.bot.send_message(chat_id=chat_id, text="The application has finished initializing.")
os.remove(reboot_file_path) os.remove(reboot_f_path)
async def browse(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: async def browse(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await browse_command(update, context, self.bot) await browse_command(update, context, self.bot)