-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_bot.py
405 lines (311 loc) · 11.2 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
import os
from textwrap import dedent
from dotenv import load_dotenv
from telegram import LabeledPrice
from telegram.ext import (
CallbackQueryHandler,
CommandHandler,
ConversationHandler,
Filters,
MessageHandler,
PicklePersistence,
PreCheckoutQueryHandler,
Updater,
)
from api.moltin_requests import (
add_product_to_cart,
create_customer,
fetch_cart_items,
fetch_products,
fetch_product_by_id,
remove_cart_item_by_id,
upload_entry,
EntityExistsError,
)
from api.yandex_api_requests import fetch_coordinates
from helpers.tg_chat_replying import (
send_cart,
send_delivery_options,
send_order_details,
send_product_details,
send_products,
)
from helpers.delivery import (
calculate_delivery_cost,
find_nearest_pizzeria,
get_order_details_for_invoice,
notify_about_pizza,
)
from helpers.token_handers import AuthToken
(
HANDLE_MENU,
HANDLE_DESCRIPTION,
HANDLE_CART,
WAIT_EMAIL,
HANDLE_COORDINATES,
HANDLE_PAYMENT,
) = range(6)
def start(update, context):
"""Start handler.
Returns:
next bot state for user
"""
chat = update.message
chat.bot.delete_message(chat.chat_id, message_id=chat.message_id)
auth_token = context.bot_data['auth_token'].token
products = fetch_products(auth_token)
send_products(products, chat)
return HANDLE_MENU
def handle_menu(update, context):
"""Products menu handler.
Returns:
next bot state for user
"""
query = update.callback_query.data
chat = update.callback_query.message
chat.bot.delete_message(chat.chat_id, message_id=chat.message_id)
auth_token = context.bot_data['auth_token'].token
if query == 'Cart':
cart = fetch_cart_items(auth_token, 'pizza_{}'.format(chat.chat_id))
send_cart(cart, chat)
return HANDLE_CART
product = fetch_product_by_id(auth_token, query)['data']
send_product_details(product, chat, auth_token)
return HANDLE_DESCRIPTION
def handle_description(update, context):
"""Product description handler.
Returns:
next bot state for user
"""
query = update.callback_query.data
chat = update.callback_query.message
chat.bot.delete_message(chat.chat_id, message_id=chat.message_id)
auth_token = context.bot_data['auth_token'].token
if query == 'Back to menu':
products = fetch_products(auth_token)
send_products(products, chat)
return HANDLE_MENU
if query == 'Cart':
cart = fetch_cart_items(auth_token, f'tg_pizza_{chat.chat_id}')
send_cart(cart, chat)
return HANDLE_CART
product_id, quantity = query.split(';')
cart = add_product_to_cart(
auth_token,
'pizza_{}'.format(chat.chat_id),
product_id,
int(quantity),
)
send_cart(cart, chat)
return HANDLE_CART
def handle_cart(update, context):
"""Client cart handler.
Returns:
next bot state for user
"""
query = update.callback_query.data
chat = update.callback_query.message
chat.bot.delete_message(chat.chat_id, message_id=chat.message_id)
auth_token = context.bot_data['auth_token'].token
if query == 'Back to menu':
products = fetch_products(auth_token)
send_products(products, chat)
return HANDLE_MENU
if query == 'Pay':
chat.reply_text('Введите ваш email')
return WAIT_EMAIL
cart = remove_cart_item_by_id(
auth_token,
'pizza_{}'.format(chat.chat_id),
query,
)
send_cart(cart, chat)
return HANDLE_CART
def wait_email(update, context):
"""Waits client email input.
Returns:
next bot state for user
"""
chat = update.message or update.edited_message
chat.bot.delete_message(chat.chat_id, message_id=chat.message_id)
email = chat.text
auth_token = context.bot_data['auth_token'].token
try:
create_customer(token=auth_token, email=email)
bot_reply = f'Пользователь с email {email} зарегистрирован.'
except EntityExistsError:
bot_reply = f'Пользователь с email {email} уже зарегистрирован.'
next_request = 'Пришлите нам Ваш адрес текстом или геолокацию.'
chat.reply_text('{}\n{}'.format(bot_reply, next_request))
return HANDLE_COORDINATES
def handle_incorrect_email(update, context):
"""Asks client for reentering his email.
Returns:
previous bot state for user
"""
chat = update.message or update.edited_message
chat.bot.delete_message(chat.chat_id, message_id=chat.message_id)
bot_reply = 'Введите снова Ваш email. Кажется введеный Вами неправильный.'
chat.reply_text(bot_reply)
return WAIT_EMAIL
def handle_address_or_location(update, context):
"""Handles entered address or location.
Returns:
next bot state for user
"""
auth_token = context.bot_data['auth_token'].token
yandex_token = context.bot_data['yandex_token']
if location := update.message.location:
position = (location.latitude, location.longitude)
elif not (
position := fetch_coordinates(yandex_token, update.message.text)
):
update.message.reply_text(
'Не получилось определить ваши координаты. '
'Пришлите нам Ваш адрес текстом или геолокацию.'
)
return HANDLE_COORDINATES
nearest_pizzeria = find_nearest_pizzeria(auth_token, position)
context.user_data['nearest_pizzeria'] = nearest_pizzeria
send_delivery_options(nearest_pizzeria, update.message)
if None not in position:
client_coordinates = {
'latitude': position[0],
'longitude': position[1],
}
context.user_data['client_coordinates'] = client_coordinates
upload_entry(auth_token, client_coordinates, 'customer_address')
return HANDLE_COORDINATES
def handle_delivery(update, context):
"""Handles client delivery choice.
Returns:
next bot state for user
"""
query = update.callback_query.data
chat = update.callback_query.message
distance = context.user_data['nearest_pizzeria']['distance']
delivery_cost = calculate_delivery_cost(distance)
order_details, total_amount = get_order_details_for_invoice(
cart_id=chat.chat_id,
context=context,
)
context.user_data['delivery_option'] = query
if query == 'pickup':
user_choice = 'самовывоз'
else:
user_choice = 'доставку'
order_details = f'{order_details}Доставка: {delivery_cost}'
total_amount += delivery_cost
chat.reply_text(f'Вы выбрали {user_choice}. Можете оплатить Ваш заказ.')
context.bot.send_invoice(
chat_id=chat.chat_id,
title='Оплата заказа из пиццерии',
description=order_details,
payload='pizza-bot-payload',
provider_token=context.bot_data['payment_token'],
currency='RUB',
need_name=True,
need_phone_number=True,
prices=[LabeledPrice('Заказ из пиццерии', total_amount * 100)],
)
return HANDLE_PAYMENT
def handle_invoice_checking(update, context):
"""Checks if client invoice is correct.
Returns:
next bot state for user
"""
query = update.pre_checkout_query
if query.invoice_payload != 'pizza-bot-payload':
query.answer(ok=False, error_message="Что-то пошло не так...")
else:
query.answer(ok=True)
return HANDLE_PAYMENT
def handle_payment(update, context):
"""Handles successful payment processing for client.
Returns:
end of conversation state
"""
chat = update.message
delivery_option = context.user_data['delivery_option']
if delivery_option == 'pickup':
pizzeria_address = context.user_data['nearest_pizzeria']['address']
update.message.reply_text(
dedent(
f"""
Вы можете забрать Ваз заказ по адресу: {pizzeria_address}.
Спасибо за заказ!"""
)
)
return ConversationHandler.END
distance = context.user_data['nearest_pizzeria']['distance']
deliver_cost = calculate_delivery_cost(distance)
client_coordinates = context.user_data['client_coordinates']
nearest_pizzeria = context.user_data['nearest_pizzeria']
auth_token = context.bot_data['auth_token'].token
send_order_details(
cart_id=chat.chat_id,
auth_token=auth_token,
pizzeria=nearest_pizzeria,
client_coordinates=client_coordinates,
delivery_cost=deliver_cost,
chat=context.bot,
)
context.job_queue.run_once(notify_about_pizza, 3600, context=chat.chat_id)
bot_reply = 'Информация о заказе передана курьеру. Спасибо за заказ!'
update.message.reply_text(bot_reply)
return ConversationHandler.END
def exit(update, context):
"""User fallback handler
Args:
update and context: standard bot handler args
Returns:
end of conversation state
"""
update.message.reply_text('Вы закончили диалог')
return ConversationHandler.END
if __name__ == '__main__':
load_dotenv()
bot_token = os.getenv('TG_PIZZA_BOT_TOKEN')
client_id = os.getenv('CLIENT_ID')
client_secret = os.getenv('CLIENT_SECRET')
persistence = PicklePersistence(filename='conversationbot')
updater = Updater(bot_token, use_context=True, persistence=persistence)
jq = updater.job_queue
dp = updater.dispatcher
dp.bot_data['yandex_token'] = os.getenv('YANDEX_API_TOKEN')
dp.bot_data['payment_token'] = os.getenv('PAYMENT_PROVIDER_TOKEN')
dp.bot_data['auth_token'] = AuthToken(client_id, client_secret)
handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
HANDLE_MENU: [CallbackQueryHandler(handle_menu)],
HANDLE_DESCRIPTION: [CallbackQueryHandler(handle_description)],
HANDLE_CART: [CallbackQueryHandler(handle_cart)],
WAIT_EMAIL: [
MessageHandler(Filters.regex('^\w+@\w+\.\w+$'), wait_email),
MessageHandler(Filters.text, handle_incorrect_email),
],
HANDLE_COORDINATES: [
MessageHandler(
Filters.location | Filters.text,
handle_address_or_location,
),
CallbackQueryHandler(handle_delivery),
],
HANDLE_PAYMENT: [
PreCheckoutQueryHandler(handle_invoice_checking),
MessageHandler(
Filters.successful_payment,
handle_payment,
pass_job_queue=True,
),
],
},
fallbacks=[CommandHandler('exit', exit)],
persistent=True,
name='conversation_handler',
per_chat=False,
)
dp.add_handler(handler)
updater.start_polling()
updater.idle()