Add configurable welcome bonus days for referred signups
- add REFERRAL_WELCOME_BONUS_DAYS setting (default 3) in settings.py - document REFERRAL_WELCOME_BONUS_DAYS in .env.example - apply welcome bonus on first /start only for newly created users with referred_by_id - replace hardcoded 3 days with settings.REFERRAL_WELCOME_BONUS_DAYS - skip bonus flow when value is 0 or less - send user notification after successful bonus application - add i18n key referral_welcome_bonus_applied to ru.json and en.json
This commit is contained in:
@@ -124,6 +124,7 @@ SUBSCRIPTION_NOTIFY_DAYS_BEFORE=3 #
|
|||||||
|
|
||||||
|
|
||||||
REFERRAL_ONE_BONUS_PER_REFEREE=False # Give a bonus only once per referee
|
REFERRAL_ONE_BONUS_PER_REFEREE=False # Give a bonus only once per referee
|
||||||
|
REFERRAL_WELCOME_BONUS_DAYS=3 # Welcome bonus for newly registered user from referral link
|
||||||
LEGACY_REFS=true # Allow ref_<tg_id> links. Leave unset/true unless you want to disable old links
|
LEGACY_REFS=true # Allow ref_<tg_id> links. Leave unset/true unless you want to disable old links
|
||||||
# Referral Bonus Days
|
# Referral Bonus Days
|
||||||
# Bonus for the inviting user
|
# Bonus for the inviting user
|
||||||
|
|||||||
@@ -449,6 +449,50 @@ async def start_command_handler(message: types.Message,
|
|||||||
f"New user {user_id} added to session. Referred by: {referred_by_user_id or 'N/A'}."
|
f"New user {user_id} added to session. Referred by: {referred_by_user_id or 'N/A'}."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Auto-grant referral welcome bonus to newly registered referred users.
|
||||||
|
referral_welcome_days = max(
|
||||||
|
0, int(getattr(settings, "REFERRAL_WELCOME_BONUS_DAYS", 0) or 0)
|
||||||
|
)
|
||||||
|
if referred_by_user_id and referral_welcome_days > 0:
|
||||||
|
try:
|
||||||
|
referral_bonus_end_date = await subscription_service.extend_active_subscription_days(
|
||||||
|
session,
|
||||||
|
user_id,
|
||||||
|
referral_welcome_days,
|
||||||
|
reason="referral_welcome_bonus",
|
||||||
|
)
|
||||||
|
if referral_bonus_end_date:
|
||||||
|
await session.commit()
|
||||||
|
logging.info(
|
||||||
|
"Referral welcome bonus applied: user %s got %s days, new end date %s.",
|
||||||
|
user_id,
|
||||||
|
referral_welcome_days,
|
||||||
|
referral_bonus_end_date.isoformat(),
|
||||||
|
)
|
||||||
|
await message.answer(
|
||||||
|
_(
|
||||||
|
"referral_welcome_bonus_applied",
|
||||||
|
days=referral_welcome_days,
|
||||||
|
end_date=referral_bonus_end_date.strftime("%d.%m.%Y %H:%M:%S"),
|
||||||
|
),
|
||||||
|
parse_mode="HTML",
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
await session.rollback()
|
||||||
|
logging.warning(
|
||||||
|
"Referral welcome bonus was not applied for user %s (referred by %s).",
|
||||||
|
user_id,
|
||||||
|
referred_by_user_id,
|
||||||
|
)
|
||||||
|
except Exception as referral_bonus_error:
|
||||||
|
await session.rollback()
|
||||||
|
logging.error(
|
||||||
|
"Failed to apply referral welcome bonus for user %s: %s",
|
||||||
|
user_id,
|
||||||
|
referral_bonus_error,
|
||||||
|
exc_info=True,
|
||||||
|
)
|
||||||
|
|
||||||
# Send notification about new user registration
|
# Send notification about new user registration
|
||||||
try:
|
try:
|
||||||
from bot.services.notification_service import NotificationService
|
from bot.services.notification_service import NotificationService
|
||||||
|
|||||||
@@ -170,6 +170,10 @@ class Settings(BaseSettings):
|
|||||||
default=True,
|
default=True,
|
||||||
description="When true, referral bonuses (for inviter and referee) are applied only once per invited user - on their first successful payment."
|
description="When true, referral bonuses (for inviter and referee) are applied only once per invited user - on their first successful payment."
|
||||||
)
|
)
|
||||||
|
REFERRAL_WELCOME_BONUS_DAYS: int = Field(
|
||||||
|
default=3,
|
||||||
|
description="Welcome bonus days granted to a newly registered user who joined via referral link.",
|
||||||
|
)
|
||||||
LEGACY_REFS: bool = Field(
|
LEGACY_REFS: bool = Field(
|
||||||
default=True,
|
default=True,
|
||||||
description="Allow legacy referral links like ref_<telegram_id> to continue working. Defaults to True when unset."
|
description="Allow legacy referral links like ref_<telegram_id> to continue working. Defaults to True when unset."
|
||||||
|
|||||||
@@ -92,6 +92,7 @@
|
|||||||
"trial_confirm_activate_button": "✅ Activate!",
|
"trial_confirm_activate_button": "✅ Activate!",
|
||||||
"trial_activated_alert": "Trial activated!",
|
"trial_activated_alert": "Trial activated!",
|
||||||
"trial_activated_details_message": "✅ Trial activated!\nYour {days}-day trial is active until {end_date}.\n\nConnection key:\n<code>{config_link}</code>\n\nTo connect, open the link and follow the instructions 👇",
|
"trial_activated_details_message": "✅ Trial activated!\nYour {days}-day trial is active until {end_date}.\n\nConnection key:\n<code>{config_link}</code>\n\nTo connect, open the link and follow the instructions 👇",
|
||||||
|
"referral_welcome_bonus_applied": "🎁 You have received {days} bonus day(s) for registering via a referral link!\nYour subscription is active until {end_date}.",
|
||||||
"yes_button": "Yes",
|
"yes_button": "Yes",
|
||||||
"no_button": "No",
|
"no_button": "No",
|
||||||
"referral_program_info_new": "🎁 <b>Referral Program</b>\n\n📊 <b>Your stats:</b>\n👥 Friends invited: <b>{invited_count}</b>\n💳 Purchased subscription: <b>{purchased_count}</b>\n\n🔗 Your link:\n<code>{referral_link}</code>\n\n💰 <b>Invitation bonuses:</b>\n{bonus_details}\n\n📢 Share the link with friends and get bonuses!",
|
"referral_program_info_new": "🎁 <b>Referral Program</b>\n\n📊 <b>Your stats:</b>\n👥 Friends invited: <b>{invited_count}</b>\n💳 Purchased subscription: <b>{purchased_count}</b>\n\n🔗 Your link:\n<code>{referral_link}</code>\n\n💰 <b>Invitation bonuses:</b>\n{bonus_details}\n\n📢 Share the link with friends and get bonuses!",
|
||||||
|
|||||||
@@ -92,6 +92,7 @@
|
|||||||
"trial_confirm_activate_button": "✅ Активировать!",
|
"trial_confirm_activate_button": "✅ Активировать!",
|
||||||
"trial_activated_alert": "Пробный период активирован!",
|
"trial_activated_alert": "Пробный период активирован!",
|
||||||
"trial_activated_details_message": "✅ Пробный доступ активирован!\nВаш триал на {days} дн. действует до {end_date}.\n\nКлюч подключения:\n<code>{config_link}</code>\n\nЧтобы подключиться, перейдите по ссылке и следуйте инструкции 👇",
|
"trial_activated_details_message": "✅ Пробный доступ активирован!\nВаш триал на {days} дн. действует до {end_date}.\n\nКлюч подключения:\n<code>{config_link}</code>\n\nЧтобы подключиться, перейдите по ссылке и следуйте инструкции 👇",
|
||||||
|
"referral_welcome_bonus_applied": "🎁 Вам начислено {days} бонусных дн. за регистрацию по реферальной ссылке!\nПодписка активна до {end_date}.",
|
||||||
"yes_button": "Да",
|
"yes_button": "Да",
|
||||||
"no_button": "Нет",
|
"no_button": "Нет",
|
||||||
"referral_program_info_new": "🎁 <b>Реферальная программа</b>\n\n📊 <b>Твоя статистика:</b>\n👥 Приглашено друзей: <b>{invited_count}</b>\n💳 Купили подписку: <b>{purchased_count}</b>\n\n🔗 Твоя ссылка:\n<code>{referral_link}</code>\n\n💰 <b>Бонусы за приглашения:</b>\n{bonus_details}\n\n📢 Поделись ссылкой с друзьями и получай бонусы!",
|
"referral_program_info_new": "🎁 <b>Реферальная программа</b>\n\n📊 <b>Твоя статистика:</b>\n👥 Приглашено друзей: <b>{invited_count}</b>\n💳 Купили подписку: <b>{purchased_count}</b>\n\n🔗 Твоя ссылка:\n<code>{referral_link}</code>\n\n💰 <b>Бонусы за приглашения:</b>\n{bonus_details}\n\n📢 Поделись ссылкой с друзьями и получай бонусы!",
|
||||||
|
|||||||
Reference in New Issue
Block a user