feat: mirror subscription lifecycle notifications
This commit is contained in:
@@ -3,12 +3,15 @@ import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Optional
|
||||
|
||||
from aiogram import Bot
|
||||
from aiogram.types import InlineKeyboardMarkup
|
||||
from aiohttp import web
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from sqlalchemy import or_, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import selectinload, sessionmaker
|
||||
|
||||
from bot.infra.webhook_queue import enqueue_webhook_event
|
||||
from bot.keyboards.inline.user_keyboards import (
|
||||
@@ -16,17 +19,32 @@ from bot.keyboards.inline.user_keyboards import (
|
||||
get_subscribe_only_markup,
|
||||
)
|
||||
from bot.middlewares.i18n import JsonI18n
|
||||
from bot.services.subscription_lifecycle_notifications import (
|
||||
SubscriptionLifecycleNotificationService,
|
||||
SubscriptionNotificationStage,
|
||||
)
|
||||
from config.settings import Settings
|
||||
from db.dal import tariff_dal, user_dal
|
||||
from db.dal import subscription_dal, tariff_dal, user_dal
|
||||
from db.models import Subscription, User
|
||||
|
||||
from .email_auth_service import EmailAuthService
|
||||
from .email_templates import render_subscription_expiring
|
||||
from .panel_api_service import PanelApiService
|
||||
|
||||
EVENT_MAP = {
|
||||
"user.expires_in_72_hours": (3, "subscription_72h_notification"),
|
||||
"user.expires_in_48_hours": (2, "subscription_48h_notification"),
|
||||
"user.expires_in_24_hours": (1, "subscription_24h_notification"),
|
||||
"user.expires_in_72_hours": SubscriptionNotificationStage(
|
||||
key="before_3d",
|
||||
message_key="subscription_72h_notification",
|
||||
days_left=3,
|
||||
),
|
||||
"user.expires_in_48_hours": SubscriptionNotificationStage(
|
||||
key="before_2d",
|
||||
message_key="subscription_48h_notification",
|
||||
days_left=2,
|
||||
),
|
||||
"user.expires_in_24_hours": SubscriptionNotificationStage(
|
||||
key="before_1d",
|
||||
message_key="subscription_24h_notification",
|
||||
days_left=1,
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +66,11 @@ class PanelWebhookService:
|
||||
self.i18n = i18n
|
||||
self.async_session_factory = async_session_factory
|
||||
self.panel_service = panel_service
|
||||
self.lifecycle_notifications = SubscriptionLifecycleNotificationService(
|
||||
settings,
|
||||
bot,
|
||||
i18n,
|
||||
)
|
||||
self._event_semaphore = asyncio.Semaphore(self._MAX_CONCURRENT_EVENTS)
|
||||
if not self.settings.PANEL_WEBHOOK_SECRET:
|
||||
logging.error(
|
||||
@@ -102,113 +125,182 @@ class PanelWebhookService:
|
||||
)
|
||||
|
||||
async def handle_event(self, event_name: str, user_payload: dict):
|
||||
telegram_id = user_payload.get("telegramId")
|
||||
if not telegram_id:
|
||||
logging.warning("Panel webhook without telegramId received")
|
||||
return
|
||||
user_id = int(telegram_id)
|
||||
|
||||
if not self.settings.SUBSCRIPTION_NOTIFICATIONS_ENABLED:
|
||||
return
|
||||
|
||||
async with self.async_session_factory() as session:
|
||||
db_user = await user_dal.get_user_by_telegram_id(session, user_id)
|
||||
if not db_user:
|
||||
db_user = await user_dal.get_user_by_id(session, user_id)
|
||||
internal_user_id = db_user.user_id if db_user else user_id
|
||||
db_user = await self._user_for_payload(session, user_payload)
|
||||
sub = await self._subscription_for_payload(session, user_payload, db_user)
|
||||
telegram_id = self._payload_telegram_id(user_payload)
|
||||
internal_user_id = (
|
||||
int(db_user.user_id)
|
||||
if db_user
|
||||
else int(getattr(sub, "user_id", 0) or telegram_id or 0)
|
||||
)
|
||||
lang = (
|
||||
db_user.language_code
|
||||
if db_user and db_user.language_code
|
||||
else self.settings.DEFAULT_LANGUAGE
|
||||
)
|
||||
first_name = db_user.first_name or f"User {user_id}" if db_user else f"User {user_id}"
|
||||
user_email = (db_user.email or "").strip() if db_user else ""
|
||||
if not sub:
|
||||
if not telegram_id:
|
||||
logging.warning("Panel webhook event %s has no local subscription", event_name)
|
||||
return
|
||||
await self._send_legacy_without_dedupe(
|
||||
event_name,
|
||||
user_payload,
|
||||
int(telegram_id),
|
||||
lang,
|
||||
db_user,
|
||||
)
|
||||
return
|
||||
|
||||
markup = get_subscribe_only_markup(lang, self.i18n)
|
||||
markup = get_subscribe_only_markup(lang, self.i18n)
|
||||
end_date_text = self._payload_expire_date(user_payload)
|
||||
|
||||
if event_name in EVENT_MAP:
|
||||
days_left, msg_key = EVENT_MAP[event_name]
|
||||
hwid_renewal_note = await self._hwid_renewal_note(internal_user_id, lang)
|
||||
if days_left == 1:
|
||||
# Trigger auto-renew via SubscriptionService (wired in at factory)
|
||||
try:
|
||||
subscription_service = getattr(self, "subscription_service", None)
|
||||
if subscription_service:
|
||||
async with self.async_session_factory() as session:
|
||||
from db.dal import subscription_dal
|
||||
|
||||
sub = await subscription_dal.get_active_subscription_by_user_id(
|
||||
session, internal_user_id
|
||||
)
|
||||
if sub and sub.auto_renew_enabled and sub.provider == "yookassa":
|
||||
try:
|
||||
ok = await subscription_service.charge_subscription_renewal(
|
||||
session, sub
|
||||
if event_name in EVENT_MAP:
|
||||
stage = EVENT_MAP[event_name]
|
||||
days_left = int(stage.days_left or 0)
|
||||
hwid_renewal_note = await self._hwid_renewal_note(internal_user_id, lang)
|
||||
if days_left == 1:
|
||||
# Trigger auto-renew via SubscriptionService (wired in at factory)
|
||||
try:
|
||||
subscription_service = getattr(self, "subscription_service", None)
|
||||
if subscription_service:
|
||||
async with self.async_session_factory() as renewal_session:
|
||||
active_sub = (
|
||||
await subscription_dal.get_active_subscription_by_user_id(
|
||||
renewal_session,
|
||||
internal_user_id,
|
||||
)
|
||||
# If initiation succeeded, suppress the 24h reminder by returning early # noqa: E501
|
||||
if ok:
|
||||
await session.commit()
|
||||
return
|
||||
else:
|
||||
await session.rollback()
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
logging.exception("Auto-renew attempt (24h) failed")
|
||||
except Exception:
|
||||
logging.exception("Auto-renew trigger (24h) failed pre-check")
|
||||
if days_left <= self.settings.SUBSCRIPTION_NOTIFY_DAYS_BEFORE:
|
||||
# For 48h event, if auto-renew is enabled, show special notice with cancel button
|
||||
if days_left == 2:
|
||||
async with self.async_session_factory() as session:
|
||||
from db.dal import subscription_dal
|
||||
|
||||
sub = await subscription_dal.get_active_subscription_by_user_id(
|
||||
session, internal_user_id
|
||||
)
|
||||
if (
|
||||
active_sub
|
||||
and active_sub.auto_renew_enabled
|
||||
and active_sub.provider == "yookassa"
|
||||
):
|
||||
try:
|
||||
ok = await subscription_service.charge_subscription_renewal(
|
||||
renewal_session,
|
||||
active_sub,
|
||||
)
|
||||
# If initiation succeeded, suppress the 24h reminder by returning early # noqa: E501
|
||||
if ok:
|
||||
await renewal_session.commit()
|
||||
return
|
||||
await renewal_session.rollback()
|
||||
except Exception:
|
||||
await renewal_session.rollback()
|
||||
logging.exception("Auto-renew attempt (24h) failed")
|
||||
except Exception:
|
||||
logging.exception("Auto-renew trigger (24h) failed pre-check")
|
||||
if days_left <= self.settings.SUBSCRIPTION_NOTIFY_DAYS_BEFORE:
|
||||
# For 48h, auto-renew users get a cancel button instead.
|
||||
if days_left == 2:
|
||||
active_sub = await subscription_dal.get_active_subscription_by_user_id(
|
||||
session,
|
||||
internal_user_id,
|
||||
)
|
||||
logging.info(
|
||||
"48h webhook check: user_id=%s sub_found=%s auto_renew=%s provider=%s",
|
||||
user_id,
|
||||
bool(sub),
|
||||
getattr(sub, "auto_renew_enabled", None) if sub else None,
|
||||
getattr(sub, "provider", None) if sub else None,
|
||||
internal_user_id,
|
||||
bool(active_sub),
|
||||
getattr(active_sub, "auto_renew_enabled", None) if active_sub else None,
|
||||
getattr(active_sub, "provider", None) if active_sub else None,
|
||||
)
|
||||
if sub and sub.auto_renew_enabled and sub.provider == "yookassa":
|
||||
if (
|
||||
active_sub
|
||||
and active_sub.auto_renew_enabled
|
||||
and active_sub.provider == "yookassa"
|
||||
):
|
||||
cancel_kb = get_autorenew_cancel_keyboard(lang, self.i18n)
|
||||
await self._send_message(
|
||||
user_id,
|
||||
lang,
|
||||
"autorenew_48h_charge_tomorrow_notice",
|
||||
reply_markup=cancel_kb,
|
||||
user_name=first_name,
|
||||
await self.lifecycle_notifications.send_stage(
|
||||
session,
|
||||
sub,
|
||||
SubscriptionNotificationStage(
|
||||
key="before_2d_autorenew",
|
||||
message_key="autorenew_48h_charge_tomorrow_notice",
|
||||
days_left=2,
|
||||
),
|
||||
user=db_user,
|
||||
telegram_markup=cancel_kb,
|
||||
extra_text=hwid_renewal_note,
|
||||
end_date_text=end_date_text,
|
||||
)
|
||||
await session.commit()
|
||||
return
|
||||
await self._send_message(
|
||||
user_id,
|
||||
lang,
|
||||
msg_key,
|
||||
reply_markup=markup,
|
||||
user_name=first_name,
|
||||
end_date=user_payload.get("expireAt", "")[:10],
|
||||
extra_text=hwid_renewal_note,
|
||||
)
|
||||
if days_left == 3 and user_email:
|
||||
await self._send_subscription_expiring_email(
|
||||
recipient=user_email,
|
||||
lang=lang,
|
||||
days_left=days_left,
|
||||
end_date_text=user_payload.get("expireAt", "")[:10],
|
||||
await self.lifecycle_notifications.send_stage(
|
||||
session,
|
||||
sub,
|
||||
stage,
|
||||
user=db_user,
|
||||
telegram_markup=markup,
|
||||
extra_text=hwid_renewal_note,
|
||||
end_date_text=end_date_text,
|
||||
)
|
||||
elif event_name == "user.expired":
|
||||
if self.settings.SUBSCRIPTION_NOTIFY_ON_EXPIRE:
|
||||
await self._send_message(
|
||||
user_id,
|
||||
lang,
|
||||
"subscription_expired_notification",
|
||||
reply_markup=markup,
|
||||
user_name=first_name,
|
||||
end_date=user_payload.get("expireAt", "")[:10],
|
||||
await session.commit()
|
||||
elif event_name == "user.expired":
|
||||
if self.settings.SUBSCRIPTION_NOTIFY_ON_EXPIRE:
|
||||
await self.lifecycle_notifications.send_stage(
|
||||
session,
|
||||
sub,
|
||||
SubscriptionNotificationStage(
|
||||
key="expired",
|
||||
message_key="subscription_expired_notification",
|
||||
days_left=0,
|
||||
),
|
||||
user=db_user,
|
||||
telegram_markup=markup,
|
||||
end_date_text=end_date_text,
|
||||
)
|
||||
await session.commit()
|
||||
elif (
|
||||
event_name == "user.expired_24_hours_ago"
|
||||
and self.settings.SUBSCRIPTION_NOTIFY_AFTER_EXPIRE
|
||||
):
|
||||
await self.lifecycle_notifications.send_stage(
|
||||
session,
|
||||
sub,
|
||||
SubscriptionNotificationStage(
|
||||
key="expired_24h_after",
|
||||
message_key="subscription_expired_yesterday_notification",
|
||||
days_left=0,
|
||||
),
|
||||
user=db_user,
|
||||
telegram_markup=markup,
|
||||
end_date_text=end_date_text,
|
||||
)
|
||||
await session.commit()
|
||||
|
||||
async def _send_legacy_without_dedupe(
|
||||
self,
|
||||
event_name: str,
|
||||
user_payload: dict,
|
||||
user_id: int,
|
||||
lang: str,
|
||||
db_user: Optional[User],
|
||||
) -> None:
|
||||
first_name = getattr(db_user, "first_name", None) or f"User {user_id}"
|
||||
markup = get_subscribe_only_markup(lang, self.i18n)
|
||||
if event_name in EVENT_MAP:
|
||||
stage = EVENT_MAP[event_name]
|
||||
await self._send_message(
|
||||
user_id,
|
||||
lang,
|
||||
stage.message_key,
|
||||
reply_markup=markup,
|
||||
user_name=first_name,
|
||||
end_date=self._payload_expire_date(user_payload),
|
||||
)
|
||||
elif event_name == "user.expired" and self.settings.SUBSCRIPTION_NOTIFY_ON_EXPIRE:
|
||||
await self._send_message(
|
||||
user_id,
|
||||
lang,
|
||||
"subscription_expired_notification",
|
||||
reply_markup=markup,
|
||||
user_name=first_name,
|
||||
end_date=self._payload_expire_date(user_payload),
|
||||
)
|
||||
elif (
|
||||
event_name == "user.expired_24_hours_ago"
|
||||
and self.settings.SUBSCRIPTION_NOTIFY_AFTER_EXPIRE
|
||||
@@ -219,33 +311,113 @@ class PanelWebhookService:
|
||||
"subscription_expired_yesterday_notification",
|
||||
reply_markup=markup,
|
||||
user_name=first_name,
|
||||
end_date=user_payload.get("expireAt", "")[:10],
|
||||
end_date=self._payload_expire_date(user_payload),
|
||||
)
|
||||
|
||||
async def _send_subscription_expiring_email(
|
||||
async def _user_for_payload(
|
||||
self,
|
||||
*,
|
||||
recipient: str,
|
||||
lang: str,
|
||||
days_left: int,
|
||||
end_date_text: str,
|
||||
) -> None:
|
||||
"""Best-effort branded reminder; silently no-ops without SMTP config."""
|
||||
if not self.settings.email_auth_configured:
|
||||
return
|
||||
try:
|
||||
content = render_subscription_expiring(
|
||||
self.settings,
|
||||
language_code=lang,
|
||||
days_left=days_left,
|
||||
end_date_text=end_date_text,
|
||||
dashboard_url=(self.settings.SUBSCRIPTION_MINI_APP_URL or "").strip() or None,
|
||||
i18n=self.i18n,
|
||||
session: AsyncSession,
|
||||
user_payload: dict,
|
||||
) -> Optional[User]:
|
||||
telegram_id = self._payload_telegram_id(user_payload)
|
||||
if telegram_id:
|
||||
user = await user_dal.get_user_by_telegram_id(session, telegram_id)
|
||||
if user:
|
||||
return user
|
||||
user = await user_dal.get_user_by_id(session, telegram_id)
|
||||
if user:
|
||||
return user
|
||||
|
||||
panel_uuid = self._payload_panel_uuid(user_payload)
|
||||
if panel_uuid:
|
||||
user = await user_dal.get_user_by_panel_uuid(session, panel_uuid)
|
||||
if user:
|
||||
return user
|
||||
|
||||
email = str(user_payload.get("email") or "").strip()
|
||||
if email:
|
||||
return await user_dal.get_user_by_email(session, email)
|
||||
return None
|
||||
|
||||
async def _subscription_for_payload(
|
||||
self,
|
||||
session: AsyncSession,
|
||||
user_payload: dict,
|
||||
db_user: Optional[User],
|
||||
) -> Optional[Subscription]:
|
||||
conditions = []
|
||||
if db_user:
|
||||
conditions.append(Subscription.user_id == db_user.user_id)
|
||||
panel_uuid = self._payload_panel_uuid(user_payload)
|
||||
if panel_uuid:
|
||||
conditions.append(Subscription.panel_user_uuid == panel_uuid)
|
||||
if not conditions:
|
||||
return None
|
||||
base_stmt = (
|
||||
select(Subscription)
|
||||
.where(
|
||||
Subscription.skip_notifications == False,
|
||||
or_(*conditions),
|
||||
)
|
||||
email_service = EmailAuthService(self.settings, self.i18n)
|
||||
await email_service.send_rendered_email(email=recipient, content=content)
|
||||
except Exception:
|
||||
logging.exception("Failed to send subscription-expiring email to %s", recipient)
|
||||
.options(selectinload(Subscription.user))
|
||||
)
|
||||
|
||||
expire_at = self._payload_expire_datetime(user_payload)
|
||||
if expire_at is not None:
|
||||
window_stmt = (
|
||||
base_stmt.where(
|
||||
Subscription.end_date >= expire_at - timedelta(days=1),
|
||||
Subscription.end_date <= expire_at + timedelta(days=1),
|
||||
)
|
||||
.order_by(Subscription.end_date.desc())
|
||||
.limit(1)
|
||||
)
|
||||
result = await session.execute(window_stmt)
|
||||
found = result.scalars().first()
|
||||
if found:
|
||||
return found
|
||||
|
||||
stmt = base_stmt.order_by(Subscription.end_date.desc()).limit(1)
|
||||
result = await session.execute(stmt)
|
||||
return result.scalars().first()
|
||||
|
||||
@staticmethod
|
||||
def _payload_telegram_id(user_payload: dict) -> Optional[int]:
|
||||
raw = user_payload.get("telegramId")
|
||||
try:
|
||||
value = int(raw or 0)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
return value if value > 0 else None
|
||||
|
||||
@staticmethod
|
||||
def _payload_panel_uuid(user_payload: dict) -> str:
|
||||
return str(
|
||||
user_payload.get("uuid")
|
||||
or user_payload.get("userUuid")
|
||||
or user_payload.get("shortUuid")
|
||||
or ""
|
||||
).strip()
|
||||
|
||||
@staticmethod
|
||||
def _payload_expire_date(user_payload: dict) -> str:
|
||||
return str(user_payload.get("expireAt") or "")[:10]
|
||||
|
||||
@staticmethod
|
||||
def _payload_expire_datetime(user_payload: dict) -> Optional[datetime]:
|
||||
raw = str(user_payload.get("expireAt") or "").strip()
|
||||
if not raw:
|
||||
return None
|
||||
try:
|
||||
value = datetime.fromisoformat(raw.replace("Z", "+00:00"))
|
||||
except ValueError:
|
||||
try:
|
||||
value = datetime.fromisoformat(raw[:10])
|
||||
except ValueError:
|
||||
return None
|
||||
if value.tzinfo is None:
|
||||
return value.replace(tzinfo=timezone.utc)
|
||||
return value.astimezone(timezone.utc)
|
||||
|
||||
async def handle_webhook(
|
||||
self, raw_body: bytes, signature_header: Optional[str]
|
||||
|
||||
Reference in New Issue
Block a user