Files
cyclop/browse_command.py
T

50 lines
2.2 KiB
Python

import os
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import ContextTypes
async def browse_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
prompts_dir = "prompts"
try:
# Get all items in the prompts directory
items = os.listdir(prompts_dir)
# Separate and sort subdirectories and files
subdirs = sorted([item for item in items if os.path.isdir(os.path.join(prompts_dir, item))])
files = sorted([item for item in items if os.path.isfile(os.path.join(prompts_dir, item))])
# Create keyboard layout
keyboard = []
for subdir in subdirs:
keyboard.append([InlineKeyboardButton(f"[{subdir}]", callback_data=f"browse:{subdir}")])
for file in files:
keyboard.append([InlineKeyboardButton(file, callback_data=f"file:{file}")])
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text("Browse files and directories:", reply_markup=reply_markup)
except FileNotFoundError:
await update.message.reply_text("The 'prompts' directory does not exist.")
except Exception as e:
await update.message.reply_text(f"An error occurred: {str(e)}")
# This function will need to be called when a button is pressed
async def button_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
query = update.callback_query
await query.answer()
data = query.data
if data.startswith("browse:"):
# Handle subdirectory navigation (for future implementation)
subdir = data.split(":")[1]
await query.edit_message_text(f"Navigating to [{subdir}] is not yet implemented.")
elif data.startswith("file:"):
# Handle file selection
file = data.split(":")[1]
file_path = os.path.join('prompts', file)
os.environ['SYSTEM_PROMPT_PATH'] = file_path
await query.edit_message_text(f"Selected: {file}. SYSTEM_PROMPT_PATH updated.")
# Optionally, you may want to reload the system prompt here if needed.
# bot.system_prompt = bot.load_system_prompt() # Uncomment if you have access to the bot instance.