-
Notifications
You must be signed in to change notification settings - Fork 4
/
tg_bot.py
490 lines (420 loc) · 16.6 KB
/
tg_bot.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
import json
import logging
import os
import redis
from dotenv import load_dotenv
from telegram import (
InlineKeyboardButton,
InlineKeyboardMarkup,
KeyboardButton,
LabeledPrice,
ParseMode,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
)
from telegram.ext import (
CallbackQueryHandler,
CommandHandler,
Filters,
MessageHandler,
PreCheckoutQueryHandler,
Updater,
)
import moltin
import utils
def get_database():
db_url = os.getenv('REDIS_URL')
db_port = os.getenv('REDIS_PORT')
db_password = os.getenv('REDIS_PASSWORD')
database = redis.Redis(
host=db_url,
port=db_port,
password=db_password,
charset='utf-8',
decode_responses=True,
)
return database
def create_chunks(products, size=7):
for i in range(0, len(products), size):
yield products[i: i + size]
def send_order_to_deliverer(context, order_id, deliverer, longitude, latitude):
order_text = 'Офомлен заказ на:\n\n'
order_text += moltin.format_basket_for_sending(order_id)
order_text += '\n\nКоординаты клиента:'
context.bot.send_message(
chat_id=deliverer, text=order_text, parse_mode=ParseMode.MARKDOWN
)
context.bot.send_location(chat_id=deliverer, longitude=longitude, latitude=latitude)
def create_menu_buttons(chunk):
products = moltin.get_products()
chunks = list(create_chunks(products))
last_chunk = len(chunks) - 1
keyboard = [
[InlineKeyboardButton(pr_name, callback_data=pr_id)]
for pr_id, pr_name in chunks[chunk]
]
if chunk == 0:
keyboard.append([InlineKeyboardButton("Следующие", callback_data='next')])
elif chunk == last_chunk:
keyboard.append([InlineKeyboardButton("Предыдущие", callback_data='prev')])
else:
keyboard.append(
[
InlineKeyboardButton("Предыдущие", callback_data='prev'),
InlineKeyboardButton("Следующие", callback_data='next'),
]
)
keyboard.append([InlineKeyboardButton("Корзина", callback_data='basket')])
return InlineKeyboardMarkup(keyboard)
def create_description_buttons():
buttons_list = [['1 шт', 'cart 1'], ['3 шт', 'cart 3'], ['5 шт', 'cart 5']]
keyboard = [
[
InlineKeyboardButton(button_name, callback_data=button_id)
for button_name, button_id in buttons_list
]
]
keyboard.append([InlineKeyboardButton('Назад', callback_data='back_to_menu')])
keyboard.append([InlineKeyboardButton("Корзина", callback_data='basket')])
return InlineKeyboardMarkup(keyboard)
def create_basket_buttons(user_id=None):
user_basket = moltin.get_cart(user_id)
keyboard = []
for product in user_basket:
pr_name = product['name']
pr_id = product['cart_id']
keyboard.append(
[InlineKeyboardButton(f'Удалить {pr_name}', callback_data=pr_id)]
)
keyboard.append(
[InlineKeyboardButton("Назад в меню", callback_data='back_to_menu')]
)
keyboard.append([InlineKeyboardButton("Оформить заказ", callback_data='sell')])
return InlineKeyboardMarkup(keyboard)
def create_delivery_buttons(distance):
if distance <= 20:
keyboard = [
[
InlineKeyboardButton('Доставка', callback_data='delivery'),
InlineKeyboardButton('Самовывоз', callback_data='pickup'),
]
]
else:
keyboard = [[InlineKeyboardButton('Самовывоз', callback_data='pickup')]]
return InlineKeyboardMarkup(keyboard)
def start(update, context):
chat_id = update.message.chat_id
keyboard = create_menu_buttons(chunk=0)
user_data = json.loads(db.get(chat_id))
user_data['last_chunk'] = 0
db.set(chat_id, json.dumps(user_data))
context.bot.send_message(
chat_id=chat_id,
text='_Пожалуйста, выберите пиццу или много пицц :):_',
reply_markup=keyboard,
parse_mode=ParseMode.MARKDOWN,
)
return 'HANDLE_MENU'
def handle_button(update, context):
query = update.callback_query
chat_id = query.message.chat_id
message_id = query.message.message_id
user_data = json.loads(db.get(chat_id))
chunk = user_data['last_chunk']
if query.data == 'basket':
keyboard = create_basket_buttons(user_id=chat_id)
message = moltin.format_basket_for_sending(chat_id)
context.bot.send_message(
chat_id=chat_id,
text=message,
reply_markup=keyboard,
parse_mode=ParseMode.MARKDOWN,
)
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
return 'HANDLE_BASKET'
elif query.data == 'prev':
keyboard = create_menu_buttons(chunk=chunk - 1)
context.bot.send_message(
chat_id=chat_id,
text='_Пожалуйста, выберите пиццу или много пицц :):_',
reply_markup=keyboard,
parse_mode=ParseMode.MARKDOWN,
)
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
user_data['last_chunk'] -= 1
db.set(chat_id, json.dumps(user_data))
return 'HANDLE_MENU'
elif query.data == 'next':
keyboard = create_menu_buttons(chunk=chunk + 1)
context.bot.send_message(
chat_id=chat_id,
text='_Пожалуйста, выберите пиццу или много пицц :):_',
reply_markup=keyboard,
parse_mode=ParseMode.MARKDOWN,
)
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
user_data['last_chunk'] += 1
db.set(chat_id, json.dumps(user_data))
return 'HANDLE_MENU'
else:
pizza_data = moltin.get_by_id(query.data)
pizza_name = pizza_data['name']
pizza_text = pizza_data['description']
pizza_price = pizza_data['meta']['display_price']['with_tax']['formatted']
image_id = pizza_data['relationships']['main_image']['data']['id']
image_url = db.get(image_id) or moltin.get_picture(image_id)
basket_message = moltin.check_product_in_cart(chat_id, query.data) or ''
keyboard = create_description_buttons()
user_data = json.loads(db.get(chat_id))
user_data['last_product'] = query.data
db.set(chat_id, json.dumps(user_data))
message = f'*{pizza_name}*\n\n{pizza_text}\n\n_Цена {pizza_price}_\n\n{basket_message}'
context.bot.send_photo(
chat_id=chat_id,
photo=image_url,
caption=message,
reply_markup=keyboard,
parse_mode=ParseMode.MARKDOWN,
)
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
return 'HANDLE_DESCRIPTION'
def handle_description(update, context):
query = update.callback_query
chat_id = query.message.chat_id
message_id = query.message.message_id
user_data = json.loads(db.get(chat_id))
product_id = user_data['last_product']
if query.data == 'back_to_menu':
user_data = json.loads(db.get(chat_id))
chunk = user_data['last_chunk']
keyboard = create_menu_buttons(chunk=chunk)
context.bot.send_message(
chat_id=chat_id,
text='_Пожалуйста, выберите пиццу или много пицц :):_',
reply_markup=keyboard,
parse_mode=ParseMode.MARKDOWN,
)
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
return 'HANDLE_MENU'
elif query.data == 'basket':
keyboard = create_basket_buttons(user_id=chat_id)
message = moltin.format_basket_for_sending(chat_id)
context.bot.send_message(
chat_id=chat_id,
text=message,
reply_markup=keyboard,
parse_mode=ParseMode.MARKDOWN,
)
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
return 'HANDLE_BASKET'
elif query.data.split()[0] == 'cart':
quantity = int(query.data.split()[1])
moltin.put_in_cart(chat_id, product_id, quantity)
print(chat_id, product_id)
context.bot.answer_callback_query(
callback_query_id=query.id, text='Добавили в корзину!'
)
return 'HANDLE_DESCRIPTION'
def handle_basket(update, context):
query = update.callback_query
chat_id = query.message.chat_id
message_id = query.message.message_id
if query.data == 'back_to_menu':
keyboard = create_menu_buttons(chunk=0)
context.bot.send_message(
chat_id=chat_id,
text='_Пожалуйста, выберите пиццу или много пицц :):_',
reply_markup=keyboard,
parse_mode=ParseMode.MARKDOWN,
)
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
return 'HANDLE_MENU'
elif query.data == 'sell':
location_keyboard = KeyboardButton(
text='Отправить свою геолокацию', request_location=True
)
custom_keyboard = [[location_keyboard]]
reply_markup = ReplyKeyboardMarkup(custom_keyboard)
context.bot.send_message(
chat_id=chat_id,
text='Отправить геолокацию можно только с телефона',
reply_markup=reply_markup,
)
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
return 'WAITING_GEO'
else:
moltin.delete_item_in_cart(chat_id, query.data)
message = moltin.format_basket_for_sending(chat_id)
keyboard = create_basket_buttons(user_id=chat_id)
context.bot.answer_callback_query(
callback_query_id=query.id, text='Удалили из корзины!'
)
context.bot.send_message(
chat_id=chat_id,
text=message,
reply_markup=keyboard,
parse_mode=ParseMode.MARKDOWN,
)
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
return 'HANDLE_BASKET'
def handle_waiting(update, context):
message = update.message
chat_id = message.chat_id
if message.text:
try:
current_pos = utils.fetch_coordinates(message.text)
except IndexError as error:
logging.error(error)
update.message.reply_text(
text='Не смогли определить адрес, попробуйте еще.'
)
current_pos = None
else:
current_pos = (message.location.longitude, message.location.latitude)
if current_pos is None:
return 'WAITING_GEO'
closest_pizzeria = utils.get_closest_pizzeria(db, current_pos)
message, dist = utils.calculate_distance_for_message(closest_pizzeria)
update.message.reply_text(
text='Данные приняты, спасибо.', reply_markup=ReplyKeyboardRemove()
)
keyboard = create_delivery_buttons(dist)
context.bot.send_message(chat_id=chat_id, text=message, reply_markup=keyboard)
user_data = json.loads(db.get(chat_id))
user_data['closest_pizzeria'] = closest_pizzeria
user_data['customer_geo'] = current_pos
db.set(chat_id, json.dumps(user_data))
return 'WAITING_CHOOSING'
def handle_delivery_choosing(update, context):
query = update.callback_query
chat_id = query.message.chat_id
message_id = query.message.message_id
user_name = query.message.chat.first_name
user_data = json.loads(db.get(chat_id))
closest_pizzeria = user_data['closest_pizzeria']
customer_geo = user_data['customer_geo']
pizzeria_name = closest_pizzeria['alias']
pizzeria_address = closest_pizzeria['address']
pizzeria_id = closest_pizzeria['id']
customer_longitude, customer_latitude = customer_geo
if query.data == 'pickup':
context.bot.send_message(
chat_id=chat_id,
text=f'Отлично! Вы можете забрать ваш заказ в ресторане {pizzeria_name} по адресу: {pizzeria_address}\n\nУдачного дня и заходите еще!',
)
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
moltin.create_customer_entry(
chat_id, pizzeria_name, customer_longitude, customer_latitude
)
elif query.data == 'delivery':
context.bot.send_message(
chat_id=chat_id, text=f'Ваш заказ принят, ожидаем оплату.'
)
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
moltin.create_customer_entry(
chat_id, user_name, customer_longitude, customer_latitude
)
deliverer = moltin.get_deliverer(pizzeria_id)
send_order_to_deliverer(
context, chat_id, deliverer, customer_longitude, customer_latitude
)
create_invoice(context, chat_id=chat_id)
job_queue.run_once(remind_about_order, 3600, context=chat_id)
def remind_about_order(context):
message = 'Приятного аппетита!\n\n'
message += (
'Но если заказ не пришел в течении этого часа, следующий заказ за наш счет:('
)
context.bot.send_message(chat_id=context.job.context, text=message)
def create_invoice(context, chat_id):
user_total = moltin.get_total(chat_id)
amount = user_total.split()[0].split('.')[0].replace(',', '')
title = "Оплата заказа"
description = "Пожалуйста, нажмите, чтобы оплатить заказ."
payload = os.getenv('PAYMENT_PAYLOAD')
provider_token = os.getenv('TG_TRANZZO_TOKEN')
start_parameter = 'payment'
currency = "RUB"
prices = [LabeledPrice("Оплатить заказ", int(amount) * 100)]
context.bot.sendInvoice(
chat_id,
title,
description,
payload,
provider_token,
start_parameter,
currency,
prices,
)
def handle_invoice(update, context):
payload = os.getenv('PAYMENT_PAYLOAD')
query = update.pre_checkout_query
if query.invoice_payload != payload:
context.bot.answer_pre_checkout_query(
pre_checkout_query_id=query.id,
ok=False,
error_message='Что-то пошло не так...',
)
else:
context.bot.answer_pre_checkout_query(pre_checkout_query_id=query.id, ok=True)
def handle_successful_payment(update, context):
update.message.reply_text("Спасибо, мы получили ваш платеж!")
def handle_users_reply(update, context):
if update.message:
user_reply = update.message.text
chat_id = update.message.chat_id
elif update.callback_query:
user_reply = update.callback_query.data
chat_id = update.callback_query.message.chat_id
else:
return
if user_reply == '/start':
user_state = 'START'
else:
user = db.get(chat_id)
user_state = json.loads(user)['state']
if user_state == 'START':
user = {}
user["state"] = 'START'
db.set(chat_id, json.dumps(user))
states_functions = {
'START': start,
'HANDLE_MENU': handle_button,
'HANDLE_DESCRIPTION': handle_description,
'HANDLE_BASKET': handle_basket,
'WAITING_GEO': handle_waiting,
'WAITING_CHOOSING': handle_delivery_choosing,
}
state_handler = states_functions[user_state]
try:
next_state = state_handler(update, context)
except Exception as error:
logging.exception(error)
next_state = None
if next_state is not None:
user = db.get(chat_id)
if user:
user_data = json.loads(user)
user_data['state'] = next_state
else:
user_data = {'state': next_state}
db.set(chat_id, json.dumps(user_data))
if __name__ == "__main__":
load_dotenv()
tg_token = os.getenv('TG_TOKEN')
global db
db = get_database()
updater = Updater(tg_token, use_context=True)
dispatcher = updater.dispatcher
job_queue = updater.job_queue
dispatcher.add_handler(CommandHandler('start', handle_users_reply))
dispatcher.add_handler(CallbackQueryHandler(handle_users_reply))
dispatcher.add_handler(
MessageHandler(Filters.text | Filters.location, handle_users_reply)
)
dispatcher.add_handler(PreCheckoutQueryHandler(handle_invoice))
dispatcher.add_handler(
MessageHandler(Filters.successful_payment, handle_successful_payment)
)
updater.start_polling()
updater.idle()