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