Merge pull request #125 from bucolucas/feature/browse-command

Implement /browse command for file navigation
This commit is contained in:
2024-08-20 16:54:04 -05:00
committed by GitHub
2 changed files with 52 additions and 1 deletions
+45
View File
@@ -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.")
+7 -1
View File
@@ -6,6 +6,7 @@ import time
import git import git
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler
from browse_command import browse_command, button_callback
class TelegramHelper: class TelegramHelper:
def __init__(self, bot): def __init__(self, bot):
@@ -108,6 +109,9 @@ class TelegramHelper:
logging.error(f"Error checking for new commits: {str(e)}") logging.error(f"Error checking for new commits: {str(e)}")
await asyncio.sleep(60) # Check every 60 seconds await asyncio.sleep(60) # Check every 60 seconds
async def browse(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await browse_command(update, context)
def run(self): def run(self):
application = Application.builder().token(self.telegram_bot_token).build() application = Application.builder().token(self.telegram_bot_token).build()
@@ -116,8 +120,10 @@ class TelegramHelper:
application.add_handler(CommandHandler("switch", self.switch)) application.add_handler(CommandHandler("switch", self.switch))
application.add_handler(CommandHandler("status", self.status)) application.add_handler(CommandHandler("status", self.status))
application.add_handler(CommandHandler("reboot", self.reboot)) application.add_handler(CommandHandler("reboot", self.reboot))
application.add_handler(CommandHandler("browse", self.browse))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self.handle_message)) application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self.handle_message))
application.add_handler(CallbackQueryHandler(self.abort_processing, pattern='^abort$')) application.add_handler(CallbackQueryHandler(self.abort_processing, pattern='^abort$'))
application.add_handler(CallbackQueryHandler(button_callback, pattern='^(browse|file):'))
logging.info("Bot is running...") logging.info("Bot is running...")
@@ -127,4 +133,4 @@ class TelegramHelper:
# Start the commit checking task # Start the commit checking task
asyncio.get_event_loop().create_task(self.check_for_new_commits()) asyncio.get_event_loop().create_task(self.check_for_new_commits())
application.run_polling() application.run_polling()