import os import logging import sys import asyncio import time 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: # --- Constants for configurable paths and magic strings --- REBOOT_CLAUDE_FILE = '.reboot_claude' REBOOT_FILE = '.doreboot' CLAUDE_REBOOT_TARGET = 'claude' HTML_QUOTE_BLOCK_START = '
Thinking...' HTML_QUOTE_BLOCK_END = '' def __init__(self, bot): self.bot = bot self.telegram_bot_token = os.getenv('TELEGRAM_BOT_TOKEN') self.start_time = time.time() async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await self.bot.start() await update.message.reply_text( "Hello! I'm your AI assistant. How can I help you today?" ) async def clear(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: user_id = update.effective_user.id await self.bot.clear_conversation_history(user_id) await update.message.reply_text("Conversation history cleared. Let's start fresh!") async def status(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: status_message = await self.bot.get_bot_status() await update.message.reply_text(status_message) async def switch(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: if hasattr(self.bot, 'switch_model'): status_message = await self.bot.switch_model() await update.message.reply_text(status_message) else: await update.message.reply_text("Model switching is not supported for this bot.") async def update_status_message(self, context: ContextTypes.DEFAULT_TYPE, chat_id: int, message_id: int, status: str): keyboard = [ [InlineKeyboardButton("Abort", callback_data='abort')] ] reply_markup = InlineKeyboardMarkup(keyboard) await context.bot.edit_message_text( chat_id=chat_id, message_id=message_id, text=f"Current status: {status}", reply_markup=reply_markup ) async def handle_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: try: user_id = update.effective_user.id user_message = update.message.text logging.info(f"Message from user {user_id}: {user_message}") status_message = await update.message.reply_text("Processing your request...", reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Abort", callback_data='abort')]]))\ await self.bot.set_processing_status(user_id, status_message.message_id) response = await self.bot.handle_message(user_id, user_message) await context.bot.delete_message(chat_id=update.effective_chat.id, message_id=status_message.message_id) await self.bot.clear_processing_status(user_id) response = response.replace("