import asyncio import os import logging from typing import List from dotenv import load_dotenv import chromadb from aiogram import Bot, Dispatcher from src.crawlers.base import ICrawler from src.crawlers.rss_crawler import RSSCrawler from src.processor.ollama_provider import OllamaProvider from src.storage.chroma_store import ChromaStore from src.notifications.telegram import TelegramNotifier from src.orchestrator.service import TrendScoutService from src.bot.bot import setup_bot logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s") logger = logging.getLogger(__name__) async def background_task(orchestrator: TrendScoutService, interval: int = 3600): """Run the orchestrator periodically.""" while True: logger.info("Starting new TrendScout iteration...") try: await orchestrator.run_iteration() logger.info("Iteration completed successfully.") except Exception as e: logger.error(f"Error during iteration: {e}", exc_info=True) logger.info(f"Sleeping for {interval} seconds before next iteration.") await asyncio.sleep(interval) async def main(): load_dotenv() # Load configuration bot_token = os.getenv("TELEGRAM_BOT_TOKEN") chat_id = os.getenv("TELEGRAM_CHAT_ID", "") ollama_url = os.getenv("OLLAMA_API_URL", "http://localhost:11434/api/generate") chroma_db_path = os.getenv("CHROMA_DB_PATH", "./chroma_db") if not bot_token: logger.error("TELEGRAM_BOT_TOKEN is missing!") return if not chat_id or chat_id == "YOUR_CHAT_ID_HERE": logger.warning("TELEGRAM_CHAT_ID is missing or not set. Notifications will fail.") # 1. Initialize Components that do not depend on Bot crawlers: List[ICrawler] = [ RSSCrawler("https://habr.com/ru/rss/hubs/artificial_intelligence/articles/?fl=ru", source="Habr AI") ] processor = OllamaProvider() if chroma_db_path: chroma_client = chromadb.PersistentClient(path=chroma_db_path) else: chroma_client = chromadb.Client() storage = ChromaStore(client=chroma_client) # 2. Initialize Bot & Dispatcher bot, dp = setup_bot(bot_token, storage, chat_id) # 3. Initialize Notifier and Orchestrator notifier = TelegramNotifier(bot, chat_id) orchestrator = TrendScoutService( crawlers=crawlers, processor=processor, storage=storage, notifier=notifier ) # 4. Start tasks logger.info("Starting TrendScout AI Bot and Background Task...") # Create the background task bg_task = asyncio.create_task(background_task(orchestrator, interval=3600)) # Start polling the Telegram bot (blocking call) try: await dp.start_polling(bot) finally: bg_task.cancel() logger.info("Shutting down...") if __name__ == "__main__": asyncio.run(main())