-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard_callbacks.py
517 lines (466 loc) Β· 15.2 KB
/
dashboard_callbacks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
import logging
import re
from typing import Dict
from html import escape
from uuid import uuid4
from bson.objectid import ObjectId
import datetime
from random import randint
from telegram import (
InlineKeyboardButton,
InlineKeyboardMarkup,
Update,
constants,
InlineQueryResultArticle,
InputTextMessageContent,
)
from telegram.ext import (
ApplicationHandlerStop,
ConversationHandler,
ContextTypes,
)
from db_connection import uc
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
# for monitoring activities
logger = logging.getLogger(__name__)
# define states
# conv_dashboard state variables
# Pages
DASH_PAGE, FAV_PAGE = range(2)
# Callback data
DASH, FAV, SEARCH_CRED, ADD_CRED = range(4)
# FAV callback data
ONE, TWO, THREE, FOUR, FIVE, SIX = range(6)
def format_btns(fav_len, favs):
fav_len = fav_len
keyboard = []
if fav_len == 1:
keyboard.append(
[
InlineKeyboardButton(
f"{favs[0]}",
switch_inline_query_current_chat=f"π: {favs[0]}",
),
],
)
elif (fav_len % 2) == 1:
count = 0
for i in range(int((fav_len - 1) / 2)):
keyboard.append(
[
InlineKeyboardButton(
f"{favs[count]}",
switch_inline_query_current_chat=f"π: {favs[count]}",
),
InlineKeyboardButton(
f"{favs[count+1]}",
switch_inline_query_current_chat=f"π: {favs[count+1]}",
),
],
)
count += 2
keyboard.append(
[
InlineKeyboardButton(
f"{favs[count]}",
switch_inline_query_current_chat=f"π: {favs[count]}",
),
],
)
elif (fav_len % 2) == 0:
count = 0
for i in range(int(fav_len / 2)):
keyboard.append(
[
InlineKeyboardButton(
f"{favs[count]}",
switch_inline_query_current_chat=f"π: {favs[count]}",
),
InlineKeyboardButton(
f"{favs[count+1]}",
switch_inline_query_current_chat=f"π: {favs[count+1]}",
),
],
)
count += 2
return keyboard
def format_cred_query(cred_data: Dict[str, str]) -> str:
"""Hepler function for formatting the credential data"""
text = f"""
<b>Name</b>: {cred_data["name"]}
<b>Username</b>: {cred_data["username"]}
<b>Password</b>: <code>{escape(cred_data["password"])}</code>
<b>URI</b>: {cred_data["uri"]}
<b>Tags</b>: {" ".join(cred_data["tags"])}
<b>Note</b>: {cred_data["note"]}
<b>Date</b>: {cred_data["date"]}
"""
return text
async def dash(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
query = update.callback_query
if not query:
user = update.message.from_user
logger.info(f"{user.first_name} has accessed dashbooard via command")
doc = uc.find_one(
{"user_id": f"{user.id}"},
{"_id": 0, "credentials": 1, "favourites": 1},
)
context.user_data["credentials"] = doc["credentials"]
context.user_data["favourites"] = doc["favourites"]
keyboard = [
[
InlineKeyboardButton("β FAV", callback_data="fav"),
InlineKeyboardButton(
"π SEARCH",
switch_inline_query_current_chat=str("π: "),
),
],
[
InlineKeyboardButton(
"π GENERATE PASSWORD",
switch_inline_query_current_chat=str(randint(9, 25)),
),
],
[
InlineKeyboardButton(
"π ADD CREDENTIAL",
callback_data="add_cred",
),
],
]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text(
f"""β’ββΰΌ»ββ’β’Dashboardβ’β’βΰΌΊβββ’
<b>Credentials</b>: {len(context.user_data["credentials"])}
<b>Favourites</b>: {len(context.user_data["favourites"])}
<b>Notes</b>: Coming Soon.
<b>Tags</b>: Coming Soon.
<b>Trash</b>: Coming Soon.
""",
reply_markup=reply_markup,
parse_mode=constants.ParseMode.HTML,
)
raise ApplicationHandlerStop
else:
user = query.from_user
logger.info(f"{user.first_name} has accessed dashbooard via button")
doc = uc.find_one(
{"user_id": f"{user.id}"},
{"_id": 0, "credentials": 1, "favourites": 1},
)
context.user_data["credentials"] = doc["credentials"]
context.user_data["favourites"] = doc["favourites"]
keyboard = [
[
InlineKeyboardButton("β FAV", callback_data="fav"),
InlineKeyboardButton(
"π SEARCH",
switch_inline_query_current_chat=str("π: "),
),
],
[
InlineKeyboardButton(
"π GENERATE PASSWORD",
switch_inline_query_current_chat=str(randint(9, 25)),
),
],
[
InlineKeyboardButton(
"π ADD CREDENTIAL",
callback_data="add_cred",
),
],
]
reply_markup = InlineKeyboardMarkup(keyboard)
await query.edit_message_text(
f"""β’ββΰΌ»ββ’β’Dashboardβ’β’βΰΌΊβββ’
<b>Credentials</b>: {len(context.user_data["credentials"])}
<b>Favourites</b>: {len(context.user_data["favourites"])}
<b>Notes</b>: Coming Soon.
<b>Tags</b>: Coming Soon.
<b>Trash</b>: Coming Soon.
""",
reply_markup=reply_markup,
parse_mode=constants.ParseMode.HTML,
)
raise ApplicationHandlerStop
async def fav(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Favourite credentials"""
query = update.callback_query
user = query.from_user
logger.info(f"{user.first_name} is accessing favorites")
doc = uc.find_one(
{"user_id": f"{user.id}"},
{"favourites": 1},
)
context.user_data["favourites"] = doc["favourites"]
favs = context.user_data["favourites"]
await query.answer()
fav_len = len(favs)
if fav_len == 0:
keyboard = [
[
InlineKeyboardButton(
"<< BACK",
callback_data="dash",
),
]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await query.edit_message_text(
"Sorry you do not have any favourites:",
reply_markup=reply_markup,
)
return ConversationHandler.END
else:
keyboard = format_btns(fav_len, favs)
keyboard.append(
[
InlineKeyboardButton(
"<< BACK",
callback_data="dash",
),
],
)
reply_markup = InlineKeyboardMarkup(keyboard)
await query.edit_message_text(
"Here are your favourites:",
reply_markup=reply_markup,
)
return ConversationHandler.END
# async def fav_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
# query = update.callback_query
# user = query.from_user
# context.user_data["collection"] = uc.find_one(
# {"user_id": f"{user.id}"}
# )
# creds = context.user_data.collection["credentials"]
# favs = context.user_data.collection["favourites"]
# fav_len = len(favs)
# logger.info(f"{user.first_name} has accessed dashboard-fav from query")
# await query.answer()
# keys = [
# [
# InlineKeyboardButton(f"{favs[0]}", callback_data=str(ONE)),
# InlineKeyboardButton(f"{favs[1]}", callback_data=str(TWO)),
# ],
# [
# InlineKeyboardButton(f"{favs[2]}", callback_data=str(THREE)),
# InlineKeyboardButton(f"{favs[3]}", callback_data=str(FOUR)),
# ],
# [
# InlineKeyboardButton(f"{favs[4]}", callback_data=str(FIVE)),
# InlineKeyboardButton(f"{favs[5]}", callback_data=str(SIX)),
# ],
# ]
# keyboard = format_btns(fav_len, keys)
# keyboard.append(
# [
# InlineKeyboardButton(
# "<< BACK",
# callback_data=str(DASH),
# ),
# ],
# )
# reply_markup = InlineKeyboardMarkup(keyboard)
# await query.edit_message_text(
# """Here are your favourites:
# """,
# reply_markup=reply_markup,
# )
# return FAV_PAGE
async def done_query(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Done with credential"""
query = update.callback_query
user = query.from_user
logger.info(f"{user.first_name} is done with credential")
await query.answer()
keyboard = [
[
InlineKeyboardButton(
"π Add Credential",
callback_data="add_cred",
),
InlineKeyboardButton(
"π Search",
switch_inline_query_current_chat="π: ",
),
],
[
InlineKeyboardButton(
"Dashboard",
callback_data="dash",
),
],
]
# await update.message.delete()
await query.edit_message_text(
text="All done!",
reply_markup=InlineKeyboardMarkup(keyboard),
)
context.user_data.clear()
return ConversationHandler.END
# get_cred
async def get_cred(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
"""Search for credential"""
keyboard = [
[
InlineKeyboardButton(
"π Search",
switch_inline_query_current_chat="π: ",
),
],
]
await update.message.delete()
await update.message.reply_text(
text="click π to search.",
reply_markup=InlineKeyboardMarkup(keyboard),
)
raise ApplicationHandlerStop
# async def chosen_cred(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
# chosen_result = update.chosen_inline_result
# chosen_id = chosen_result.inline_message_id
# user = chosen_result.from_user
# logger.info(f"{chosen_id}")
# credentials = context.user_data["credentials"]
# print(f"These are the credentials:\n{credentials}\n")
# cred_result = {}
# for i in credentials:
# if str(i["_id"]) == chosen_id:
# cred_result = i
# break
# logger.info(f"\n\n{cred_result}\n\n")
# keyboard = [
# [
# InlineKeyboardButton(
# "EDIT",
# callback_data="edit_cred",
# ),
# InlineKeyboardButton(
# "DELETE",
# callback_data="delete_cred",
# ),
# ],
# [
# InlineKeyboardButton(
# "ADD TO FAV",
# callback_data="fav_cred",
# ),
# ],
# [
# InlineKeyboardButton(
# "DONE",
# callback_data="done",
# ),
# ],
# ]
# await user.send_message(
# format_cred_query(cred_result),
# parse_mode=constants.ParseMode.HTML,
# reply_markup=InlineKeyboardMarkup(keyboard),
# )
# return ConversationHandler.END
async def inline_credential(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
query = update.inline_query.query
query_user_id = update.inline_query.from_user.id
if not re.search("^π: .*$", query):
return
query = re.split(" ", query, 1)[1]
doc = uc.find_one(
{"user_id": f"{query_user_id}"}, {"_id": 0, "credentials": 1, "favourites": 1}
)
context.user_data["credentials"] = doc["credentials"]
context.user_data["favourites"] = doc["favourites"]
credentials = context.user_data["credentials"]
print(f"These are the credentials:\n{credentials}\n")
favs = context.user_data["favourites"]
print(f"These are the credentials:\n{favs}\n")
cred_results = []
for i in credentials:
if re.search(f"^{query}.*$", i["name"], re.IGNORECASE):
cred_results.append(i)
logger.info(f"\n\n{cred_results}\n\n")
results = []
for i in cred_results:
if i["name"] in favs:
keyboard_fav = [
[
InlineKeyboardButton(
"EDIT",
callback_data=f"edit_{str(i['_id'])}",
),
InlineKeyboardButton(
"DELETE",
callback_data=f"del_{str(i['_id'])}",
),
],
[
InlineKeyboardButton(
"REMOVE FROM FAV",
callback_data="defav_cred",
),
],
[
InlineKeyboardButton(
"DONE",
callback_data="done",
),
],
]
results.append(
InlineQueryResultArticle(
id=str(i["_id"]),
title=f"{i['name']}",
description=f"{i['username']}",
input_message_content=InputTextMessageContent(
format_cred_query(i),
parse_mode=constants.ParseMode.HTML,
),
reply_markup=InlineKeyboardMarkup(keyboard_fav),
),
)
else:
keyboard = [
[
InlineKeyboardButton(
"EDIT",
callback_data=f"edit_{str(i['_id'])}",
),
InlineKeyboardButton(
"DELETE",
callback_data=f"del_{str(i['_id'])}",
),
],
[
InlineKeyboardButton(
"ADD TO FAV",
callback_data="fav_cred",
),
],
[
InlineKeyboardButton(
"DONE",
callback_data="done",
),
],
]
results.append(
InlineQueryResultArticle(
id=str(i["_id"]),
title=f"{i['name']}",
description=f"{i['username']}",
input_message_content=InputTextMessageContent(
format_cred_query(i),
parse_mode=constants.ParseMode.HTML,
),
reply_markup=InlineKeyboardMarkup(keyboard),
),
)
await update.inline_query.answer(
results,
cache_time=0,
is_personal=True,
)