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