-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_validation.py
58 lines (48 loc) · 1.88 KB
/
env_validation.py
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
from typing import NamedTuple, List
from dotenv import load_dotenv
import os
from datetime import timedelta
class EnvConfig(NamedTuple):
telegram_token: str
openai_api_key: str
firecrawl_api_key: str
message_limit: int
time_window_hours: int
db_path: str = 'chatzzipper.db'
allowed_chat_ids: List[int] = []
@property
def time_window(self) -> timedelta:
return timedelta(hours=self.time_window_hours)
def validate_env() -> EnvConfig:
load_dotenv()
telegram_token = os.getenv('TELEGRAM_BOT_TOKEN')
if not telegram_token:
raise ValueError("TELEGRAM_BOT_TOKEN is required")
openai_api_key = os.getenv('OPENAI_API_KEY')
if not openai_api_key:
raise ValueError("OPENAI_API_KEY is required")
firecrawl_api_key = os.getenv('FIRECRAWL_API_KEY')
if not firecrawl_api_key:
raise ValueError("FIRECRAWL_API_KEY is required")
try:
message_limit = int(os.getenv('MESSAGE_LIMIT', '75'))
if message_limit <= 0:
raise ValueError("MESSAGE_LIMIT must be positive")
except ValueError:
raise ValueError("MESSAGE_LIMIT must be a valid integer")
try:
time_window_hours = int(os.getenv('TIME_WINDOW_HOURS', '24'))
if time_window_hours <= 0:
raise ValueError("TIME_WINDOW_HOURS must be positive")
except ValueError:
raise ValueError("TIME_WINDOW_HOURS must be a valid integer")
allowed_chat_ids_str = os.getenv('ALLOWED_CHAT_IDS', '')
allowed_chat_ids = [int(id.strip()) for id in allowed_chat_ids_str.split(',')] if allowed_chat_ids_str else []
return EnvConfig(
telegram_token=telegram_token,
openai_api_key=openai_api_key,
firecrawl_api_key=firecrawl_api_key,
message_limit=message_limit,
time_window_hours=time_window_hours,
allowed_chat_ids=allowed_chat_ids
)