-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.py
225 lines (189 loc) · 8.45 KB
/
core.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
from Cart import Cart
import telebot
from telebot import types
import requests
import time
token = '1384569458:AAH4iyBPSnB77ekv0eA-hOpdCP7l6TsUJHY'
bot = telebot.TeleBot(token)
# url = 'http://127.0.0.1:8000/api/category/'
url = 'http://127.0.0.1:8000/api/'
@bot.message_handler(commands=['start'])
def start(message):
# global cart
# cart = Cart(message.chat.id)
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
key_1 = types.KeyboardButton('Каталог')
key_2 = types.KeyboardButton('Корзина')
key_3 = types.KeyboardButton('Заказы')
key_4 = types.KeyboardButton('Контакты')
markup.add(key_1, key_2, key_3, key_4)
bot.send_message(message.chat.id,
text='a', reply_markup=markup)
@bot.callback_query_handler(func=lambda call: call.data in ['Каталог', 'order'])
def da(call, order=None):
try:
data = call.data
# call.data
except:
data = ''
if data == 'Каталог':
bot.delete_message(chat_id=call.message.chat.id,
message_id=call.message.message_id)
keyboard = types.InlineKeyboardMarkup()
r = requests.get(url+'category')
for category in r.json():
if category['categories']:
key = types.InlineKeyboardButton(
text=category['name'], callback_data=('cat_' + str(category['id'])))
keyboard.add(key)
# for category in r.json()['categories']:
# key = types.InlineKeyboardButton(
# text=category['name'], callback_data=category['id'])
# keyboard.add(key)
bot.send_message(call.message.chat.id,
text='Выбери категорию', reply_markup=keyboard)
elif data == 'order' or order:
msg = bot.send_message(call.message.chat.id, 'Введите Ваше имя')
bot.register_next_step_handler(msg, order)
@bot.message_handler(content_types=['text'])
def katalog(message):
bot.delete_message(chat_id=message.chat.id,
message_id=message.message_id)
cart = Cart(message.chat.id)
text = message.text
if text == 'Каталог':
keyboard = types.InlineKeyboardMarkup()
r = requests.get(url+'category')
for category in r.json():
if category['categories']:
key = types.InlineKeyboardButton(
text=category['name'], callback_data=('cat_' + str(category['id'])))
keyboard.add(key)
# for category in r.json()['categories']:
# key = types.InlineKeyboardButton(
# text=category['name'], callback_data=category['id'])
# keyboard.add(key)
bot.send_message(message.chat.id,
text='Выбери категорию', reply_markup=keyboard)
elif text == 'Корзина':
# bot.edit_message_text(chat_id=message.chat.id,
# message_id=message.message_id-1, text='test')
keyboard = types.InlineKeyboardMarkup()
key = types.InlineKeyboardButton(
'Заказать', callback_data='order')
key_2 = types.InlineKeyboardButton('Очистить', callback_data='clear')
keyboard.add(key, key_2)
t = [f"{prod['quantity']}x {prod['name']} - {prod['price']}р. - {prod['total_price']}р.\n" for prod in cart]
t = 'Корзина\n\n' + '\n'.join(t) + \
f'\nИтого: {cart.get_total_price()}р.'
if cart:
bot.send_message(message.chat.id, text=t, reply_markup=keyboard)
else:
bot.send_message(message.chat.id, text=t)
def order(message):
if message.text.isalpha():
with open('db.json', 'r+') as file:
data = json.load(file)
cart = Cart(message.chat.id)
cart['order'] = {
# 'name': name
# 'adress': adress
}
else:
da('test', 1)
# elif text == 'Заказы':/
@bot.callback_query_handler(func=lambda call: 'cat' in call.data.split('_'))
def callback_worker(call):
id = call.data.split('_')[-1]
bot.delete_message(call.message.chat.id, call.message.message_id)
r = requests.get(url+'category/'+id)
keyboard = types.InlineKeyboardMarkup()
key = types.InlineKeyboardButton(
text='Назад', callback_data='Каталог')
keyboard.add(key)
for category in r.json()['categories']:
key = types.InlineKeyboardButton(
text=category['name'], callback_data='prods_'+str(category['id']))
keyboard.add(key)
bot.send_message(call.message.chat.id,
text='Выбери категорию', reply_markup=keyboard)
@bot.callback_query_handler(func=lambda call: 'prods' in call.data.split('_'))
def prods(call):
bot.delete_message(call.message.chat.id, call.message.message_id)
id = call.data.split('_')[-1]
bot.last_update_id
r = requests.get(url+f'product/?category={id}&limit=3')
for prod in r.json()['results']:
prod_id = prod['id']
name = prod['name']
price = prod['price']
# images = prod['images']
images = 'https://im0-tub-ru.yandex.net/i?id=b6a2cf0ee7b2aafd66d4e996bab05433&n=13'
keyboard = types.InlineKeyboardMarkup()
key_1 = types.InlineKeyboardButton(
text='Добавить', callback_data=f'add_{prod_id}')
key_2 = types.InlineKeyboardButton(
text='Заказать в 1 клик', callback_data=f'add_{prod_id}')
keyboard.add(key_1, key_2)
img = [types.InputMediaPhoto(images, caption=name)
for img in range(3)]
# img = [types.InputMediaPhoto(img['image'], caption='test')
# for img in images]
bot.send_media_group(call.message.chat.id, img)
bot.send_message(call.message.chat.id,
f'''{name}
{price}р.''', reply_markup=keyboard)
# time.sleep(0.3)
# ОТПРАВКА 1 IMG
# bot.send_photo(call.message.chat.id, images, caption=f'''{name}
# {price}р.''', reply_markup=keyboard)
last_mes = None
@bot.callback_query_handler(func=lambda call: 'add' in call.data.split('_'))
def add(call):
cart = Cart(call.message.chat.id)
# bot.delete_message(call.message.chat.id, call.message.message_id)
id = call.data.split('_')[-1]
global last_mes
prod = cart.add(id)
keyboard = types.InlineKeyboardMarkup()
key = types.InlineKeyboardButton(
'Заказать', callback_data='order')
key_2 = types.InlineKeyboardButton('Очистить', callback_data='clear')
keyboard.add(key, key_2)
# bot.edit_message_text('adsad', call.message.chat.id)
# if prod['quantity'] == 1:
# t = [f"{prod['quantity']}x {prod['name']} - {prod['price']}р.\n" for prod in cart]
# t = '🛒 Продукты у Вас в корзине:\n\n' + '\n'.join(t) + \
# f'\nИтого: {cart.get_total_price()}р.'
t = [
f"{prod['quantity']}x {prod['name']} - {prod['price']}р.\n" for prod in cart]
t = '🛒 Продукты у Вас в корзине:\n\n' + '\n'.join(t) + \
f'\nИтого: {cart.get_total_price()}р.'
try:
if last_mes.message_id > call.message.message_id:
bot.edit_message_text(t, call.message.chat.id,
last_mes.message_id, reply_markup=keyboard)
else:
# bot.delete_message(last_mes.chat.id,last_mes.message_id)
last_mes = bot.send_message(
call.message.chat.id, t, reply_markup=keyboard)
except:
last_mes = bot.send_message(
call.message.chat.id, t, reply_markup=keyboard)
@bot.callback_query_handler(func=lambda call: 'clear' == call.data)
def clear(call):
cart = Cart(call.message.chat.id)
bot.delete_message(call.message.chat.id, call.message.message_id)
cart.clear()
bot.send_message(call.message.chat.id, 'Корзина пуста')
# else:
# bot.edit_message_text(f'''🛒 Продукты у Вас в корзине:
# {prod['quantity']}x {prod['name']} - {prod['price']}р.
# Итого: {cart.get_total_price()}р.''',
# call.message.chat.id, call.message.message_id)
bot.polling(none_stop=True, interval=0)
# r = requests.get(url+'category/'+str(message.data))
# keyboard = types.InlineKeyboardMarkup()
# key = types.InlineKeyboardButton(
# text='Назад', callback_data=call.data)
# keyboard.add(key)