gitscope
← marzneshin / v0.1.1 / app/telegram/handlers/report.py
https://dev.kronoz.win/r/marzneshin.git
app/telegram/handlers/report.py · Python
Blame Raw
  1import datetime
  2import logging
  3from datetime import datetime
  4
  5from telebot.apihelper import ApiTelegramException
  6from telebot.formatting import escape_html
  7
  8from app.telegram import bot
  9from app.telegram.utils.keyboard import BotKeyboard
 10from app.utils.system import readable_size
 11from config import TELEGRAM_ADMIN_ID, TELEGRAM_LOGGER_CHANNEL_ID
 12
 13logger = logging.getLogger(__name__)
 14
 15
 16async def report(message: str, parse_mode="html", keyboard=None):
 17    if bot and (TELEGRAM_ADMIN_ID or TELEGRAM_LOGGER_CHANNEL_ID):
 18        try:
 19            if TELEGRAM_LOGGER_CHANNEL_ID:
 20                await bot.send_message(TELEGRAM_LOGGER_CHANNEL_ID, message, parse_mode=parse_mode)
 21            else:
 22                for admin in TELEGRAM_ADMIN_ID:
 23                    await bot.send_message(admin, message, parse_mode=parse_mode, reply_markup=keyboard)
 24        except ApiTelegramException as e:
 25            logger.error(e)
 26
 27
 28async def report_new_user(user_id: int, username: str, by: str, expire_date: int, data_limit: int, services: list):
 29    text = '''\
 30🆕 <b>#Created</b>
 31➖➖➖➖➖➖➖➖➖
 32<b>Username :</b> <code>{username}</code>
 33<b>Traffic Limit :</b> <code>{data_limit}</code>
 34<b>Expire Date :</b> <code>{expire_date}</code>
 35<b>Services :</b> <code>{services}</code>
 36➖➖➖➖➖➖➖➖➖
 37<b>By :</b> <b>#{by}</b>'''.format(
 38        by=escape_html(by),
 39        username=escape_html(username),
 40        data_limit=readable_size(data_limit) if data_limit else "Unlimited",
 41        expire_date=datetime.fromtimestamp(expire_date).strftime("%H:%M:%S %Y-%m-%d") if expire_date else "Never",
 42        services="" if not services else ", ".join([s.name for s in services])
 43    )
 44
 45    return await report(
 46        text,
 47        keyboard=BotKeyboard.user_menu({
 48            'username': username,
 49            'id': user_id,
 50            'status': 'active'
 51        }, with_back=False)
 52    )
 53
 54
 55async def report_user_modification(username: str, expire_date: int, data_limit: int, services: list, by: str):
 56    text = '''\
 57✏️ <b>#Modified</b>
 58➖➖➖➖➖➖➖➖➖
 59<b>Username :</b> <code>{username}</code>
 60<b>Traffic Limit :</b> <code>{data_limit}</code>
 61<b>Expire Date :</b> <code>{expire_date}</code>
 62<b>Services :</b> <code>{services}</code>
 63➖➖➖➖➖➖➖➖➖
 64<b>By :</b> <b>#{by}</b>\
 65    '''.format(
 66        by=escape_html(by),
 67        username=escape_html(username),
 68        data_limit=readable_size(data_limit) if data_limit else "Unlimited",
 69        expire_date=datetime.fromtimestamp(expire_date).strftime("%H:%M:%S %Y-%m-%d") if expire_date else "Never",
 70        services=", ".join([s.name for s in services])
 71    )
 72
 73    return await report(text, keyboard=BotKeyboard.user_menu({
 74        'username': username,
 75        'status': 'active'
 76    }, with_back=False))
 77
 78
 79async def report_user_deletion(username: str, by: str):
 80    text = '''\
 81🗑 <b>#Deleted</b>
 82➖➖➖➖➖➖➖➖➖
 83<b>Username</b> : <code>{username}</code>
 84➖➖➖➖➖➖➖➖➖
 85<b>By</b> : <b>#{by}</b>\
 86    '''.format(
 87        by=escape_html(by),
 88        username=escape_html(username)
 89    )
 90    return await report(text)
 91
 92
 93async def report_status_change(username: str, status: str):
 94    _status = {
 95        'active': '✅ <b>#Activated</b>',
 96        'disabled': '❌ <b>#Disabled</b>',
 97        'limited': '🪫 <b>#Limited</b>',
 98        'expired': '🕔 <b>#Expired</b>'
 99    }
100    text = '''\
101{status}
102➖➖➖➖➖➖➖➖➖
103<b>Username</b> : <code>{username}</code>\
104    '''.format(
105        username=escape_html(username),
106        status=_status[status]
107    )
108    return await report(text)
109
110
111async def report_user_usage_reset(username: str, by: str):
112    text = """  
113🔁 <b>#Reset</b>
114➖➖➖➖➖➖➖➖➖
115<b>Username</b> : <code>{username}</code>
116➖➖➖➖➖➖➖➖➖
117<b>By</b> : <b>#{by}</b>\
118    """.format(
119        by=escape_html(by),
120        username=escape_html(username)
121    )
122
123    return await report(text)
124
125
126async def report_user_subscription_revoked(username: str, by: str):
127    text = """  
128🔁 <b>#Revoked</b>
129➖➖➖➖➖➖➖➖➖
130<b>Username</b> : <code>{username}</code>
131➖➖➖➖➖➖➖➖➖
132<b>By</b> : <b>#{by}</b>\
133    """.format(
134        by=escape_html(by),
135        username=escape_html(username)
136    )
137
138    return await report(text)