diff --git a/src/club/campaigns.py b/src/club/campaigns.py index 7d5a1b8..4f08f29 100644 --- a/src/club/campaigns.py +++ b/src/club/campaigns.py @@ -126,7 +126,7 @@ def _calculate_score(cls, total_subset: int): @classmethod def _is_channel_member(cls, db_user: User): result = bot.get_chat_member( - chat_id=config.TELEGRAM_CHANNEL, + chat_id=f"@{config.TELEGRAM_CHANNEL}", user_id=db_user.telegram_chat_id, ) if result.status in ["administrator", "creator", "member"]: diff --git a/src/club/jobs.py b/src/club/jobs.py index c59a258..08ffee0 100644 --- a/src/club/jobs.py +++ b/src/club/jobs.py @@ -95,7 +95,7 @@ def create_new_club_profiles(): def _is_channel_member(db_user: User): result = bot.get_chat_member( - chat_id=config.TELEGRAM_CHANNEL, user_id=db_user.telegram_chat_id + chat_id=f"@{config.TELEGRAM_CHANNEL}", user_id=db_user.telegram_chat_id ) if result.status in ["administrator", "creator", "member"]: logger.info(f"User {db_user.full_name} is channel member") diff --git a/src/config.py b/src/config.py index d4c44fa..b20894c 100644 --- a/src/config.py +++ b/src/config.py @@ -25,8 +25,8 @@ print("Failed to get SERVER_IP, using 127.0.0.1 instead") SERVER_IP = "127.0.0.1" -UVICORN_HOST = config("UVICORN_HOST", default="0.0.0.0") -UVICORN_PORT = config("UVICORN_PORT", cast=int, default=8000) +UVICORN_HOST = get_setting("UVICORN_HOST", default="0.0.0.0") +UVICORN_PORT = get_setting("UVICORN_PORT", cast=int, default=8000) UVICORN_UDS = config("UVICORN_UDS", default=None) UVICORN_SSL_CERTFILE = get_setting("UVICORN_SSL_CERTFILE", default=None) UVICORN_SSL_KEYFILE = get_setting("UVICORN_SSL_KEYFILE", default=None) @@ -37,9 +37,18 @@ ) TELEGRAM_ADMIN_ID = get_setting("TELEGRAM_ADMIN_ID", cast=int, default=0) TELEGRAM_ADMIN_USER_NAME = get_setting("TELEGRAM_ADMIN_USER_NAME", default=None) -BOT_USER_NAME = config("BOT_USER_NAME", default="") -TELEGRAM_CHANNEL = config("TELEGRAM_CHANNEL", default=None) -TELEGRAM_PROXY_URL = config("TELEGRAM_PROXY_URL", default=None) +BOT_USER_NAME = get_setting("BOT_USER_NAME", default="") +TELEGRAM_CHANNEL = get_setting("TELEGRAM_CHANNEL", default=None) +TELEGRAM_PROXY_URL = get_setting("TELEGRAM_PROXY_URL", default=None) + +# Bot URLS + +TELEGRAM_CHANNEL_URL = get_setting("TELEGRAM_CHANNEL_URL", default="") +IPHONE_HELP_POST_URL = get_setting("IPHONE_HELP_POST_URL", default="") +ANDROID_HELP_POST_URL = get_setting("ANDROID_HELP_POST_URL", default="") +WINDOWS_HELP_POST_URL = get_setting("WINDOWS_HELP_POST_URL", default="") +MAC_HELP_POST_URL = get_setting("MAC_HELP_POST_URL", default="") + MINIMUM_PAYMENT_TO_TRUST_USER = config( "MINIMUM_PAYMENT_TO_TRUST_USER", cast=int, default=200000 @@ -47,8 +56,8 @@ TRUST_CARD_NUMBER = config("TRUST_CARD_NUMBER", default="") TRUST_CARD_OWNER = config("TRUST_CARD_OWNER", default="") -CARD_NUMBER = config("CARD_NUMBER", default="") -CARD_OWNER = config("CARD_OWNER", default="") +CARD_NUMBER = get_setting("CARD_NUMBER", default="") +CARD_OWNER = get_setting("CARD_OWNER", default="") TEST_ACCOUNT_EMAIL_PREFIX = config("TEST_ACCOUNT_EMAIL_PREFIX", default="test_") TEST_ACCOUNT_LIMIT_INTERVAL_DAYS = config( @@ -60,7 +69,7 @@ ) TEST_ACCOUNT_HOST_ZONE_ID = config("TEST_ACCOUNT_HOST_ZONE_ID", cast=int, default=1) -TEST_SERVICE_ID = config("TEST_SERVICE_ID", cast=int, default=0) +TEST_SERVICE_ID = get_setting("TEST_SERVICE_ID", cast=int, default=0) JWT_ACCESS_TOKEN_EXPIRE_MINUTES = config( "JWT_ACCESS_TOKEN_EXPIRE_MINUTES", cast=int, default=1440 @@ -71,7 +80,7 @@ config("SUDO_USERNAME", default="admin"): config("SUDO_PASSWORD", default="admin") } -SUBSCRIPTION_BASE_URL = config( +SUBSCRIPTION_BASE_URL = get_setting( "SUBSCRIPTION_BASE_URL", default="https://localhost:8000/api/sub" ) diff --git a/src/migration/alembic/versions/47010335de98_move_some_settings_to_db.py b/src/migration/alembic/versions/47010335de98_move_some_settings_to_db.py new file mode 100644 index 0000000..43a3815 --- /dev/null +++ b/src/migration/alembic/versions/47010335de98_move_some_settings_to_db.py @@ -0,0 +1,75 @@ +"""Move some settings to DB + +Revision ID: 47010335de98 +Revises: a00786a4da47 +Create Date: 2024-11-30 22:17:52.510679 + +""" + +from datetime import datetime + +from alembic import op +import sqlalchemy as sa + +from src import config + +# revision identifiers, used by Alembic. +revision = "47010335de98" +down_revision = "a00786a4da47" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + config_settings_table = sa.table( + "config_settings", + sa.Column("key", sa.String(), nullable=False), + sa.Column("value", sa.String(), nullable=True), + sa.Column("value_type", sa.String(), nullable=True), + sa.Column("updated_at", sa.DateTime(), nullable=True), + # Add other columns as needed + ) + op.bulk_insert( + config_settings_table, + [ + { + "key": "TEST_SERVICE_ID", + "value": config.TEST_SERVICE_ID, + "value_type": "int", + "updated_at": datetime.utcnow(), + }, + { + "key": "TELEGRAM_CHANNEL", + "value": config.TELEGRAM_CHANNEL, + "value_type": "str", + "updated_at": datetime.utcnow(), + }, + { + "key": "BOT_USER_NAME", + "value": config.BOT_USER_NAME, + "value_type": "str", + "updated_at": datetime.utcnow(), + }, + { + "key": "UVICORN_HOST", + "value": config.UVICORN_HOST, + "value_type": "str", + "updated_at": datetime.utcnow(), + }, + { + "key": "UVICORN_PORT", + "value": config.UVICORN_PORT, + "value_type": "int", + "updated_at": datetime.utcnow(), + }, + ], + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f("ix_config_settings_key"), table_name="config_settings") + op.create_index("ix_config_settings_key", "config_settings", ["key"], unique=False) + # ### end Alembic commands ### diff --git a/src/migration/alembic/versions/b1bc7aad133f_add_subscription_base_url_settings_to_db.py b/src/migration/alembic/versions/b1bc7aad133f_add_subscription_base_url_settings_to_db.py new file mode 100644 index 0000000..83f59c9 --- /dev/null +++ b/src/migration/alembic/versions/b1bc7aad133f_add_subscription_base_url_settings_to_db.py @@ -0,0 +1,50 @@ +"""Add SUBSCRIPTION_BASE_URL settings to DB + +Revision ID: b1bc7aad133f +Revises: 47010335de98 +Create Date: 2024-11-30 22:53:24.042474 + +""" +from datetime import datetime + +from alembic import op +import sqlalchemy as sa + +from src import config + +# revision identifiers, used by Alembic. +revision = 'b1bc7aad133f' +down_revision = '47010335de98' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + config_settings_table = sa.table( + "config_settings", + sa.Column("key", sa.String(), nullable=False), + sa.Column("value", sa.String(), nullable=True), + sa.Column("value_type", sa.String(), nullable=True), + sa.Column("updated_at", sa.DateTime(), nullable=True), + # Add other columns as needed + ) + op.bulk_insert( + config_settings_table, + [ + { + "key": "SUBSCRIPTION_BASE_URL", + "value": config.SUBSCRIPTION_BASE_URL, + "value_type": "str", + "updated_at": datetime.utcnow(), + } + ], + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_config_settings_key'), table_name='config_settings') + op.create_index('ix_config_settings_key', 'config_settings', ['key'], unique=False) + # ### end Alembic commands ### diff --git a/src/telegram/user/keyboard.py b/src/telegram/user/keyboard.py index 26a2113..1a73320 100644 --- a/src/telegram/user/keyboard.py +++ b/src/telegram/user/keyboard.py @@ -1,5 +1,6 @@ from telebot import types # noqa +from src import config from src.accounts.models import Account from src.commerce.schemas import OrderStatus from src.telegram import utils @@ -50,21 +51,15 @@ def channel_menu(): def help_links(): keyboard = types.InlineKeyboardMarkup() keyboard.add( + types.InlineKeyboardButton(text="آیفون", url=config.IPHONE_HELP_POST_URL), types.InlineKeyboardButton( - text="آیفون", url="https://t.me/EloraVPNChannel/210" - ), - types.InlineKeyboardButton( - text="اندروید", url="https://t.me/EloraVPNChannel/72" + text="اندروید", url=config.ANDROID_HELP_POST_URL ), ) keyboard.add( - types.InlineKeyboardButton( - text="ویندوز", url="https://t.me/EloraVPNChannel/90" - ), - types.InlineKeyboardButton( - text="مک بوک", url="https://t.me/EloraVPNChannel/93" - ), + types.InlineKeyboardButton(text="ویندوز", url=config.WINDOWS_HELP_POST_URL), + types.InlineKeyboardButton(text="مک بوک", url=config.MAC_HELP_POST_URL), ) return keyboard diff --git a/src/telegram/user/messages.py b/src/telegram/user/messages.py index c70df63..b294d6d 100644 --- a/src/telegram/user/messages.py +++ b/src/telegram/user/messages.py @@ -13,7 +13,7 @@ همچنین از طریق منوی زیر می توانید به اطلاعات پروفایل و وضعیت سرویس های خریداری شده خود دسترسی داشته باشید. -[📣 کانال تلگرام](https://t.me/EloraVPNChannel/73) +[📣 کانال تلگرام]({telegram_channel_url}) [💬 ارتباط با پشتیبانی](https://t.me/{admin_id}) """ @@ -98,7 +98,7 @@ به نام: {card_owner} -📣 کانال تلگرام +📣 کانال تلگرام 💬 ارتباط با پشتیبانی """ diff --git a/src/telegram/user/user_handlers.py b/src/telegram/user/user_handlers.py index 9e2d8db..d5f5345 100644 --- a/src/telegram/user/user_handlers.py +++ b/src/telegram/user/user_handlers.py @@ -50,7 +50,7 @@ def check(message: types.Message): return True else: result = bot.get_chat_member( - config.TELEGRAM_CHANNEL, user_id=message.from_user.id + f"@{config.TELEGRAM_CHANNEL}", user_id=message.from_user.id ) if result.status not in ["administrator", "creator", "member"]: bot.send_message( @@ -113,7 +113,10 @@ def start_game(message: types.Message): def send_welcome(message: types.Message): bot.send_message( chat_id=message.from_user.id, - text=messages.WELCOME_MESSAGE.format(admin_id=config.TELEGRAM_ADMIN_USER_NAME), + text=messages.WELCOME_MESSAGE.format( + admin_id=config.TELEGRAM_ADMIN_USER_NAME, + telegram_channel_url=config.TELEGRAM_CHANNEL_URL, + ), disable_web_page_preview=True, reply_markup=BotUserKeyboard.main_menu(), parse_mode="markdown", @@ -163,7 +166,10 @@ def my_profile(message): def support(message): bot.reply_to( message, - text=messages.WELCOME_MESSAGE.format(admin_id=config.TELEGRAM_ADMIN_USER_NAME), + text=messages.WELCOME_MESSAGE.format( + telegram_channel_url=config.TELEGRAM_CHANNEL_URL, + admin_id=config.TELEGRAM_ADMIN_USER_NAME, + ), parse_mode="markdown", ) @@ -196,14 +202,16 @@ def payment(message): total_payment = utils.get_total_payment(user_id=user.id) text = messages.PAYMENT_MESSAGE.format( + telegram_channel_url=config.TELEGRAM_CHANNEL_URL, balance=user.balance_readable, card_number=config.CARD_NUMBER, card_owner=config.CARD_OWNER, admin_id=config.TELEGRAM_ADMIN_USER_NAME, ) - if total_payment > config.MINIMUM_PAYMENT_TO_TRUST_USER: + if total_payment > config.MINIMUM_PAYMENT_TO_TRUST_USER > 0: text = messages.PAYMENT_MESSAGE.format( + telegram_channel_url=config.TELEGRAM_CHANNEL_URL, balance=user.balance_readable, card_number=config.TRUST_CARD_NUMBER, card_owner=config.TRUST_CARD_OWNER, @@ -385,7 +393,10 @@ def main_menu(call: types.CallbackQuery): bot.send_message( chat_id=call.from_user.id, - text=messages.WELCOME_MESSAGE.format(admin_id=config.TELEGRAM_ADMIN_USER_NAME), + text=messages.WELCOME_MESSAGE.format( + telegram_channel_url=config.TELEGRAM_CHANNEL_URL, + admin_id=config.TELEGRAM_ADMIN_USER_NAME, + ), disable_web_page_preview=True, reply_markup=BotUserKeyboard.main_menu(), parse_mode="markdown",