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