-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
99 lines (90 loc) · 3.48 KB
/
Copy pathmain.py
File metadata and controls
99 lines (90 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import asyncio
import logging
from pathlib import Path
from aiogram import Bot, Dispatcher
from aiogram.fsm.storage.memory import MemoryStorage
from app.handlers.voice import create_voice_router
from app.handlers.start import create_start_router
from app.handlers.settings import create_settings_router
from app.handlers.delete import create_delete_router
from app.logging_setup import setup_logging
from app.scheduler import scheduler_loop
from app.services.bot_settings_service import BotSettingsService
from app.services.delete_service import DeleteService
from app.services.intent_service import IntentService
from app.services.openai_service import OpenAIService
from app.services.qa_service import QAService
from app.services.router_service import RouterService
from app.services.sheets_service import SheetsService
from app.services.summary_service import SummaryService
from config import Config
async def main() -> None:
setup_logging()
config = Config.from_env()
logger = logging.getLogger(__name__)
base_dir = Path(__file__).resolve().parent
service_account_path = base_dir / "service_account.json"
if not service_account_path.exists():
fallback_path = base_dir / "service_account.json.json"
if fallback_path.exists():
logger.warning("service_account.json not found, using %s", fallback_path.name)
service_account_path = fallback_path
else:
raise FileNotFoundError(
f"Service account key not found in {service_account_path} or {fallback_path}"
)
openai_service = OpenAIService(api_key=config.openai_api_key)
sheets_service = await SheetsService.create(
spreadsheet_id=config.google_sheet_id,
service_account_path=service_account_path,
)
await sheets_service.ensure_worksheet("Inbox", rows=1000, cols=10)
router_service = RouterService(openai_service)
intent_service = IntentService(openai_service)
settings_service = BotSettingsService(base_dir / "data" / "settings.json")
qa_service = QAService(openai_service, sheets_service)
delete_service = DeleteService(sheets_service)
summary_service = SummaryService(openai_service, sheets_service)
bot = Bot(token=config.telegram_token)
dp = Dispatcher(storage=MemoryStorage())
dp.include_router(
create_voice_router(
openai_service,
sheets_service,
router_service,
intent_service,
settings_service,
qa_service,
delete_service,
allowed_user_ids=config.allowed_user_ids,
allowed_usernames=config.allowed_usernames,
)
)
dp.include_router(
create_start_router(
settings_service,
allowed_user_ids=config.allowed_user_ids,
allowed_usernames=config.allowed_usernames,
)
)
dp.include_router(
create_delete_router(
delete_service,
allowed_user_ids=config.allowed_user_ids,
allowed_usernames=config.allowed_usernames,
)
)
dp.include_router(
create_settings_router(
sheets_service,
settings_service,
summary_service,
allowed_user_ids=config.allowed_user_ids,
allowed_usernames=config.allowed_usernames,
)
)
asyncio.create_task(scheduler_loop(bot, settings_service, summary_service))
logger.info("Bot started")
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())