-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_polling.py
More file actions
348 lines (294 loc) · 14.1 KB
/
Copy pathmain_polling.py
File metadata and controls
348 lines (294 loc) · 14.1 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
import telebot
from configs import *
from markups import *
from dadata import suggest
import re
from classes import *
bot = telebot.TeleBot(TOKEN)
# memcached!!!
temp_data = {}
phone_regexp = re.compile(r"^((\+7|7|8)+([0-9]){10})$")
mail_regexp = re.compile(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)")
def get_phone(message):
import pdb
pdb.set_trace()
temp_data[message.chat.id].update({'phone': message.text})
data = temp_data[message.chat.id]
if data.get('type') == 'new':
bot.send_message(message.chat.id, 'Отправить адвокату?\n\n'
'Клиент: %s\n'
'ИНН: %s\n'
'Сумма иска: %s\n'
'Контактное лицо: %s\n'
'Телефон: %s\n'
'Мыло: %s' % (data.get('name'), data.get('inn'), data.get('isk'),
data.get('person'), data.get('phone'), data.get('e-mail')),
reply_markup=yes_no(5))
return
client = Client(None, t_data=temp_data[message.chat.id], parent=message.chat.id)
client.modify()
del temp_data[message.chat.id]
bot.send_message(message.chat.id, confirm_message, reply_markup=welcome_markup())
return
def get_mail(message):
temp_data[message.chat.id].update({"e-mail": message.text})
msg = bot.send_message(message.chat.id, lines[2], reply_markup=skip_markup(2))
bot.clear_step_handler(msg)
bot.register_next_step_handler(msg, get_phone)
return
def get_person(message):
if not message.chat.id in temp_data.keys():
temp_data[message.chat.id] = {'person': message.text}
else:
temp_data[message.chat.id].update({'person': message.text})
msg = bot.send_message(message.chat.id, lines[1], reply_markup=skip_markup(1))
bot.clear_step_handler(msg)
bot.register_next_step_handler(msg, get_mail)
return
# data + inn verification
def handle_inn(message):
if message.text == 'Мониторинг':
handle_monitoring(message)
return
elif message.text == 'Найден':
found_handler(message)
return
elif message.text == 'Информация':
handle_information(message)
return
result = suggest(message.text, 'party')
if not result or not message.text.isdigit():
msg = bot.send_message(message.chat.id, mon_error_text)
bot.clear_step_handler(msg)
return
if message.chat.id in temp_data.keys():
temp_data[message.chat.id].update({'inn': message.text})
else:
temp_data[message.chat.id] = {"inn": message.text}
temp_data[message.chat.id].update({'name': result[0], 'type': 'search'})
bot.send_message(message.chat.id, mon_text_2)
bot.send_message(message.chat.id, "Компания: " + result[0], reply_markup=yes_no(0))
def get_inn(message):
if message.text == 'Мониторинг':
handle_monitoring(message)
return
elif message.text == 'Найден':
found_handler(message)
return
elif message.text == 'Информация':
handle_information(message)
return
result = suggest(message.text, 'party')
if not result or not message.text.isdigit():
msg = bot.send_message(message.chat.id, mon_error_text)
bot.clear_step_handler(msg)
return
if message.chat.id in temp_data.keys():
temp_data[message.chat.id].update({'inn': message.text})
else:
temp_data[message.chat.id] = {"inn": message.text}
temp_data[message.chat.id].update({'name': result[0], 'type': 'new'})
bot.send_message(message.chat.id, mon_text_2)
bot.send_message(message.chat.id, "Компания: " + result[0], reply_markup=yes_no(3))
def get_isk(message):
if not temp_data[message.chat.id]:
temp_data[message.chat.id] = {'isk': message.text}
else:
temp_data[message.chat.id].update({'isk': message.text})
msg = bot.send_message(message.chat.id, lines[0], reply_markup=skip_markup(0))
bot.clear_step_handler(msg)
bot.register_next_step_handler(msg, get_person)
def form_page(page_num, parent):
if page_num < 1:
return
c = 0
users = []
for u in get_users(parent_id=parent):
if c >= (page_num - 1) * 10:
if c < page_num * 10:
users.append(u)
c += 1
else:
return users
else:
c += 1
return users
@bot.message_handler(commands=['start'])
def handle_start(message):
bot.send_message(message.chat.id, welcome_text, reply_markup=welcome_markup())
@bot.message_handler(regexp=r'Мониторинг')
def handle_monitoring(message):
msg = bot.send_message(message.chat.id, mon_text)
bot.clear_step_handler(msg)
bot.register_next_step_handler(msg, handle_inn) # ask for inn
@bot.message_handler(regexp=r'Информация')
def handle_information(message):
bot.send_message(message.chat.id,
'Здесь вы можете найти интересующую вас информацию.\nВыберите один из разделов...',
reply_markup=inf_markup())
@bot.message_handler(regexp=r'О Сервисе')
def about_service(message):
bot.send_message(message.chat.id, information[0])
@bot.message_handler(regexp=r'FAQ')
def about_faq(message):
bot.send_message(message.chat.id, information[1])
@bot.message_handler(regexp=r'Назад')
def step_back(message):
bot.send_message(message.chat.id, main_menu_text, reply_markup=welcome_markup())
@bot.message_handler(regexp=r'Список клиентов')
def client_list(message):
npage = user_list(message.chat.id, 1)
bot.send_message(message.chat.id, 'Следующая страница', reply_markup=npage)
@bot.message_handler(regexp=r'Найден')
def found_handler(message):
msg = bot.send_message(message.chat.id, new_client_text_1, reply_markup=yes_no(2))
bot.clear_step_handler(msg)
@bot.callback_query_handler(func=lambda call: call.data.startswith('info'))
def handle_info_query(call):
import pdb
pdb.set_trace()
uid = int(call.data.split('/')[1])
data = get_user_by_id(uid)
bot.send_message(call.message.chat.id, 'Клиент: %s\n'
'ИНН: %s\n'
'Контактное лицо: %s\n'
'Телефон: %s\n'
'Мыло: %s' % (data.get('name'), data.get('inn'),
data.get('person'), data.get('phone'), data.get('mail')),
reply_markup=edit_markup(data.get('id')))
@bot.callback_query_handler(func=lambda call: call.data.startswith('page'))
def newx_page_handler(call):
page_num = call.data.split('/')[1]
npage = user_list(call.message.chat.id, int(page_num))
bot.send_message('Следующая страница', reply_markup=npage)
@bot.callback_query_handler(func=lambda call: call.data.startswith('skip'))
def skip_step_handler(call):
if call.message.chat.id not in temp_data.keys():
return
step = int(call.data.split('/')[1])
# skip phone
if step == 2:
temp_data[call.message.chat.id].update({'phone': 'Не указано'})
if temp_data[call.message.chat.id].get('type') == 'new':
data = temp_data[call.message.chat.id]
bot.send_message(call.message.chat.id, 'Отправить адвокату?\n\n'
'Клиент: %s\n'
'ИНН: %s\n'
'Сумма иска: %s\n'
'Контактное лицо: %s\n'
'Телефон: %s\n'
'Мыло: %s' % (data.get('name'), data.get('inn'), data.get('isk'),
data.get('person'), data.get('phone'),
data.get('e-mail')),
reply_markup=yes_no(5))
return
message = call.message
client = Client(None, t_data=temp_data[call.message.chat.id], parent=message.chat.id)
client.modify()
del temp_data[message.chat.id]
msg = bot.send_message(message.chat.id, confirm_message, reply_markup=welcome_markup())
bot.clear_step_handler(msg)
return
# after isk "find"
elif step == 4:
temp_data[call.message.chat.id].update({'isk': ' Не указано'})
msg = bot.send_message(call.message.chat.id, lines[0], reply_markup=skip_markup(0))
bot.clear_step_handler(msg)
bot.register_next_step_handler(msg, get_person)
return
msg = bot.send_message(call.message.chat.id, lines[int(step) + 1], reply_markup=skip_markup(int(step) + 1))
bot.clear_step_handler(msg)
if step == 0:
temp_data[call.message.chat.id].update({'person': ' Не указано'})
bot.register_next_step_handler(msg, get_mail)
elif step == 1:
temp_data[call.message.chat.id].update({'e-mail': ' Не указано'})
bot.register_next_step_handler(msg, get_phone)
@bot.callback_query_handler(func=lambda call: call.data.startswith('edit'))
def edit_user_query(call):
uid = call.data.split('/')[1]
data = get_user_by_id(uid)
temp_data[call.message.chat.id] = {'type': 'search', 'inn': data.get('inn'), 'name': data.get('name')}
msg = bot.send_message(call.message.chat.id, lines[0], reply_markup=skip_markup(0))
bot.clear_step_handler(msg)
bot.register_next_step_handler(msg, get_person)
return
@bot.callback_query_handler(func=lambda call: not call.data.startswith('delete') and call.data != '-')
def handle_callback(call):
if call.data.count('/') == 0:
if call.data == 'error':
msg = bot.send_message(call.message.chat.id, mon_text)
bot.register_next_step_handler(msg, handle_inn)
elif call.data == 'stop':
bot.send_message(call.message.chat.id, stop_mon_text)
else:
res, step = map(str, call.data.split('/'))
# company confirmed "trace"
if step == '0':
if res == 'yes':
if not get_user(temp_data[call.message.chat.id].get('inn'), call.message.chat.id):
new_user(temp_data[call.message.chat.id].get('inn'), call.message.chat.id)
bot.send_message(call.message.chat.id, mon_text_3, reply_markup=yes_no(1))
else:
del temp_data[call.message.chat.id]
bot.send_message(call.message.chat.id, error_text, reply_markup=None)
# redirect to "trace" input seq
elif step == '1':
if res == 'yes':
msg = bot.send_message(call.message.chat.id, lines[0], reply_markup=skip_markup(0))
bot.clear_step_handler(msg)
bot.register_next_step_handler(msg, get_person)
else:
temp_data[call.message.chat.id].update({'person': 'Не указано',
'phone': 'Не указано',
'e-mail': 'Не указано'})
cl = Client(None, temp_data[call.message.chat.id], call.message.chat.id)
cl.modify()
bot.send_message(call.message.chat.id, confirm_message)
# after "any new clients?"
elif step == '2':
if res == 'yes':
msg = bot.send_message(call.message.chat.id, 'Введите ИНН нового клиента', reply_markup=None)
bot.clear_step_handler(msg)
bot.register_next_step_handler(msg, get_inn)
else:
msg = bot.send_message(call.message.chat.id, new_text_2)
bot.clear_step_handler(msg)
# company confirmed "new"
elif step == '3':
if res == 'yes':
if call.message.chat.id not in temp_data.keys():
msg = bot.send_message(call.message.chat.id, 'Что-то пошло не так! Введите ИНН ещё раз!')
bot.register_next_step_handler(msg, get_inn)
return
id = new_client(temp_data[call.message.chat.id].get('inn'), call.message.chat.id)
temp_data[call.message.chat.id].update({'id': id})
msg = bot.send_message(call.message.chat.id, 'Сумма иска', reply_markup=skip_markup(4))
bot.clear_step_handler(msg)
bot.register_next_step_handler(msg, get_isk)
return
else:
msg = bot.send_message(call.message.chat.id, error_text)
bot.clear_step_handler(msg)
elif step == '5':
if res == 'yes':
cl = Client(None, temp_data[call.message.chat.id], parent=call.message.chat.id)
cl.modify()
bot.send_message(call.message.chat.id, confirm_message, reply_markup=welcome_markup())
return
else:
import pdb
pdb.set_trace()
delete_client(temp_data[call.message.chat.id].get('id'))
del temp_data[call.message.chat.id]
msg = bot.send_message(call.message.chat.id, 'Зачем, Карл?!')
bot.clear_step_handler(msg)
return
else:
raise Exception('Expected step <=1, got %s' % str(step))
@bot.callback_query_handler(func=lambda call: call.data.startswith('delete'))
def del_user(call):
inn = call.data.split('/')[1]
delete_user(inn, call.message.chat.id)
bot.send_message(call.message.chat.id, confirm_message)
bot.polling(none_stop=True)