|
1 | | - |
2 | 1 | import asyncio |
3 | 2 | import ipaddress |
| 3 | +import logging |
4 | 4 | from telegram import Update |
5 | 5 | from telegram.ext import ( |
6 | 6 | ApplicationBuilder, |
|
11 | 11 | filters, |
12 | 12 | CallbackQueryHandler |
13 | 13 | ) |
14 | | -from app.config import TELEGRAM_API_TOKEN |
| 14 | +from app.config import TELEGRAM_API_TOKEN, SYNC_WITH_PANEL, PANEL_USERNAME, PANEL_PASSWORD, PANEL_ADDRESS |
15 | 15 | from app import user_limit_db, storage |
16 | 16 | from app.db.models import UserLimit |
17 | 17 | from app.models.user import User |
| 18 | +from app.models.panel import Panel |
18 | 19 | from app.nobetnode import nodes |
19 | 20 | from app.utils.telegram import restricted |
| 21 | +from app.db.marzneshin_db import MarzneshinDB |
| 22 | + |
| 23 | +logger = logging.getLogger(__name__) |
20 | 24 |
|
21 | 25 | ( |
22 | 26 | ADD_USER_NAME, |
|
28 | 32 | ACTIVE_IPS_USER_NAME |
29 | 33 | ) = range(7) |
30 | 34 |
|
| 35 | +sync_db_instance = None |
| 36 | +if SYNC_WITH_PANEL: |
| 37 | + try: |
| 38 | + _panel = Panel(username=PANEL_USERNAME, password=PANEL_PASSWORD, domain=PANEL_ADDRESS) |
| 39 | + sync_db_instance = MarzneshinDB(_panel) |
| 40 | + except Exception as e: |
| 41 | + logger.error(f"Failed to initialize MarzneshinDB adapter: {e}") |
| 42 | + |
31 | 43 |
|
32 | 44 | async def build_telegram_bot(): |
33 | 45 | if not TELEGRAM_API_TOKEN or not (application := ApplicationBuilder().token(TELEGRAM_API_TOKEN).build()): |
@@ -136,11 +148,23 @@ async def get_user(update: Update, _context: ContextTypes.DEFAULT_TYPE): |
136 | 148 |
|
137 | 149 | @restricted |
138 | 150 | async def get_user_name(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
139 | | - context.user_data["name"] = update.message.text.strip() |
| 151 | + username = update.message.text.strip() |
| 152 | + context.user_data["name"] = username |
| 153 | + |
| 154 | + user = None |
| 155 | + |
| 156 | + if SYNC_WITH_PANEL and sync_db_instance: |
| 157 | + try: |
| 158 | + user = await sync_db_instance.get(UserLimit.name == username) |
| 159 | + except Exception: |
| 160 | + pass |
140 | 161 |
|
141 | | - user = user_limit_db.get(UserLimit.name == context.user_data["name"]) |
| 162 | + if not user or (SYNC_WITH_PANEL and user.limit == 0): |
| 163 | + local_user = user_limit_db.get(UserLimit.name == username) |
| 164 | + if local_user: |
| 165 | + user = local_user |
142 | 166 |
|
143 | | - if not user: |
| 167 | + if not user or (user.limit == 0 and not user_limit_db.get(UserLimit.name == username)): |
144 | 168 | await context.bot.send_message( |
145 | 169 | chat_id=update.effective_chat.id, |
146 | 170 | text="User Isn't Exists" |
|
0 commit comments