The project includes a production-ready Dockerfile:
Dockerfile
# Choose Python 3.8 or higher base imageFROM python:3.8-slim# Set the working directory inside the containerWORKDIR /app# Copy the requirements.txt file into the containerCOPY requirements.txt /app/# Copy the .env.example file into the containerCOPY .env.example /app/.env# Install the project dependenciesRUN pip install --no-cache-dir -r requirements.txt# Copy your project code into the containerCOPY . /app/# Define the command to run your programCMD ["python", "bot.py"]
The bot performs critical initialization before accepting messages:
bot.py:116-128
async def initialize() -> bool: """Initialize AI models and other components.""" try: logger.info("Initializing AI models...") success = await ai_handler.initialize_models() if not success: logger.error("Failed to initialize AI models") return False logger.info("AI models initialized successfully") return True except Exception as e: logger.error(f"Error during initialization: {str(e)}") return False
If model initialization fails, the bot will exit. Ensure you have sufficient memory and disk space for the models.
The bot’s main function handles startup and error handling:
bot.py:130-174
def main() -> None: """Start the bot.""" # Get the token from environment variable token = os.getenv("TELEGRAM_BOT_TOKEN") if not token: logger.error("No token found! Make sure to set TELEGRAM_BOT_TOKEN in .env file") return # Create the Application and initialize models application = Application.builder().token(token).build() # Initialize models before starting the bot loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) try: # Run initialization in the event loop init_success = loop.run_until_complete(initialize()) if not init_success: logger.error("Failed to initialize AI models. Exiting...") return logger.info("AI models initialized successfully") # Add handlers application.add_handler(CommandHandler("start", start)) application.add_handler(CommandHandler("help", help_command)) application.add_handler(CommandHandler("about", about_command)) application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)) # Log startup logger.info("MilesONerd AI Bot is starting...") # Run the bot application.run_polling(allowed_updates=Update.ALL_TYPES) finally: loop.close()if __name__ == '__main__': try: main() except KeyboardInterrupt: logger.info("Bot stopped by user") except Exception as e: logger.error(f"Error running bot: {str(e)}") raise # Re-raise the exception for proper error handling