-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
361 lines (314 loc) · 23.5 KB
/
Copy pathbot.py
File metadata and controls
361 lines (314 loc) · 23.5 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
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
import getInfAboutProduct
import telebot
import config
import takeToken
import sqlite3
import checkUser
import editUser
from telebot import types
import re
from io import BytesIO
import os
user_states = {}
bot = telebot.TeleBot(config.TOKEN)
bot.set_webhook()
def mainKeyboard():
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
bottom1 = types.KeyboardButton("Главная")
bottom2 = types.KeyboardButton("Консультация")
bottom3 = types.KeyboardButton("Заказ")
markup.row(bottom1, bottom2)
markup.add(bottom3)
return markup
@bot.message_handler(commands = ['start', 'main', 'hello'])
@bot.message_handler(func=lambda message: message.text.lower() == 'главная')
def welcome(message):
#DB
conn = sqlite3.connect('shop.sql')
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS users (id int auto_increment primary key, name varchar(255), tgId varchar(20) unique not null, phone varchar(20))')
conn.commit()
cur.close()
conn.close()
###
user_states[message.chat.id] = {'name':None, 'phone':None, 'tgId':None}
sti = open('static/welcome.webp', 'rb')
bot.send_sticker(message.chat.id, sti)
bot.send_message(message.chat.id, "Добро пожаловать, {0.first_name}!\n "
"Я - <b>электронный сотрудник магазина по продаже тканей</b>, бот созданный чтобы помочь тебе сделать заказ.".format(message.from_user, bot.get_me()),
parse_mode='html')
markup = types.InlineKeyboardMarkup()
btn1 = types.InlineKeyboardButton("Статус посылки", callback_data='status')
btn2 = types.InlineKeyboardButton("Сделать заказ", callback_data='order')
markup.row(btn1, btn2)
bot.send_message(message.chat.id, "{0.first_name}, если вы хотите получить информацию по доставке вашего заказа - нажмите на <b>Статус посылки</b>\n"
"Если хотите сделать заказ - нажмите на <b>Сделать заказ</b>".format(message.from_user, bot.get_me()), parse_mode='html', reply_markup=markup)
@bot.message_handler(commands=['order'])
@bot.message_handler(func=lambda message: message.text.lower() == 'заказ')
def order(message):
user_states[message.chat.id] = {'token' : None}
#bot.delete_message(message.chat.id, message.message_id - 2)
bot.send_message(message.chat.id, "Процесс создания заказа начат. Пожалуйста, следуйте инструкциям.", reply_markup=types.ReplyKeyboardRemove())
bot.send_message(message.chat.id, "Введите, пожалуйста, <b>артикул</b> товара\n"
"<b>Пример ввода: 875234</b>\n\n"
"Если вы забыли артикул товара, то можете перейти обратно в основной канал и посмотреть его\n"
"<b>Вот ссылка -> https://t.me/bravissimo_nn</b>", parse_mode='html')
bot.register_next_step_handler(message, get_token)
#Обработка индефикатора и подтверждение правильности выбора товара
def get_token(message):
#config.token = message.text.strip()
#user_states[message.chat.id]['token':None]
user_states[message.chat.id]['token'] = message.text.strip()
#config.product_data = getInfAboutProduct.get_product_data(config.token)
#user_states[message.chat.id]['product_data':None]
user_states[message.chat.id]['product_data'] = getInfAboutProduct.get_product_data(user_states[message.chat.id]['token'])
#Проверка на существование
if (not user_states[message.chat.id]['product_data']):
bot.send_message(message.chat.id, f'К сожалению такого артикула не существует😢\n'
f'Попробуйте снова!')
order(message)
else:
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
bottom1 = types.KeyboardButton("Верно")
bottom2 = types.KeyboardButton("Неверно")
markup.row(bottom1, bottom2)
bot.send_message(message.chat.id, "Проверьте, пожалуйста, что вы правильно ввели артикул\n"
"Данные по данному артикулу:")
#name = config.product_data[1]
#price = config.product_data[3]
#width = config.product_data[4]
#token = config.product_data[5]
#photo = config.product_data[2] # Бинарные данные фотографии из базы данных
# Сохранение бинарных данных фотографии в файл
with open(f'{user_states[message.chat.id]['product_data'][1]}.jpg', 'wb') as file:
file.write(user_states[message.chat.id]['product_data'][2])
# Отправка сообщения с данными о товаре и фотографией
bot.send_photo(message.chat.id, open(f'{user_states[message.chat.id]['product_data'][1]}.jpg', 'rb'), caption = f'Name: {user_states[message.chat.id]['product_data'][1]}\n Price: {user_states[message.chat.id]['product_data'][3]}\n Width: {user_states[message.chat.id]['product_data'][4]}\n Token: {user_states[message.chat.id]['product_data'][5]}', reply_markup=markup)
os.remove(f'{user_states[message.chat.id]['product_data'][1]}.jpg')
#Проверка на правильный выбор
bot.register_next_step_handler(message, check_product)
def check_product(message):
if (message.text.strip() == 'Неверно'):
order(message)
elif (message.text.strip() == 'Верно'):
get_info_user(message)
else:
bot.send_message(message.chat.id, "Выберите одну из кнопок: Верно или Невернно")
bot.register_next_step_handler(message, check_product)
def get_info_user(message):
#Register users
#user_states[message.chat.id]['user_data':None]
user_states[message.chat.id]['user_data'] = checkUser.get_user_data(message.from_user.id)
#config.user_data = checkUser.get_user_data(message.from_user.id)
if (user_states[message.chat.id]['user_data']):
markup = types.InlineKeyboardMarkup()
bottom1 = types.InlineKeyboardButton('Верно', callback_data='true_enter')
bottom2 = types.InlineKeyboardButton('Изменились', callback_data='edit_data')
markup.row(bottom1, bottom2)
user_states[message.chat.id]['name'] = user_states[message.chat.id]['user_data'][1]
user_states[message.chat.id]['phone'] = user_states[message.chat.id]['user_data'][3]
user_states[message.chat.id]['tgId'] = message.from_user.id
#config.name = config.user_data[1]
#config.phone = config.user_data[3]
#config.tg_id = config.user_data[2]
bot.send_message(message.chat.id,f'Вы уже были в нашем магазине, и у нас есть ваши данные😁', reply_markup=types.ReplyKeyboardRemove())
bot.send_message(message.chat.id, f'Проверьте, пожалуйста, текущие данные на корректность:\n\n'
f'ФИО: {user_states[message.chat.id]['user_data'][1]}\n'
f'Номер телефона: {user_states[message.chat.id]['user_data'][3]}', reply_markup=markup)
else:
bot.send_message(message.chat.id, "Сейчас нужно вас зарегестрирвоать!\n"
"Введите, пожалуйста, свои: Фамилия, Имя, Отчество", reply_markup=types.ReplyKeyboardRemove())
bot.register_next_step_handler(message, user_name)
def user_name(message):
user_states[message.chat.id]['name'] = message.text.strip()
if re.match(r'^[А-ЯЁа-яё]+\s+[А-ЯЁа-яё]+\s+[А-ЯЁа-яё]+$', user_states[message.chat.id]['name']):
# Ввод пользователя соответствует формату Фамилия Имя Отчество
#config.name = full_name
bot.send_message(message.chat.id, "Введите свой номер телефона в форматах:\n"
"89*********\n"
"+79*********")
bot.register_next_step_handler(message, user_phone)
else:
# Ввод пользователя не соответствует формату ФИО
bot.send_message(message.chat.id, "Пожалуйста, введите Фамилию Имя Отчество в правильном формате.")
bot.register_next_step_handler(message, user_name)
def user_phone(message):
user_states[message.chat.id]['phone'] = message.text.strip()
if re.match(r'^(\+7|8)9\d{9}$', user_states[message.chat.id]['phone']):
# Введенный номер телефона соответствует формату
#config.phone = message.text.strip()
#config.tg_id = message.from_user.id
user_states[message.chat.id]['tgId'] = message.from_user.id
markup = types.InlineKeyboardMarkup()
bottom1 = types.InlineKeyboardButton('Верно', callback_data='true_enter')
bottom2 = types.InlineKeyboardButton('Неверно', callback_data='false_enter')
markup.row(bottom1,bottom2)
bot.send_message(message.chat.id, f'Мы закончили небольшую регистрацию🔥')
bot.send_message(message.chat.id,f'Проверьте, пожалуйста, ваши данные на корректность:\n ФИО: {user_states[message.chat.id]['name']}\n Номер телефона: {user_states[message.chat.id]['phone']}', reply_markup=markup)
else:
# Неверный формат номера телефона
bot.send_message(message.chat.id, "Пожалуйста, введите корректный номер телефона.")
bot.register_next_step_handler(message, user_phone)
@bot.message_handler(commands=['advice'])
@bot.message_handler(func=lambda message: message.text.lower() == 'консультация')
def consult(message):
#name = config.product_data[1]
#price = config.product_data[3]
#width = config.product_data[4]
#token = config.product_data[5]
#photo = config.product_data[2] # Бинарные данные фотографии из базы данных
# Сохранение бинарных данных фотографии в файл
with open(f'{user_states[message.chat.id]['product_data'][1]}.jpg', 'wb') as file:
file.write(user_states[message.chat.id]['product_data'][2])
# Отправка сообщения с данными о товаре и фотографией
bot.send_photo(config.manager_id, open(f'{user_states[message.chat.id]['product_data'][1]}.jpg', 'rb'), caption= f'Консультация!\n'
f'Информация о заказе с артикулом - {user_states[message.chat.id]['product_data'][5]}:\n'
f'Название: {user_states[message.chat.id]['product_data'][1]}\n'
f'Цена: {user_states[message.chat.id]['product_data'][3]}\n'
f'Ширина: {user_states[message.chat.id]['product_data'][4]}\n\n'
f'Информация о пользователе:\n'
f'Ник пользователя - {message.from_user.username}\n'
f'ФИО - {user_states[message.chat.id]['name']}\n'
f'Номер телефона - {user_states[message.chat.id]['phone']}')
os.remove(f'{user_states[message.chat.id]['product_data'][1]}.jpg')
bot.send_message(message.chat.id,f'Перейдите по следующей ссылке, чтобы связаться с менеджером. Обязательно отправьте артикул своего товара, чтобы менеджер смог вас понять)\n\n'
f'<b>Переходи сюда</b> -> https://t.me/res12245', parse_mode='html', reply_markup=types.ReplyKeyboardRemove())
@bot.message_handler(commands=['edit_name'])
@bot.message_handler(func=lambda message: message.text.lower() == 'фио')
def name(message):
bot.send_message(message.chat.id, "Введите, пожалуйста, свои: Фамилия, Имя, Отчество", reply_markup=types.ReplyKeyboardRemove())
bot.register_next_step_handler(message, get_name)
def get_name(message):
#full_name = message.text.strip()
user_states[message.chat.id]['name'] = message.text.strip()
if re.match(r'^[А-ЯЁа-яё]+\s+[А-ЯЁа-яё]+\s+[А-ЯЁа-яё]+$', user_states[message.chat.id]['name']):
#config.name = full_name
editUser.update_user_name(user_states[message.chat.id]['name'], message.from_user.id)
markup = types.InlineKeyboardMarkup()
bottom1 = types.InlineKeyboardButton('Верно', callback_data='true_enter')
bottom2 = types.InlineKeyboardButton('Неверно', callback_data='edit_data')
markup.row(bottom1, bottom2)
bot.send_message(message.chat.id,f'Проверьте, пожалуйста, ваши данные на корректность:\n ФИО: {user_states[message.chat.id]['name']}\n Номер телефона: {user_states[message.chat.id]['phone']}',reply_markup=markup)
else:
# Ввод пользователя не соответствует формату ФИО
bot.send_message(message.chat.id, "Пожалуйста, введите Фамилию Имя Отчество в правильном формате.")
bot.register_next_step_handler(message, get_name)
@bot.message_handler(commands=['edit_phone'])
@bot.message_handler(func=lambda message: message.text.lower() == 'номер')
def phone(message):
bot.send_message(message.chat.id, "Введите, пожалуйста, новый номер", reply_markup=types.ReplyKeyboardRemove())
bot.register_next_step_handler(message, get_phone)
def get_phone(message):
# phone_nember = message.text.strip()
user_states[message.chat.id]['phone'] = message.text.strip()
if re.match(r'^79\d{9}$', user_states[message.chat.id]['phone']):
#config.phone = phone_number
editUser.update_user_phone(user_states[message.chat.id]['phone'], message.from_user.id)
markup = types.InlineKeyboardMarkup()
bottom1 = types.InlineKeyboardButton('Верно', callback_data='true_enter')
bottom2 = types.InlineKeyboardButton('Неверно', callback_data='edit_data')
markup.row(bottom1, bottom2)
bot.send_message(message.chat.id,f'Проверьте, пожалуйста, ваши данные на корректность:\n ФИО: {user_states[message.chat.id]['name']}\n Номер телефона: {user_states[message.chat.id]['phone']}',reply_markup=markup)
else:
# Неверный формат номера телефона
bot.send_message(message.chat.id, "Пожалуйста, введите корректный номер телефона.")
bot.register_next_step_handler(message, get_phone)
@bot.message_handler(commands=['edit_all'])
@bot.message_handler(func=lambda message: message.text.lower() == 'все')
def name_from_all(message):
bot.send_message(message.chat.id, "Введите, пожалуйста, свои: Фамилия, Имя, Отчество", reply_markup=types.ReplyKeyboardRemove())
bot.register_next_step_handler(message, get_name_from_all)
def get_name_from_all(message):
# full_name = message.text.strip()
user_states[message.chat.id]['name'] = message.text.strip()
if re.match(r'^[А-ЯЁа-яё]+\s+[А-ЯЁа-яё]+\s+[А-ЯЁа-яё]+$', user_states[message.chat.id]['name']):
# config.name = full_name
bot.send_message(message.chat.id, "Введите, пожалуйста, новый номер")
bot.register_next_step_handler(message, get_all)
else:
# Ввод пользователя не соответствует формату ФИО
bot.send_message(message.chat.id, "Пожалуйста, введите Фамилию Имя Отчество в правильном формате.")
bot.register_next_step_handler(message, get_name_from_all)
def get_all(message):
# phone_number = message.text.strip()
user_states[message.chat.id]['phone'] = message.text.strip()
if re.match(r'^79\d{9}$', user_states[message.chat.id]['phone']):
# config.phone = phone_number
editUser.update_user_all(user_states[message.chat.id]['phone'], user_states[message.chat.id]['name'], message.from_user.id)
markup = types.InlineKeyboardMarkup()
bottom1 = types.InlineKeyboardButton('Верно', callback_data='true_enter')
bottom2 = types.InlineKeyboardButton('Неверно', callback_data='edit_data')
markup.row(bottom1, bottom2)
bot.send_message(message.chat.id,f'Проверьте, пожалуйста, ваши данные на корректность:\n ФИО: {user_states[message.chat.id]['name']}\n Номер телефона: {user_states[message.chat.id]['phone']}',reply_markup=markup)
else:
# Неверный формат номера телефона
bot.send_message(message.chat.id, "Пожалуйста, введите корректный номер телефона.")
bot.register_next_step_handler(message, get_all)
@bot.message_handler(content_types = ['photo', 'video', 'audio', 'sticker', 'emoji'])
def noneContent(message):
bot.reply_to(message, f'Извините, {message.from_user.first_name}, я не умею обрабатывать такие сообщения((')
@bot.callback_query_handler(func=lambda callback: True)
def callback_message(callback):
if callback.data == 'status':
bot.send_message(callback.message.chat.id,'Перенаправляю Вас на моего коллегу, регистрация в один клик, далее введите <b>/add (номер вашего заказа)</b>, а затем <b>/tracks</b>. Также напоминаю, что по правилам нашего магазина (далее правила по срокам доставки)\n\n' + "Ссылка -> https://t.me/RLabbot",parse_mode='html')
# bot.delete_message(callback.message.chat.id, callback.message.message_id)
elif callback.data == 'order':
markup1 = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton("Заказ")
markup1.add(item1)
bot.send_message(callback.message.chat.id, "Чтобы сделать заказ, отправьте команду <b>/order</b> или нажмите на кнопку <b>Заказ</b>" , parse_mode='html', reply_markup=markup1)
elif callback.data == 'true_enter':
if (not user_states[callback.message.chat.id]['user_data']):
conn = sqlite3.connect('shop.sql')
cur = conn.cursor()
cur.execute(f"INSERT INTO users(name, tgId, phone) VALUES ('{user_states[callback.message.chat.id]['name']}', '{user_states[callback.message.chat.id]['tgId']}', '{user_states[callback.message.chat.id]['phone']}')")
conn.commit()
cur.close()
conn.close()
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
bottom1 = types.KeyboardButton("Консультация")
bottom2 = types.KeyboardButton("Продолжаем")
markup.row(bottom1, bottom2)
bot.send_message(callback.message.chat.id, "Ваши данные успешно зарегестрированы)")
bot.send_message(callback.message.chat.id, "Нужна ли вам дополнительная консультация с нашим менеджером по поводу заказа?\n\n"
"Выберите <b>Консультация</b>, если нужна\n"
"Выберите <b>Продолжаем</b>, если не нужна", parse_mode='html', reply_markup=markup)
#bot.delete_message(callback.message.chat.id,callback.message.message_id)
elif callback.data == 'false_enter':
markup1 = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton("Заказ")
markup1.add(item1)
bot.send_message(callback.message.chat.id, "Давайте заполним ваши данные заново. Отправьте, пожалуйста, команду <b>/order</b> или нажмите на кнопку <b>Заказ</b>", parse_mode='html', reply_markup=markup1)
elif callback.data == 'edit_data':
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
bottom1 = types.KeyboardButton("ФИО")
bottom2 = types.KeyboardButton("Номер")
bottom3 = types.KeyboardButton("Все")
markup.row(bottom1, bottom2)
markup.add(bottom3)
bot.send_message(callback.message.chat.id, f'Выберите, пожалуйста, какие данные поменялись🙃', reply_markup=markup)
bot.edit_message_text(chat_id=callback.message.chat.id, message_id=callback.message.message_id, text = 'Continue....', reply_markup=None)
@bot.message_handler()
def info(message):
if (message.text.lower() == 'привет'):
bot.send_message(message.chat.id, "😕")
bot.send_message(message.chat.id, "Добро пожаловать, {0.first_name}!\n "
"Я - <b>электронный сотрудник магазина по продаже тканей</b>, бот созданный чтобы помочь тебе сделать заказ.".format(message.from_user, bot.get_me()),
parse_mode='html')
elif (message.text.lower() == 'id'):
bot.reply_to(message, f'ID: {message.from_user.id}')
elif (message.text.lower() == 'test'):
conn = sqlite3.connect('shop.sql')
cur = conn.cursor()
cur.execute('SELECT * FROM users')
products = cur.fetchall()
info = ''
for elm in products:
info += f'name: {elm[1]}, phone: {elm[3]}, id: {elm[2]}'
# Отправка сообщения с данными о товаре и фотографией
bot.send_message(message.chat.id, info)
cur.close()
conn.close()
else:
bot.reply_to(message, f'Извините, {message.from_user.first_name}, я не умею обрабатывать такие сообщения((')
bot.polling(none_stop = True)