2024-08-19 11:31:06 -05:00
import os
2024-08-19 11:35:29 -05:00
import logging
2024-08-20 12:37:35 -05:00
import sys
2024-08-20 14:11:19 -05:00
import asyncio
2024-08-20 14:21:41 -05:00
import time
2024-08-20 14:28:51 -05:00
import git
2024-08-19 11:31:06 -05:00
from telegram import Update , InlineKeyboardButton , InlineKeyboardMarkup
from telegram . ext import Application , CommandHandler , MessageHandler , filters , ContextTypes , CallbackQueryHandler
2024-08-20 16:52:09 -05:00
from browse_command import browse_command , button_callback
2024-08-19 11:31:06 -05:00
class TelegramHelper :
def __init__ ( self , bot ) :
self . bot = bot
2024-08-19 11:35:29 -05:00
self . telegram_bot_token = os . getenv ( ' TELEGRAM_BOT_TOKEN ' )
2024-08-20 14:46:12 -05:00
self . repo = git . Repo ( " . " )
2024-08-20 14:21:41 -05:00
self . start_time = time . time ( )
2024-08-19 11:31:06 -05:00
async def start ( self , update : Update , context : ContextTypes . DEFAULT_TYPE ) - > None :
await self . bot . start ( )
await update . message . reply_text (
2024-08-19 11:35:29 -05:00
" Hello! I ' m your AI assistant. How can I help you today? "
2024-08-19 11:31:06 -05:00
)
async def clear ( self , update : Update , context : ContextTypes . DEFAULT_TYPE ) - > None :
user_id = update . effective_user . id
await self . bot . clear ( user_id )
2024-08-19 11:35:29 -05:00
await update . message . reply_text ( " Conversation history cleared. Let ' s start fresh! " )
2024-08-19 11:31:06 -05:00
async def status ( self , update : Update , context : ContextTypes . DEFAULT_TYPE ) - > None :
status_message = await self . bot . status ( )
await update . message . reply_text ( status_message )
2024-08-19 11:35:29 -05:00
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. " )
2024-08-19 11:31:06 -05:00
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
)
2024-08-19 11:35:29 -05:00
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 ' ) ] ] ) )
self . bot . processing_status [ user_id ] = { " processing " : True , " message_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 )
del self . bot . processing_status [ user_id ]
await update . message . reply_text ( response )
except Exception as e :
logging . error ( f " An error occurred: { str ( e ) } " )
await update . message . reply_text ( " Sorry, an error occurred while processing your request. " )
async def abort_processing ( self , update : Update , context : ContextTypes . DEFAULT_TYPE ) - > None :
query = update . callback_query
await query . answer ( )
user_id = query . from_user . id
result = await self . bot . abort_processing ( user_id )
await query . edit_message_text ( text = result )
2024-08-20 12:37:35 -05:00
async def reboot ( self , update : Update , context : ContextTypes . DEFAULT_TYPE ) - > None :
2024-08-21 17:07:00 -05:00
user_message = update . message . text . split ( ) # Split the message to check for 'claude'
if len ( user_message ) > 1 and user_message [ 1 ] . lower ( ) == ' claude ' :
open ( ' ./.reboot_claude ' , ' w ' ) . close ( ) # Create an empty file
2024-08-20 14:28:51 -05:00
if update :
await update . message . reply_text ( " Rebooting the bot... " )
2024-08-20 13:11:24 -05:00
logging . info ( " Received reboot command. Exiting process... " )
reboot_file_path = " ./.doreboot "
if not os . path . exists ( reboot_file_path ) :
2024-08-20 13:22:59 -05:00
with open ( reboot_file_path , ' w ' ) as f :
2024-08-20 14:28:51 -05:00
f . write ( str ( update . effective_chat . id ) if update else " " )
2024-08-20 13:11:24 -05:00
sys . exit ( 0 )
2024-08-20 12:37:35 -05:00
2024-08-20 14:09:12 -05:00
async def check_doreboot_file ( self , application : Application ) :
reboot_file_path = " ./.doreboot "
if os . path . exists ( reboot_file_path ) :
with open ( reboot_file_path , ' r ' ) as f :
chat_id = f . read ( ) . strip ( )
2024-08-20 14:28:51 -05:00
if chat_id :
await application . bot . send_message ( chat_id = chat_id , text = " The application has finished initializing. " )
2024-08-20 14:09:12 -05:00
os . remove ( reboot_file_path )
2024-08-20 16:52:09 -05:00
async def browse ( self , update : Update , context : ContextTypes . DEFAULT_TYPE ) - > None :
2024-08-21 13:26:34 -05:00
await browse_command ( update , context , self . bot )
2024-08-20 16:52:09 -05:00
2024-08-19 11:31:06 -05:00
def run ( self ) :
2024-08-19 11:35:29 -05:00
application = Application . builder ( ) . token ( self . telegram_bot_token ) . build ( )
2024-08-19 11:31:06 -05:00
application . add_handler ( CommandHandler ( " start " , self . start ) )
application . add_handler ( CommandHandler ( " clear " , self . clear ) )
2024-08-19 11:35:29 -05:00
application . add_handler ( CommandHandler ( " switch " , self . switch ) )
2024-08-19 11:31:06 -05:00
application . add_handler ( CommandHandler ( " status " , self . status ) )
2024-08-20 12:37:35 -05:00
application . add_handler ( CommandHandler ( " reboot " , self . reboot ) )
2024-08-20 16:52:09 -05:00
application . add_handler ( CommandHandler ( " browse " , self . browse ) )
2024-08-19 11:31:06 -05:00
application . add_handler ( MessageHandler ( filters . TEXT & ~ filters . COMMAND , self . handle_message ) )
application . add_handler ( CallbackQueryHandler ( self . abort_processing , pattern = ' ^abort$ ' ) )
2024-08-20 16:52:09 -05:00
application . add_handler ( CallbackQueryHandler ( button_callback , pattern = ' ^(browse|file): ' ) )
2024-08-20 13:22:59 -05:00
2024-08-19 11:35:29 -05:00
logging . info ( " Bot is running... " )
2024-08-20 13:52:32 -05:00
2024-08-20 14:09:12 -05:00
# Check for .doreboot file and send message if it exists
2024-08-20 14:11:19 -05:00
asyncio . get_event_loop ( ) . create_task ( self . check_doreboot_file ( application ) )
2024-08-20 14:09:12 -05:00
2024-08-21 17:10:20 -05:00
# Commenting out the commit checking task
# asyncio.get_event_loop().create_task(self.check_for_new_commits())
2024-08-20 14:21:41 -05:00
2024-08-20 16:52:09 -05:00
application . run_polling ( )