This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbot_functions.py
422 lines (352 loc) · 16.3 KB
/
bot_functions.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
import datetime
import json
import re
import inscriptions
from classes.cart import Cart
from classes.catalog import Catalog
from classes.order import Order
from database import *
import logging
log = logging.getLogger("bot_functions")
# Creating new Catalog
catalog = Catalog(None)
catalog_group = dict()
# Creating new making_order
making_order_group = dict()
def handler(bot, types, message, call):
creating_unique_catalog(message, call)
if message and message.text:
checking_messages(bot, message, types)
elif call and call.message:
checking_new_callback_data(bot, call, types)
def creating_unique_catalog(message, call):
global catalog
if message is not None or call is not None:
if message:
catalog = Catalog(message.chat.id)
elif call.message.chat.id:
catalog = Catalog(call.message.chat.id)
if message is not None or call is not None:
try:
if catalog_group[catalog.chat_id] is None:
log.debug(catalog_group)
except KeyError:
catalog_group[catalog.chat_id] = catalog
if message and catalog_group[catalog.chat_id] is not None:
catalog = catalog_group[message.chat.id]
elif call and catalog_group[catalog.chat_id] is not None:
catalog = catalog_group[call.message.chat.id]
def sending_start_message(bot, message, types):
markup = creating_start_markup_buttons(types)
with open("images/greeting/AnimatedSticker.tgs", "rb") as sticker:
bot.send_sticker(message.chat.id, sticker)
bot.send_message(message.chat.id, 'Привет, {0.first_name}, рады '.format(message.from_user) +
'видеть тебя в нашем боте!\nЭто магазин <b>' +
'{0.first_name}</b>.\nВыбирай товары в каталоге,'.format(bot.get_me()) +
' а затем оформляй заказ в ' +
'корзине :)', parse_mode='html', reply_markup=markup)
def start_func(message):
if not if_user_exists(message.chat.id):
new_user(message.chat.id, message.from_user.first_name, message.from_user.username, None)
def sending_help_message(bot, message):
log.info(f"{message.from_user.username} sent a /help command")
bot.send_message(message.chat.id, inscriptions.help_text, parse_mode='html')
def creating_start_markup_buttons(types):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
item1 = types.KeyboardButton(inscriptions.catalog)
item2 = types.KeyboardButton(inscriptions.cart)
item3 = types.KeyboardButton(inscriptions.search)
item4 = types.KeyboardButton(inscriptions.orders)
item5 = types.KeyboardButton(inscriptions.faq)
item6 = types.KeyboardButton(inscriptions.contacts)
markup.add(item1, item2, item3, item4, item5, item6)
return markup
def checking_messages(bot, message, types):
# Making order
if get_making_order_by_id(message.chat.id):
# global making_order_group
try:
steps = making_order_group[message.chat.id][0]
params = making_order_group[message.chat.id][1]
checking_order_creating_steps(bot, message, types, steps, params)
except KeyError:
making_order_group[message.chat.id] = [
{
"city": True,
"number_of_departament": False,
"full_name": False,
"number": False,
"payment_system": False
},
{
"city": None,
"number_of_departament": None,
"full_name": None,
"number": None,
"payment_system": None
}
]
steps = making_order_group[message.chat.id][0]
params = making_order_group[message.chat.id][1]
checking_order_creating_steps(bot, message, types, steps, params)
else:
# Main buttons
if message.text == inscriptions.catalog:
catalog_function(bot, message, types)
elif message.text == inscriptions.cart:
cart_function(bot, message, types)
elif message.text == inscriptions.search:
bot.send_message(message.chat.id, inscriptions.search_text, parse_mode='html')
elif message.text == inscriptions.orders:
orders_function(bot, types, message)
elif message.text == inscriptions.faq:
faq_function(bot, message)
elif message.text == inscriptions.contacts:
contacts_function(bot, message)
else:
bot.send_message(message.chat.id, inscriptions.unrecognized_message, parse_mode='html')
def checking_order_creating_steps(bot, message, types, steps, params):
if not steps["number_of_departament"]:
bot.send_message(message.chat.id, inscriptions.number_of_departament)
params["city"] = message.text
steps["number_of_departament"] = True
elif not steps["full_name"]:
bot.send_message(message.chat.id, inscriptions.full_name)
params["number_of_departament"] = message.text
steps["full_name"] = True
elif not steps["number"]:
bot.send_message(message.chat.id, inscriptions.number)
params["full_name"] = message.text
steps["number"] = True
elif not steps["payment_system"]:
bot.send_message(message.chat.id, inscriptions.payment_system, parse_mode='html')
params["number"] = message.text
steps["payment_system"] = True
else:
params["payment_system"] = message.text
cart = Cart(message.chat.id)
catalog.set_pre_order_params(params)
markup = create_pre_order_markup(types)
items_arr = make_items_array(message)
items_text = items_arr[0]
result_sum = items_arr[1]
params_text = ""
for k in params:
params_text += "<b>{0}: </b>{1}\n".format(inscriptions.params_text[k], params[k])
bot.send_message(message.chat.id, "{1}\n\n---\n{0}".format(items_text,
inscriptions.is_everything_right) +
"---\n\n{1}<b>Итого: </b>{0}".format(result_sum, params_text) +
inscriptions.currency, parse_mode='html', reply_markup=markup)
cart.pre_order_params = params
def create_pre_order_markup(types):
markup = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton(inscriptions.order_true_btn, callback_data="order_true")
item2 = types.InlineKeyboardButton(inscriptions.order_false_btn, callback_data="order_false")
markup.add(item1, item2)
return markup
def catalog_function(bot, message, types):
catalog_first_prod(bot, message, types)
def catalog_first_prod(bot, message, types):
try:
with open(catalog.products[catalog.current_prod][4], 'rb') as photo:
markup = catalog_markup_create(message.chat.id, catalog.products,
catalog.current_prod, catalog.prod_amount, types)
bot.send_photo(message.chat.id, photo,
"<b>{0[1]}</b>\n{0[2]}".format(catalog.products[catalog.current_prod]),
reply_markup=markup, parse_mode='html')
except IndexError as e:
print(e)
log.error("No products in catalog")
bot.send_message(message.chat.id, inscriptions.no_prods_in_catalog)
def catalog_update(bot, call, types):
markup = catalog_markup_create(call.message.chat.id, catalog.products,
catalog.current_prod, catalog.prod_amount, types)
bot.edit_message_caption(chat_id=call.message.chat.id, message_id=call.message.message_id,
caption="<b>{0[1]}</b>\n{0[2]}".format(catalog.products[catalog.current_prod]),
reply_markup=markup, parse_mode="html")
def catalog_markup_create(call, products, current_prod_catalog, last_prod, types):
products_in_cart = how_many_in_cart(call, products, current_prod_catalog)
markup = types.InlineKeyboardMarkup(row_width=1)
item = types.InlineKeyboardButton(products_in_cart + inscriptions.currency + " " +
str(products[current_prod_catalog][3]) + " " +
inscriptions.add_to_cart + " " +
products[current_prod_catalog][1], callback_data="to_cart")
markup.add(item)
markup.row_width = 3
item1 = types.InlineKeyboardButton("←", callback_data="prev")
item2 = types.InlineKeyboardButton(f"{current_prod_catalog + 1} / {last_prod + 1}", callback_data="nothing")
item3 = types.InlineKeyboardButton("→", callback_data="next")
markup.add(item1, item2, item3)
return markup
def how_many_in_cart(chat_id, products, current_prod_catalog):
cart = Cart(chat_id)
products_in_cart = cart.items
try:
cur_prod_amount = products_in_cart[str(products[current_prod_catalog][0])][7]["amount"]
cur_prod_amount_string = f"({cur_prod_amount}) "
return cur_prod_amount_string
except TypeError:
return ""
except KeyError:
return ""
def checking_new_callback_data(bot, call, types):
if call.message:
callback_data_catalog(bot, call, types)
callback_data_cart(bot, call)
callback_data_order(bot, call)
if get_making_order_by_id(call.message.chat.id):
callback_data_pre_order(bot, call)
def callback_data_catalog(bot, call, types):
if call.data == "to_cart":
callback_to_cart(call)
catalog_update(bot, call, types)
elif call.data == "next":
callback_next_prod()
catalog_update(bot, call, types)
elif call.data == "prev":
callback_prev_prod()
catalog_update(bot, call, types)
def callback_to_cart(call):
cart = Cart(call.message.chat.id)
cart.add_item(catalog.products[catalog.current_prod])
cart.set_cart_to_user()
def callback_next_prod():
if catalog.current_prod == catalog.prod_amount:
catalog.current_prod = 0
else:
catalog.current_prod += 1
def callback_prev_prod():
if catalog.current_prod == 0:
catalog.current_prod = catalog.prod_amount
else:
catalog.current_prod -= 1
def cart_function(bot, message, types):
items_array = make_items_array(message)
if items_array is not None:
items_text = items_array[0]
result_sum = items_array[1]
markup = cart_markup_create(types)
bot.send_message(message.chat.id, "<b>Корзина</b>\n\n---\n{0}".format(items_text) +
"---\n\n<b>Итого: </b>{0}".format(result_sum) +
inscriptions.currency, reply_markup=markup, parse_mode='html')
elif items_array is None:
bot.send_message(message.chat.id, inscriptions.cart_is_empty, parse_mode='html')
def make_items_array(message):
cart = Cart(message.chat.id)
items = cart.items
items_arr = list()
items_text = str()
items_sum = 0
try:
for item in items:
amount = items[item][7]['amount']
price = items[item][3]
local_sum = amount * price
items_text += f"<b>{items[item][1]}</b>\n{amount} " \
f"{inscriptions.amount} x {price}{inscriptions.currency} = {local_sum}\n"
items_sum += local_sum
if items_text == "":
return None
else:
items_arr.append(items_text)
items_arr.append(items_sum)
return items_arr
except TypeError:
return None
def cart_markup_create(types):
markup = types.InlineKeyboardMarkup(row_width=1)
item1 = types.InlineKeyboardButton(inscriptions.clear_cart, callback_data="clear_cart")
item2 = types.InlineKeyboardButton(inscriptions.make_order, callback_data="make_order")
markup.add(item1, item2)
return markup
def callback_data_cart(bot, call):
if call.data == "clear_cart":
callback_clear_cart(call)
cart_update(bot, call)
elif call.data == "make_order":
callback_make_order(bot, call)
cart_update(bot, call)
def cart_update(bot, call):
cart = Cart(call.message.chat.id)
if cart.items == {}:
bot.delete_message(call.message.chat.id, call.message.message_id)
bot.send_message(call.message.chat.id, inscriptions.cart_is_empty, parse_mode='html')
def callback_clear_cart(call):
cart = Cart(call.message.chat.id)
cart.items = None
cart.set_cart_to_user()
def callback_make_order(bot, call):
start_making_order(bot, call)
def start_making_order(bot, call):
bot.send_message(call.message.chat.id, inscriptions.city_of_dislocation)
set_making_order_status_to_user(call.message.chat.id, 1)
def callback_data_order(bot, call):
orders = Order(call.message.chat.id)
# If order in user`s order list
for order in orders.orders:
if call.data == order[0]:
bot.send_message(call.message.chat.id, order.return_items_note_str())
def orders_function(bot, types, message):
order = Order(message.chat.id)
if not order.is_orders():
bot.send_message(message.chat.id, inscriptions.no_orders_text)
elif order.is_orders():
markup = create_orders_markup(types, message)
bot.send_message(message.chat.id, inscriptions.some_orders_here, reply_markup=markup)
def create_orders_markup(types, message):
orders = Order(message.chat.id)
markup = types.InlineKeyboardMarkup(row_width=2)
for order in orders.orders:
item = types.InlineKeyboardButton(order[0], callback_data=order[0])
markup.add(item)
return markup
def callback_data_pre_order(bot, call):
if call.data == "order_true":
user = get_user_by_id(call.message.chat.id)
date = datetime.datetime.now()
date_id = re.search(r"[0-9]{4}-[0-9]{2}-[0-9]{2}", str(date))
date_id = date_id.group()
cart = Cart(call.message.chat.id)
params = catalog.pre_order_params
new_order(call.message.chat.id, json.dumps({
"Chat ID": call.message.chat.id,
"Phone": params["number"],
"Name": user[1],
"Username": user[2],
}), cart.return_cart_json(), date_id, 0, json.dumps(params))
set_phone_number_to_user(call.message.chat.id, params["number"])
set_making_order_status_to_user(call.message.chat.id, 0)
send_to_operators(bot, call.message)
bot.send_message(call.message.chat.id,
inscriptions.order_true.format(max(get_orders_ids_by_id(778508362))[0]))
set_making_order_status_to_user(call.message.chat.id, 0)
callback_clear_cart(call)
elif call.data == "order_false":
set_making_order_status_to_user(call.message.chat.id, 0)
catalog.pre_order_params = None
bot.send_message(call.message.chat.id, inscriptions.order_false)
def send_to_operators(bot, message):
# try:
user = get_user_by_id(message.chat.id)
params = catalog.pre_order_params
items_arr = make_items_array(message)
items_text = items_arr[0]
result_sum = items_arr[1]
order = get_orders_by_id(message.chat.id)[len(get_orders_by_id(message.chat.id)) - 1]
params_text = ""
for k in params:
params_text += "<b>{0}: </b>{1}\n".format(inscriptions.params_text[k], params[k])
user_info_str = "------------------------\n" + f"<b>Имя:</b> {user[1]}\n" + f"<b>Username:</b> {user[2]}\n" + \
f"<b>Номер телефона:</b> {user[3]}\n" + f"<b>Telegram order ID:</b> | {order[0]} |\n" + \
f"<b>Дата:</b> {order[4]}\n" + "-----\n" + \
f"Статус: {inscriptions.status[order[5]]}\n" + "-----\n\n"
for operator_id in get_operators():
bot.send_message(operator_id, user_info_str + "<b>Корзина</b>\n\n---\n{0}".format(items_text) +
"---\n\n{1}<b>Итого: </b>{0}".format(result_sum, params_text) +
inscriptions.currency, parse_mode='html')
# except TypeError as e:
# log.error(f"No operators! {e}")
def faq_function(bot, message):
bot.send_message(message.chat.id, inscriptions.faq_text, parse_mode='html')
def contacts_function(bot, message):
bot.send_message(message.chat.id, inscriptions.contacts_text, parse_mode='html')