diff --git a/telegram_inference_bot.py b/telegram_inference_bot.py index a00bdfe..90f28d8 100644 --- a/telegram_inference_bot.py +++ b/telegram_inference_bot.py @@ -4,6 +4,7 @@ import importlib import inspect import tempfile import base64 +import logging from telegram import Update from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes from openai import OpenAI @@ -18,6 +19,12 @@ client = OpenAI() GPT_4O = "gpt-4o" GPT_4O_MINI = "gpt-4o-mini" +# Set up logging to console and file +logging.basicConfig(level=logging.INFO, handlers=[ + logging.StreamHandler(), + logging.FileHandler('logs/output.log', mode='a') +]) + # TODO: ensure log output goes to both console and logs/output.log # Set up Telegram bot @@ -50,6 +57,7 @@ for tool in tools: functions.extend(tool.get_functions()) async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: + logging.info("Bot started") await update.message.reply_text("Hello! I'm your AI assistant. How can I help you today? You can send me images and then ask questions about them.") async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: @@ -59,6 +67,7 @@ async def clear(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: if user_id in user_images: os.remove(user_images[user_id]) del user_images[user_id] + logging.info(f"Cleared conversation history and image for user {user_id}") await update.message.reply_text("Conversation history and image cleared. Let's start fresh!") async def handle_image(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: @@ -75,6 +84,7 @@ async def handle_image(update: Update, context: ContextTypes.DEFAULT_TYPE) -> No await photo_file.download_to_drive(custom_path=temp_file.name) user_images[user_id] = temp_file.name + logging.info(f"Received image from user {user_id}") await update.message.reply_text("I've received your image. What would you like to know about it?") async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: @@ -82,6 +92,8 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> user_id = update.effective_user.id user_message = update.message.text + logging.info(f"Message from user {user_id}: {user_message}") + # Initialize conversation history for new users if user_id not in conversation_history: conversation_history[user_id] = [] @@ -154,7 +166,7 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> await update.message.reply_text(assistant_reply) except Exception as e: - print(f"An error occurred: {str(e)}") + logging.error(f"An error occurred: {str(e)}") await update.message.reply_text("Sorry, an error occurred while processing your request.") def call_tool(function_call, messages): @@ -186,7 +198,7 @@ def main() -> None: application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) # Start the Bot - print("Bot is running...") + logging.info("Bot is running...") application.run_polling() if __name__ == '__main__':