From 8ee1ae9224149b88766abc0653399b02e661129f Mon Sep 17 00:00:00 2001 From: cyclop-bot <178948048+cyclop-bot@users.noreply.github.com> Date: Tue, 20 Aug 2024 16:49:46 -0500 Subject: [PATCH 1/4] Implement /browse command for file navigation --- browse_command.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 browse_command.py diff --git a/browse_command.py b/browse_command.py new file mode 100644 index 0000000..b77e5f9 --- /dev/null +++ b/browse_command.py @@ -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.") \ No newline at end of file From 9f8418b47de67a8a0fff0bd9dbce334c84d3093f Mon Sep 17 00:00:00 2001 From: cyclop-bot <178948048+cyclop-bot@users.noreply.github.com> Date: Tue, 20 Aug 2024 16:50:05 -0500 Subject: [PATCH 2/4] Add /browse command to AnthropicTelegramInferenceBot --- anthropic_telegram_inference_bot.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/anthropic_telegram_inference_bot.py b/anthropic_telegram_inference_bot.py index 2c39863..e061c1d 100644 --- a/anthropic_telegram_inference_bot.py +++ b/anthropic_telegram_inference_bot.py @@ -4,6 +4,7 @@ import logging from anthropic import Anthropic from base_telegram_inference_bot import BaseTelegramInferenceBot from telegram_helper import TelegramHelper +from browse_command import browse_command, button_callback class AnthropicTelegramInferenceBot(BaseTelegramInferenceBot): def __init__(self): @@ -101,9 +102,19 @@ class AnthropicTelegramInferenceBot(BaseTelegramInferenceBot): else: return "No active processing to abort." + async def browse(self, update, context): + await browse_command(update, context) + def main(): bot = AnthropicTelegramInferenceBot() telegram_helper = TelegramHelper(bot) + + # Add the /browse command handler + telegram_helper.add_command_handler('browse', bot.browse) + + # Add the button callback handler + telegram_helper.add_callback_query_handler(button_callback) + telegram_helper.run() if __name__ == '__main__': From 80f2fc4fedf665a6c3b88ecdcf78af446a95d6c1 Mon Sep 17 00:00:00 2001 From: cyclop-bot <178948048+cyclop-bot@users.noreply.github.com> Date: Tue, 20 Aug 2024 16:51:44 -0500 Subject: [PATCH 3/4] Revert changes to anthropic_telegram_inference_bot.py --- anthropic_telegram_inference_bot.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/anthropic_telegram_inference_bot.py b/anthropic_telegram_inference_bot.py index e061c1d..2c39863 100644 --- a/anthropic_telegram_inference_bot.py +++ b/anthropic_telegram_inference_bot.py @@ -4,7 +4,6 @@ import logging from anthropic import Anthropic from base_telegram_inference_bot import BaseTelegramInferenceBot from telegram_helper import TelegramHelper -from browse_command import browse_command, button_callback class AnthropicTelegramInferenceBot(BaseTelegramInferenceBot): def __init__(self): @@ -102,19 +101,9 @@ class AnthropicTelegramInferenceBot(BaseTelegramInferenceBot): else: return "No active processing to abort." - async def browse(self, update, context): - await browse_command(update, context) - def main(): bot = AnthropicTelegramInferenceBot() telegram_helper = TelegramHelper(bot) - - # Add the /browse command handler - telegram_helper.add_command_handler('browse', bot.browse) - - # Add the button callback handler - telegram_helper.add_callback_query_handler(button_callback) - telegram_helper.run() if __name__ == '__main__': From c52dd6a3a1a758b18106d67ca72ab2ed8cbe7d17 Mon Sep 17 00:00:00 2001 From: cyclop-bot <178948048+cyclop-bot@users.noreply.github.com> Date: Tue, 20 Aug 2024 16:52:09 -0500 Subject: [PATCH 4/4] Add /browse command and button callback handlers to TelegramHelper --- telegram_helper.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/telegram_helper.py b/telegram_helper.py index a41eaf3..86bdb71 100644 --- a/telegram_helper.py +++ b/telegram_helper.py @@ -6,6 +6,7 @@ import time import git from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, CallbackQueryHandler +from browse_command import browse_command, button_callback class TelegramHelper: def __init__(self, bot): @@ -108,6 +109,9 @@ class TelegramHelper: logging.error(f"Error checking for new commits: {str(e)}") 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): 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("status", self.status)) 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(CallbackQueryHandler(self.abort_processing, pattern='^abort$')) + application.add_handler(CallbackQueryHandler(button_callback, pattern='^(browse|file):')) logging.info("Bot is running...") @@ -127,4 +133,4 @@ class TelegramHelper: # Start the commit checking task asyncio.get_event_loop().create_task(self.check_for_new_commits()) - application.run_polling() + application.run_polling() \ No newline at end of file