-
Notifications
You must be signed in to change notification settings - Fork 0
/
info_handlers.py
83 lines (64 loc) · 2.12 KB
/
info_handlers.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
# File for handlers which provide information
import pandas as pd
from setup_database import open_close_database
from telegram.ext import CommandHandler
def get_info_handlers():
return [
CommandHandler('my_stickers', my_stickers),
CommandHandler('my_packs', my_packs)
]
@open_close_database
def my_stickers(update, context, mydb, mycursor):
user_id = update.message.from_user.id
sql = '''
select
t2.pack_name,
t3.sticker_shortcut
from user_packs t1
inner join pack_info t2
on true
and t1.user_id = %s
and t1.pack_id = t2.pack_id
inner join pack_stickers t3
on t1.pack_id = t3.pack_id
;'''
val = (user_id,)
mycursor.execute(sql, val)
pack_shortcut = mycursor.fetchall()
pack_sticker_list = pd.DataFrame(
pack_shortcut,
columns=['pack_name', 'sticker_shortcut']).groupby('pack_name')[
'sticker_shortcut'
].apply(lambda x: x.tolist()).reset_index().values.tolist()
answer = '\n\n'.join(
f'<b>{pack}</b>:\n' + '\n'.join(stickers)
for pack, stickers in pack_sticker_list)
update.message.reply_text(
f'These are stickers you can currently use \
sorted by packs:\n\n{answer}',
parse_mode='html')
@open_close_database
def my_packs(update, context, mydb, mycursor):
user_id = update.message.from_user.id
answer = get_user_packs(user_id=user_id)
update.message.reply_text(
f'These are names and ids of packs you can currently use:\n\n{answer}')
@open_close_database
def get_user_packs(user_id, mydb, mycursor):
sql = '''
select
t1.pack_id,
t2.pack_name
from user_packs t1
inner join pack_info t2
on true
and t1.user_id = %s
and t1.pack_id = t2.pack_id
'''
val = (user_id,)
mycursor.execute(sql, val)
pack_id_name = mycursor.fetchall()
answer = '\n'.join([
f'{pack_name}: {pack_id}' for pack_id, pack_name in pack_id_name
])
return answer