gitscope
https://dev.kronoz.win/r/marzneshin.git
app/telegram/utils/keyboard.py · v0.3.0-alpha3
Source
1 a47eb4c Dawsh 2y ago from datetime import datetime as dt
2 a47eb4c Dawsh 2y ago from itertools import islice
3 a47eb4c Dawsh 2y ago from typing import Literal, Dict, List
4 a47eb4c Dawsh 2y ago
5 a47eb4c Dawsh 2y ago from telebot import types # noqa
6 a47eb4c Dawsh 2y ago
7 a47eb4c Dawsh 2y ago from app.utils.system import readable_size
8 a47eb4c Dawsh 2y ago
9 a47eb4c Dawsh 2y ago
10 a47eb4c Dawsh 2y ago def chunk_dict(data: dict, size: int = 2):
11 a47eb4c Dawsh 2y ago it = iter(data)
12 a47eb4c Dawsh 2y ago for i in range(0, len(data), size):
13 a47eb4c Dawsh 2y ago yield {k: data[k] for k in islice(it, size)}
14 a47eb4c Dawsh 2y ago
15 a47eb4c Dawsh 2y ago
16 a47eb4c Dawsh 2y ago class BotKeyboard:
17 a47eb4c Dawsh 2y ago
18 a47eb4c Dawsh 2y ago @staticmethod
19 a47eb4c Dawsh 2y ago def main_menu():
20 a47eb4c Dawsh 2y ago keyboard = types.InlineKeyboardMarkup()
21 a47eb4c Dawsh 2y ago keyboard.add(
22 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(text="🔁 System Info", callback_data="system"),
23 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(text="♻️ Restart Xray", callback_data="restart"),
24 01cdf2e Dawsh 2y ago )
25 a47eb4c Dawsh 2y ago keyboard.add(
26 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(text="👥 Users", callback_data="users:1"),
27 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(
28 01cdf2e Dawsh 2y ago text="✏️ Edit All Users", callback_data="edit_all"
29 01cdf2e Dawsh 2y ago ),
30 01cdf2e Dawsh 2y ago )
31 01cdf2e Dawsh 2y ago # keyboard.add(
32 9bfa731 Dawsh 2y ago # types.InlineKeyboardButton(text='➕ Create User From Template', callback_data='template_add_user'))
33 a47eb4c Dawsh 2y ago keyboard.add(
34 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(text="➕ Create User", callback_data="add_user")
35 01cdf2e Dawsh 2y ago )
36 a47eb4c Dawsh 2y ago return keyboard
37 a47eb4c Dawsh 2y ago
38 a47eb4c Dawsh 2y ago @staticmethod
39 a47eb4c Dawsh 2y ago def edit_all_menu():
40 a47eb4c Dawsh 2y ago keyboard = types.InlineKeyboardMarkup()
41 a47eb4c Dawsh 2y ago keyboard.add(
42 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(
43 01cdf2e Dawsh 2y ago text="🗑 Delete Expired", callback_data="delete_expired"
44 01cdf2e Dawsh 2y ago ),
45 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(
46 01cdf2e Dawsh 2y ago text="🗑 Delete Limited", callback_data="delete_limited"
47 01cdf2e Dawsh 2y ago ),
48 01cdf2e Dawsh 2y ago )
49 a47eb4c Dawsh 2y ago keyboard.add(
50 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(
51 01cdf2e Dawsh 2y ago text="🔋 Data (➕|➖)", callback_data="add_data"
52 01cdf2e Dawsh 2y ago ),
53 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(
54 01cdf2e Dawsh 2y ago text="📅 Time (➕|➖)", callback_data="add_time"
55 01cdf2e Dawsh 2y ago ),
56 01cdf2e Dawsh 2y ago )
57 01cdf2e Dawsh 2y ago # keyboard.add(
58 9bfa731 Dawsh 2y ago # types.InlineKeyboardButton(text='➕ Add Inbound', callback_data='inbound_add'),
59 9bfa731 Dawsh 2y ago # types.InlineKeyboardButton(text='➖ Remove Inbound', callback_data='inbound_remove'))
60 01cdf2e Dawsh 2y ago keyboard.add(types.InlineKeyboardButton(text="🔙 Back", callback_data="cancel"))
61 a47eb4c Dawsh 2y ago return keyboard
62 a47eb4c Dawsh 2y ago
63 01cdf2e Dawsh 2y ago # @staticmethod
64 01cdf2e Dawsh 2y ago # def inbounds_menu(action, inbounds):
65 9bfa731 Dawsh 2y ago # keyboard = types.InlineKeyboardMarkup()
66 9bfa731 Dawsh 2y ago # for inbound in inbounds:
67 9bfa731 Dawsh 2y ago # keyboard.add(types.InlineKeyboardButton(text=inbound, callback_data=f'confirm_{action}:{inbound}'))
68 9bfa731 Dawsh 2y ago # keyboard.add(types.InlineKeyboardButton(text='🔙 Back', callback_data='cancel'))
69 9bfa731 Dawsh 2y ago # return keyboard
70 a47eb4c Dawsh 2y ago
71 a47eb4c Dawsh 2y ago @staticmethod
72 01cdf2e Dawsh 2y ago def random_username(template_id: str = ""):
73 a47eb4c Dawsh 2y ago keyboard = types.InlineKeyboardMarkup()
74 a47eb4c Dawsh 2y ago
75 01cdf2e Dawsh 2y ago keyboard.add(
76 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(
77 01cdf2e Dawsh 2y ago text="🔡 Random Username", callback_data=f"random:{template_id}"
78 01cdf2e Dawsh 2y ago )
79 01cdf2e Dawsh 2y ago )
80 01cdf2e Dawsh 2y ago keyboard.add(
81 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(text="🔙 Cancel", callback_data="cancel")
82 01cdf2e Dawsh 2y ago )
83 a47eb4c Dawsh 2y ago return keyboard
84 a47eb4c Dawsh 2y ago
85 a47eb4c Dawsh 2y ago @staticmethod
86 a47eb4c Dawsh 2y ago def user_menu(user_info, with_back: bool = True, page: int = 1, note: bool = False):
87 a47eb4c Dawsh 2y ago keyboard = types.InlineKeyboardMarkup()
88 a47eb4c Dawsh 2y ago keyboard.add(
89 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
90 01cdf2e Dawsh 2y ago text="❌ Disable" if user_info["status"] == "active" else "✅ Activate",
91 01cdf2e Dawsh 2y ago callback_data=f"{'suspend' if user_info['status'] == 'active' else 'activate'}:{user_info['username']}",
92 a47eb4c Dawsh 2y ago ),
93 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
94 01cdf2e Dawsh 2y ago text="🗑 Delete", callback_data=f"delete:{user_info['username']}"
95 a47eb4c Dawsh 2y ago ),
96 a47eb4c Dawsh 2y ago )
97 a47eb4c Dawsh 2y ago if note:
98 a47eb4c Dawsh 2y ago keyboard.add(
99 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
100 01cdf2e Dawsh 2y ago text="🚫 Revoke Sub",
101 01cdf2e Dawsh 2y ago callback_data=f"revoke_sub:{user_info['username']}",
102 01cdf2e Dawsh 2y ago ),
103 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
104 01cdf2e Dawsh 2y ago text="✏️ Edit", callback_data=f"edit:{user_info['username']}"
105 01cdf2e Dawsh 2y ago ),
106 01cdf2e Dawsh 2y ago )
107 a47eb4c Dawsh 2y ago keyboard.add(
108 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
109 01cdf2e Dawsh 2y ago text="📝 Edit Note",
110 01cdf2e Dawsh 2y ago callback_data=f"edit_note:{user_info['username']}",
111 01cdf2e Dawsh 2y ago ),
112 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
113 01cdf2e Dawsh 2y ago text="📡 Links", callback_data=f"links:{user_info['username']}"
114 01cdf2e Dawsh 2y ago ),
115 01cdf2e Dawsh 2y ago )
116 a47eb4c Dawsh 2y ago else:
117 a47eb4c Dawsh 2y ago keyboard.add(
118 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
119 01cdf2e Dawsh 2y ago text="📡 Links", callback_data=f"links:{user_info['username']}"
120 01cdf2e Dawsh 2y ago ),
121 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
122 01cdf2e Dawsh 2y ago text="✏️ Edit", callback_data=f"edit:{user_info['username']}"
123 01cdf2e Dawsh 2y ago ),
124 01cdf2e Dawsh 2y ago )
125 a47eb4c Dawsh 2y ago keyboard.add(
126 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
127 01cdf2e Dawsh 2y ago text="🔁 Reset usage",
128 01cdf2e Dawsh 2y ago callback_data=f"reset_usage:{user_info['username']}",
129 a47eb4c Dawsh 2y ago ),
130 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
131 01cdf2e Dawsh 2y ago text="🔋 Charge", callback_data=f"charge:{user_info['username']}"
132 01cdf2e Dawsh 2y ago ),
133 a47eb4c Dawsh 2y ago )
134 a47eb4c Dawsh 2y ago if with_back:
135 a47eb4c Dawsh 2y ago keyboard.add(
136 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
137 01cdf2e Dawsh 2y ago text="🔙 Back", callback_data=f"users:{page}"
138 a47eb4c Dawsh 2y ago )
139 a47eb4c Dawsh 2y ago )
140 a47eb4c Dawsh 2y ago return keyboard
141 a47eb4c Dawsh 2y ago
142 a47eb4c Dawsh 2y ago @staticmethod
143 a47eb4c Dawsh 2y ago def show_links(username: str):
144 a47eb4c Dawsh 2y ago keyboard = types.InlineKeyboardMarkup()
145 a47eb4c Dawsh 2y ago
146 a47eb4c Dawsh 2y ago keyboard.add(
147 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
148 01cdf2e Dawsh 2y ago text="🖼 QR code", callback_data=f"genqr:{username}"
149 a47eb4c Dawsh 2y ago )
150 a47eb4c Dawsh 2y ago )
151 a47eb4c Dawsh 2y ago keyboard.add(
152 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(text="🔙 Back", callback_data=f"user:{username}")
153 a47eb4c Dawsh 2y ago )
154 a47eb4c Dawsh 2y ago return keyboard
155 a47eb4c Dawsh 2y ago
156 a47eb4c Dawsh 2y ago @staticmethod
157 a47eb4c Dawsh 2y ago def subscription_page(sub_url: str):
158 a47eb4c Dawsh 2y ago keyboard = types.InlineKeyboardMarkup()
159 01cdf2e Dawsh 2y ago if sub_url[:4] == "http":
160 01cdf2e Dawsh 2y ago keyboard.add(
161 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(text="🚀 Subscription Page", url=sub_url)
162 01cdf2e Dawsh 2y ago )
163 a47eb4c Dawsh 2y ago return keyboard
164 a47eb4c Dawsh 2y ago
165 a47eb4c Dawsh 2y ago @staticmethod
166 a47eb4c Dawsh 2y ago def confirm_action(action: str, username: str = None):
167 a47eb4c Dawsh 2y ago keyboard = types.InlineKeyboardMarkup()
168 a47eb4c Dawsh 2y ago keyboard.add(
169 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
170 01cdf2e Dawsh 2y ago text="Yes", callback_data=f"{action}:{username}"
171 a47eb4c Dawsh 2y ago ),
172 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(text="No", callback_data=f"cancel"),
173 a47eb4c Dawsh 2y ago )
174 a47eb4c Dawsh 2y ago return keyboard
175 a47eb4c Dawsh 2y ago
176 a47eb4c Dawsh 2y ago @staticmethod
177 a47eb4c Dawsh 2y ago def inline_cancel_action(callback_data: str = "cancel"):
178 a47eb4c Dawsh 2y ago keyboard = types.InlineKeyboardMarkup()
179 a47eb4c Dawsh 2y ago keyboard.add(
180 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(text="🔙 Cancel", callback_data=callback_data)
181 a47eb4c Dawsh 2y ago )
182 a47eb4c Dawsh 2y ago return keyboard
183 a47eb4c Dawsh 2y ago
184 a47eb4c Dawsh 2y ago @staticmethod
185 a47eb4c Dawsh 2y ago def user_list(users: list, page: int, total_pages: int):
186 a47eb4c Dawsh 2y ago keyboard = types.InlineKeyboardMarkup()
187 a47eb4c Dawsh 2y ago if len(users) >= 2:
188 a47eb4c Dawsh 2y ago users = [p for p in users]
189 01cdf2e Dawsh 2y ago users = [users[i : i + 2] for i in range(0, len(users), 2)]
190 a47eb4c Dawsh 2y ago else:
191 a47eb4c Dawsh 2y ago users = [users]
192 a47eb4c Dawsh 2y ago for user in users:
193 a47eb4c Dawsh 2y ago row = []
194 a47eb4c Dawsh 2y ago for p in user:
195 a47eb4c Dawsh 2y ago status = {
196 01cdf2e Dawsh 2y ago "active": "✅",
197 01cdf2e Dawsh 2y ago "expired": "🕰",
198 01cdf2e Dawsh 2y ago "limited": "📵",
199 01cdf2e Dawsh 2y ago "disabled": "❌",
200 a47eb4c Dawsh 2y ago }
201 01cdf2e Dawsh 2y ago row.append(
202 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(
203 01cdf2e Dawsh 2y ago text=f"{p.username} ({status[p.status]})",
204 01cdf2e Dawsh 2y ago callback_data=f"user:{p.username}:{page}",
205 01cdf2e Dawsh 2y ago )
206 01cdf2e Dawsh 2y ago )
207 a47eb4c Dawsh 2y ago keyboard.row(*row)
208 a47eb4c Dawsh 2y ago # if there is more than one page
209 a47eb4c Dawsh 2y ago if total_pages > 1:
210 a47eb4c Dawsh 2y ago if page > 1:
211 a47eb4c Dawsh 2y ago keyboard.add(
212 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
213 01cdf2e Dawsh 2y ago text="⬅️ Previous", callback_data=f"users:{page - 1}"
214 a47eb4c Dawsh 2y ago )
215 a47eb4c Dawsh 2y ago )
216 a47eb4c Dawsh 2y ago if page < total_pages:
217 a47eb4c Dawsh 2y ago keyboard.add(
218 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
219 01cdf2e Dawsh 2y ago text="➡️ Next", callback_data=f"users:{page + 1}"
220 a47eb4c Dawsh 2y ago )
221 a47eb4c Dawsh 2y ago )
222 01cdf2e Dawsh 2y ago keyboard.add(types.InlineKeyboardButton(text="🔙 Back", callback_data="cancel"))
223 a47eb4c Dawsh 2y ago return keyboard
224 a47eb4c Dawsh 2y ago
225 a47eb4c Dawsh 2y ago @staticmethod
226 01cdf2e Dawsh 2y ago def select_services(
227 01cdf2e Dawsh 2y ago services,
228 01cdf2e Dawsh 2y ago selected_services: List[int],
229 01cdf2e Dawsh 2y ago action: Literal["edit", "create"],
230 01cdf2e Dawsh 2y ago username: str = None,
231 01cdf2e Dawsh 2y ago data_limit: float = None,
232 01cdf2e Dawsh 2y ago expire_date: dt = None,
233 01cdf2e Dawsh 2y ago ):
234 01cdf2e Dawsh 2y ago
235 a47eb4c Dawsh 2y ago keyboard = types.InlineKeyboardMarkup()
236 a47eb4c Dawsh 2y ago
237 a47eb4c Dawsh 2y ago if action == "edit":
238 a47eb4c Dawsh 2y ago keyboard.add(
239 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
240 01cdf2e Dawsh 2y ago text="⚠️ Data Limit:", callback_data=f"help_edit"
241 a47eb4c Dawsh 2y ago )
242 a47eb4c Dawsh 2y ago )
243 a47eb4c Dawsh 2y ago keyboard.add(
244 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
245 a47eb4c Dawsh 2y ago text=f"{readable_size(data_limit) if data_limit else 'Unlimited'}",
246 01cdf2e Dawsh 2y ago callback_data=f"help_edit",
247 a47eb4c Dawsh 2y ago ),
248 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
249 01cdf2e Dawsh 2y ago text="✏️ Edit", callback_data=f"edit_user:{username}:data"
250 01cdf2e Dawsh 2y ago ),
251 a47eb4c Dawsh 2y ago )
252 a47eb4c Dawsh 2y ago keyboard.add(
253 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
254 01cdf2e Dawsh 2y ago text="📅 Expire Date:", callback_data=f"help_edit"
255 a47eb4c Dawsh 2y ago )
256 a47eb4c Dawsh 2y ago )
257 a47eb4c Dawsh 2y ago keyboard.add(
258 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
259 a47eb4c Dawsh 2y ago text=f"{expire_date.strftime('%Y-%m-%d') if expire_date else 'Never'}",
260 01cdf2e Dawsh 2y ago callback_data=f"help_edit",
261 a47eb4c Dawsh 2y ago ),
262 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
263 01cdf2e Dawsh 2y ago text="✏️ Edit", callback_data=f"edit_user:{username}:expire"
264 01cdf2e Dawsh 2y ago ),
265 a47eb4c Dawsh 2y ago )
266 9bfa731 Dawsh 2y ago for s in services:
267 9bfa731 Dawsh 2y ago keyboard.add(
268 01cdf2e Dawsh 2y ago types.InlineKeyboardButton(
269 01cdf2e Dawsh 2y ago text=f"🌐 {s.name} {'✅' if s.id in selected_services else '❌'}",
270 01cdf2e Dawsh 2y ago callback_data=f"switch_service:{s.id}:{action}",
271 01cdf2e Dawsh 2y ago )
272 01cdf2e Dawsh 2y ago )
273 a47eb4c Dawsh 2y ago keyboard.add(
274 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
275 01cdf2e Dawsh 2y ago text="Done",
276 01cdf2e Dawsh 2y ago callback_data="edit_user_done" if action == "edit" else "add_user_done",
277 a47eb4c Dawsh 2y ago )
278 a47eb4c Dawsh 2y ago )
279 a47eb4c Dawsh 2y ago keyboard.add(
280 a47eb4c Dawsh 2y ago types.InlineKeyboardButton(
281 01cdf2e Dawsh 2y ago text="Cancel",
282 01cdf2e Dawsh 2y ago callback_data=f"user:{username}" if action == "edit" else "cancel",
283 a47eb4c Dawsh 2y ago )
284 a47eb4c Dawsh 2y ago )
285 a47eb4c Dawsh 2y ago
286 9bfa731 Dawsh 2y ago return keyboard