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