-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot_engine_final.py
More file actions
121 lines (99 loc) · 4.91 KB
/
bot_engine_final.py
File metadata and controls
121 lines (99 loc) · 4.91 KB
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
import logging
from telegram.ext import ConversationHandler
from telegram import ReplyKeyboardMarkup
from const import *
from utils import get_chat_info
# Включим ведение журнала
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
class BotEngine():
def start(self, update, context):
chat = get_chat_info(update)
if not context.user_data.get("contacts"):
context.user_data["contacts"] = update.message.chat.first_name
context.bot.send_message(chat_id=chat.id, text=f'Привет, {context.user_data["contacts"]}!')
if context.user_data["contacts"] == update.message.chat.first_name:
context.bot.send_message(chat_id=chat.id, text='Напиши как тебя называть')
return BOT_SELECT_CONTACTS
if context.user_data.get("food"):
buttons = ReplyKeyboardMarkup([
[ANSWER_YES, ANSWER_NO],
ALWAYS_SHOW_BUTTON_CAPTIONS
])
context.bot.send_message(
chat_id=chat.id,
text=f'В прошлый раз ты заказывал, {context.user_data.get("food")}! \n Повторить?',
reply_markup=buttons
)
return BOT_SELECT_FOOD
context.bot.send_message(chat_id=chat.id,
text=f'{context.user_data["contacts"]}, Выбери что ты хочешь заказать ',
reply_markup=FOOD_BUTTONS
)
return BOT_SELECT_FOOD
def get_contacts(self, update, context):
chat = get_chat_info(update)
context.user_data["contacts"] = update.message.text
context.bot.send_message(chat_id=chat.id,
text=f'{context.user_data["contacts"]}, Выбери что ты хочешь заказать ',
reply_markup=FOOD_BUTTONS
)
return BOT_SELECT_FOOD
def exit(self, update, context):
chat = get_chat_info(update)
context.bot.send_message(
chat_id=chat.id,
text=f'До свидания, {context.user_data["contacts"]}!',
reply_markup=FINISH_BUTTONS
)
return ConversationHandler.END
def select_food(self, update, context):
chat = get_chat_info(update)
if update.message.text not in FOOD + ANSWERS:
context.bot.send_message(chat_id=chat.id,
text=f'{context.user_data["contacts"]}, я не знаю такого продукта. \n'
'Пожалуйста, выбери из списка на кнопках'
)
return BOT_SELECT_FOOD
if update.message.text == ANSWER_NO:
buttons = ReplyKeyboardMarkup([
FOOD,
ALWAYS_SHOW_BUTTON_CAPTIONS
])
context.bot.send_message(chat_id=chat.id,
text='Выбери что ты хочешь заказать',
reply_markup=buttons
)
return BOT_SELECT_FOOD
if update.message.text != ANSWER_YES:
context.user_data["food"] = update.message.text
buttons = ReplyKeyboardMarkup([
['Все ок, доставьте заказ'],
ALWAYS_SHOW_BUTTON_CAPTIONS
])
context.bot.send_message(chat_id=chat.id,
text=f'Ты заказываешь {context.user_data["food"]}',
reply_markup=buttons
)
return BOT_SELECT_ADDRESS
def select_address(self, update, context):
selections = []
if context.user_data.get("address"):
selections.append(context.user_data["address"])
buttons = ReplyKeyboardMarkup([
selections,
ALWAYS_SHOW_BUTTON_CAPTIONS
])
chat = update.effective_chat
context.bot.send_message(chat_id=chat.id,
text='Пожалуйста, введи адрес доставки',
reply_markup=buttons
)
return BOT_DELIVERY
def delivery(self, update, context):
context.user_data["address"] = update.message.text
text = 'Спасибо за заказ! \n'+f'{context.user_data["contacts"]}, ожидайте {context.user_data["food"]} по адресу {context.user_data["address"]}'
update.message.reply_text(text, reply_markup=FINISH_BUTTONS)
return ConversationHandler.END