-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_handler.py
141 lines (111 loc) · 6.68 KB
/
request_handler.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
from utils import *
from constants import *
from utils import keyboards
from telegram import *
from telegram.ext import *
done = "✅"
prev = "⏮"
next = "⏭"
comment = "💬"
cross = "❌"
trash = "🗑"
reload = "🔄"
class send_request_handler:
def req_type(self, update:Update, context:CallbackContext):
user = update.message.from_user
req_type_name = update.message.text
req_type = db.get_req_type(user.id, req_type_name)
context.user_data['request_confirmers'] = []
if req_type is not None:
context.user_data['req_type'] = req_type
update.message.reply_text(f"<b>Iltimos so'rov malumotlarini to'g'irlab yuboring!</b>\n\n{req_type['template'] if not None else ''}", reply_markup=ReplyKeyboardMarkup([["◀️ ortga"]], resize_keyboard=True), parse_mode=ParseMode.HTML)
return GET_TEMPLATE
else:
update.message.reply_text("Kechirasiz joriy so'rov turi topilmadi!")
def get_template_text(self, update:Update, context:CallbackContext):
user = update.message.from_user
req_template = update.message.text
context.user_data['req_template'] = req_template
if req_template == "◀️ ortga":
request_types = db.get_request_types(user.id)
if len(request_types['data']):
keys = ReplyKeyboardMarkup(distribute(
[d['name'] for d in request_types['data']], 2) + [[f"◀️ ortga"]], resize_keyboard=True)
update.message.reply_text(
"So'rov turini tanlang!", reply_markup=keys)
return SELECT_REQUEST_TYPE
else:
update.message.reply_text(
"Kechirasiz hozircha hechqanday so'rov turi mavjud emas!", reply_markup=keyboards.make_menu_keyboards())
return MENU
if update.message.text == "/cancel" or update.message.text == "/start":
return self.cancel_request_2(update, context)
user = update.message.from_user
request_type = context.user_data['req_type']
request_template = context.user_data['req_template']
update.message.reply_text("Iltimos so'rov to'g'riligini tasdiqlang!")
db.get_admins_by_list(request_type['confirmers'], user.id)
confers_text = ""
for conf in request_type['confirmers']:
confers_text += f"{conf['name']} (@{conf['username']})\n"
text = f"<b>so'rov turi</b>: {request_type['name']}\n<b>shablon:</b>\n<b>-------------------------------</b>\n{request_template}\n<b>-------------------------------</b>\n<b>tasdiqlovchilar</b>\n\n{confers_text}"
context.user_data['confirm_request_msgmsg'] = update.message.reply_text(text, reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton(f"{done} tasdiqlash", callback_data="temp_accept_request_true"),
InlineKeyboardButton(f"{cross} bekor qilish", callback_data="error_request_false")
]]), disable_web_page_preview=True, parse_mode=ParseMode.HTML)
return CHECK_REQUEST_TRUE_OR_FALSE
# db.create_request(user.id,request_type, request_template, request_confirmers)
def cancel_request(self,update:Update, context:CallbackContext):
user = update.callback_query.from_user
keys = keyboards.make_menu_keyboards()
update.callback_query.message.reply_text("Sizning so'rovingiz bekor qilindi!", reply_markup=keys)
return MENU
def confirm_request(self, update:Update, context:CallbackContext):
user = update.callback_query.from_user
request_type = context.user_data['req_type']
request_template = context.user_data['req_template']
confers_text = ""
for conf in request_type['confirmers']:
confers_text += f"{conf['name']} (@{conf['username']})\n"
new_req = db.create_request(user.id, request_type['id'], request_template)
new_db_req = msg_db.create_request_2(new_req['data']['id'])
text = format_request_to_text(new_req['data'])
for admin in request_type['confirmers']:
try:
msg_id = context.bot.send_message(text=f"<b>Yangi so'rov!</b>\n\n{text}\nyuboruvchi: {user.name}", chat_id=admin['chat_id'], reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton(f"{done} so'rovni tasdiqlash", callback_data=f"confirm_user_request:{new_req['data']['id']}"),
InlineKeyboardButton(f"{cross} so'rovni rad etish", callback_data=f"deny_user_request:{new_req['data']['id']}")
]]), parse_mode=ParseMode.HTML)
msg_db.create_message_2(new_db_req[0][1], msg_id.message_id, admin['chat_id'])
except Exception as e:
print(e)
for admin in request_type['groups']:
try:
msg_id = context.bot.send_message(text=f"<b>Yangi so'rov!</b>\n\n{text}\nyuboruvchi: {user.name}", chat_id=admin['chat_id'], reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton(f"{done} so'rovni tasdiqlash", callback_data=f"confirm_user_request:{new_req['data']['id']}"),
InlineKeyboardButton(f"{cross} so'rovni rad etish", callback_data=f"deny_user_request:{new_req['data']['id']}")
]]), parse_mode=ParseMode.HTML)
msg_db.create_message_2(new_db_req[0][1], msg_id.message_id, admin['chat_id'])
except Exception as e:
print(e)
context.user_data['confirm_request_msgmsg'].delete()
user.send_message("So'rovingiz yuborildi!")
msg_id = user.send_message(text, parse_mode=ParseMode.HTML, reply_markup=keyboards.make_menu_keyboards())
msg_db.create_message_2(new_db_req[0][1], msg_id.message_id, user.id)
return MENU
def error_request(self, update:Update, context:CallbackContext):
context.user_data['req_type'] = None
context.user_data['req_template'] = ""
user = update.callback_query.message.from_user
request_types = db.get_request_types(user.id)
keys = ReplyKeyboardMarkup(distribute(
[d['name'] for d in request_types['data']], 2) + [[f"◀️ ortga"]], resize_keyboard=True)
update.callback_query.message.reply_text("Iltimos so'rov turini qaytadan tanlang!", reply_markup=keys)
context.user_data['confirm_request_msgmsg'].delete()
return SELECT_REQUEST_TYPE
def cancel_request_2(self, update:Update, context:CallbackContext):
context.user_data['req_type'] = None
context.user_data['req_template'] = ""
update.message.reply_text("Bosh menu", reply_markup=keyboards.make_menu_keyboards())
return MENU
send_request_handler = send_request_handler()