Implement /browse command for file navigation
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
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 (for future implementation)
|
||||
file = data.split(":")[1]
|
||||
await query.edit_message_text(f"Viewing contents of {file} is not yet implemented.")
|
||||
Reference in New Issue
Block a user