feat: Implement new bot architecture including handlers, services, analysis modules, and comprehensive tests.
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
"""Bot service layer."""
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import List, Optional
|
||||
|
||||
from src.bot.analysis.city_analysis_service import CityAnalysisService
|
||||
|
||||
|
||||
@dataclass
|
||||
class CityResolveResult:
|
||||
ok: bool
|
||||
city_name: Optional[str] = None
|
||||
supported_cities: List[str] | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class CityReportResult:
|
||||
ok: bool
|
||||
report: Optional[str] = None
|
||||
error: Optional[str] = None
|
||||
|
||||
|
||||
class CityCommandService:
|
||||
def __init__(self, analysis: CityAnalysisService):
|
||||
self.analysis = analysis
|
||||
|
||||
def resolve_city(self, city_input: str) -> CityResolveResult:
|
||||
city_name, supported = self.analysis.resolve_city(city_input)
|
||||
if not city_name:
|
||||
return CityResolveResult(ok=False, supported_cities=supported)
|
||||
return CityResolveResult(ok=True, city_name=city_name, supported_cities=supported)
|
||||
|
||||
def build_report(self, city_name: str, city_query_cost: int) -> CityReportResult:
|
||||
try:
|
||||
report = self.analysis.build_city_report(city_name, city_query_cost)
|
||||
return CityReportResult(ok=True, report=report)
|
||||
except Exception as exc:
|
||||
return CityReportResult(ok=False, error=str(exc))
|
||||
@@ -0,0 +1,31 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
from src.bot.analysis.deb_analysis_service import DebAnalysisService
|
||||
|
||||
|
||||
@dataclass
|
||||
class DebReportResult:
|
||||
ok: bool
|
||||
report: Optional[str] = None
|
||||
error: Optional[str] = None
|
||||
|
||||
|
||||
class DebCommandService:
|
||||
def __init__(self, analysis: DebAnalysisService):
|
||||
self.analysis = analysis
|
||||
|
||||
def resolve_city(self, city_input: str) -> str:
|
||||
return self.analysis.resolve_deb_city(city_input)
|
||||
|
||||
def has_history(self, city_name: str) -> bool:
|
||||
return self.analysis.has_deb_history(city_name)
|
||||
|
||||
def build_report(self, city_name: str, deb_query_cost: int) -> DebReportResult:
|
||||
try:
|
||||
report = self.analysis.build_deb_accuracy_report(city_name, deb_query_cost)
|
||||
return DebReportResult(ok=True, report=report)
|
||||
except Exception as exc:
|
||||
return DebReportResult(ok=False, error=str(exc))
|
||||
@@ -0,0 +1,54 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from typing import Iterable, Set
|
||||
|
||||
from src.database.db_manager import DBManager
|
||||
|
||||
|
||||
def _env_bool(name: str, default: bool = False) -> bool:
|
||||
raw = str(os.getenv(name, "")).strip().lower()
|
||||
if not raw:
|
||||
return default
|
||||
return raw in {"1", "true", "yes", "on"}
|
||||
|
||||
|
||||
@dataclass
|
||||
class EntitlementDecision:
|
||||
allowed: bool
|
||||
reason: str
|
||||
|
||||
|
||||
class BotEntitlementService:
|
||||
"""
|
||||
Payment/entitlement pre-hook for command access.
|
||||
|
||||
Disabled by default. Enable with:
|
||||
POLYWEATHER_BOT_REQUIRE_ENTITLEMENT=true
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
db: DBManager,
|
||||
enabled: bool | None = None,
|
||||
protected_commands: Iterable[str] | None = None,
|
||||
):
|
||||
self.db = db
|
||||
self.enabled = _env_bool("POLYWEATHER_BOT_REQUIRE_ENTITLEMENT", False) if enabled is None else enabled
|
||||
commands = protected_commands or ("/city", "/deb")
|
||||
self.protected_commands: Set[str] = {str(c).strip().lower() for c in commands if str(c).strip()}
|
||||
|
||||
def check(self, user_id: int, command_label: str) -> EntitlementDecision:
|
||||
command = str(command_label or "").strip().lower()
|
||||
if not self.enabled:
|
||||
return EntitlementDecision(True, "entitlement_disabled")
|
||||
if command not in self.protected_commands:
|
||||
return EntitlementDecision(True, "command_not_protected")
|
||||
|
||||
user = self.db.get_user(user_id) or {}
|
||||
has_premium = bool(user.get("is_web_premium") or user.get("is_group_premium"))
|
||||
if has_premium:
|
||||
return EntitlementDecision(True, "premium_user")
|
||||
return EntitlementDecision(False, "premium_required")
|
||||
|
||||
Reference in New Issue
Block a user