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