Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -193,72 +193,192 @@ def clean_phone_number_ss(phone_number):
|
|
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 |
-
return json.dumps({"message": "User added successfully"}), 201
|
253 |
|
254 |
-
except Exception as e:
|
255 |
-
logging.error(f"An error occurred: {str(e)}")
|
256 |
-
return json.dumps({"error": str(e)}), 500
|
257 |
|
|
|
|
|
|
|
258 |
|
|
|
|
|
|
|
|
|
259 |
|
|
|
|
|
260 |
|
|
|
261 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
262 |
|
263 |
|
264 |
|
@@ -593,16 +713,7 @@ def biz_v():
|
|
593 |
# ОБНОВЛЯЕМ CSV-файла
|
594 |
DATABASE2 = 'data_gc.db'
|
595 |
|
596 |
-
|
597 |
-
full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
|
598 |
-
payload = {"phoneNumber": phone_number}
|
599 |
-
headers = {'Content-Type': 'application/json'}
|
600 |
-
response = requests.post(full_url_ver, headers=headers, json=payload)
|
601 |
-
if response.status_code == 200:
|
602 |
-
response_body = response.json()
|
603 |
-
return response_body.get('existsWhatsapp', 'false')
|
604 |
-
else:
|
605 |
-
return "false"
|
606 |
|
607 |
def parse_csv_data(data):
|
608 |
parsed_data = []
|
|
|
193 |
|
194 |
|
195 |
|
196 |
+
# Добавляем пользователя
|
197 |
+
|
198 |
+
mt_avp = {
|
199 |
+
'name': 'name',
|
200 |
+
'phone': 'phone',
|
201 |
+
'email': 'email',
|
202 |
+
'vk_id': 'vk_id',
|
203 |
+
'utm_campaign': 'utm_campaign',
|
204 |
+
'utm_content': 'utm_content',
|
205 |
+
'utm_medium': 'utm_medium',
|
206 |
+
'utm_source': 'utm_source',
|
207 |
+
'utm_term': 'utm_term',
|
208 |
+
'gcpc': 'gcpc'
|
209 |
+
}
|
210 |
|
211 |
+
mt_bhelp = {
|
212 |
+
'name': 'name',
|
213 |
+
'phone': 'phone',
|
214 |
+
'email': 'email',
|
215 |
+
'vk_id': 'vk_id',
|
216 |
+
'utm_campaign': 'utm_campaign',
|
217 |
+
'utm_content': 'utm_content',
|
218 |
+
'utm_medium': 'utm_medium',
|
219 |
+
'utm_source': 'utm_source',
|
220 |
+
'utm_term': 'utm_term',
|
221 |
+
'gcpc': 'gcpc'
|
222 |
+
}
|
223 |
|
224 |
+
mt_gc = {
|
225 |
+
'name': 'name',
|
226 |
+
'phone': 'phone',
|
227 |
+
'email': 'email',
|
228 |
+
'vk_id': 'vk_id',
|
229 |
+
'utm_campaign': 'utm_campaign',
|
230 |
+
'utm_content': 'utm_content',
|
231 |
+
'utm_medium': 'utm_medium',
|
232 |
+
'utm_source': 'utm_source',
|
233 |
+
'utm_term': 'utm_term',
|
234 |
+
'gcpc': 'gcpc'
|
235 |
+
}
|
236 |
|
237 |
+
mt_tl = {
|
238 |
+
'name': 'name',
|
239 |
+
'phone': 'phone',
|
240 |
+
'email': 'email',
|
241 |
+
'kol': 'pr1',
|
242 |
+
'pr2': 'pr2',
|
243 |
+
'gen_pass': 'pr5',
|
244 |
+
'utm_campaign': 'utm_campaign',
|
245 |
+
'utm_content': 'utm_content',
|
246 |
+
'utm_medium': 'utm_medium',
|
247 |
+
'utm_source': 'utm_source',
|
248 |
+
'utm_term': 'utm_term',
|
249 |
+
'gcpc': 'gcpc'
|
250 |
+
}
|
251 |
|
252 |
+
tl_quest = {
|
253 |
+
'name': 'name',
|
254 |
+
'phone': 'phone',
|
255 |
+
'email': 'email',
|
256 |
+
'pr2': 'pr2',
|
257 |
+
'utm_campaign': 'utm_campaign',
|
258 |
+
'utm_content': 'utm_content',
|
259 |
+
'utm_medium': 'utm_medium',
|
260 |
+
'utm_source': 'utm_source',
|
261 |
+
'utm_term': 'utm_term',
|
262 |
+
'gcpc': 'gcpc'
|
263 |
+
}
|
264 |
|
265 |
+
mapp_templates = {
|
266 |
+
'avp': mt_avp,
|
267 |
+
'bhelp': mt_bhelp,
|
268 |
+
'gc': mt_gc,
|
269 |
+
'tilda': mt_tl,
|
270 |
+
'quest': tl_quest
|
271 |
+
}
|
272 |
|
273 |
+
DATABASE_NAME3 = 'data_gc.db'
|
274 |
+
def verify_phone_number(phone_number):
|
275 |
+
full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
|
276 |
+
payload = {"phoneNumber": phone_number}
|
277 |
+
headers = {'Content-Type': 'application/json'}
|
278 |
+
response = requests.post(full_url_ver, headers=headers, json=payload)
|
279 |
+
if response.status_code == 200:
|
280 |
+
response_body = response.json()
|
281 |
+
return response_body.get('existsWhatsapp', 'false')
|
282 |
+
else:
|
283 |
+
return "false"
|
284 |
|
285 |
+
def generate_password(length=8):
|
286 |
+
letters_and_digits = string.ascii_letters + string.digits
|
287 |
+
return ''.join(random.choice(letters_and_digits) for i in range(length))
|
288 |
|
289 |
+
def add_or_update_contact(contact_data, db_name):
|
290 |
+
conn = sqlite3.connect(db_name)
|
291 |
+
cursor = conn.cursor()
|
292 |
|
293 |
+
email = contact_data.get('email')
|
294 |
+
if not email:
|
295 |
+
logging.error(f"Missing email in contact data: {contact_data}")
|
296 |
+
return
|
297 |
|
298 |
+
utc_now = datetime.utcnow()
|
299 |
+
msk_tz = pytz.timezone('Europe/Moscow')
|
300 |
+
msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
|
301 |
+
contact_data['data_t'] = msk_now.strftime('%Y-%m-%d %H:%M:%S')
|
302 |
|
303 |
+
fields = [
|
304 |
+
'name', 'phone', 'email', 'vk_id', 'chat_id', 'ws_st', 'ws_stop', 'web_st', 'fin_prog',
|
305 |
+
'b_city', 'b_fin', 'b_ban', 'b_ign', 'b_baners', 'b_butt', 'b_mess', 'orders', 'curator',
|
306 |
+
'pr1', 'pr2', 'pr3', 'pr4', 'pr5', 'gc_url', 'key_pr', 'n_con', 'canal', 'data_on', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gcpc'
|
307 |
+
]
|
308 |
|
309 |
+
for field in fields:
|
310 |
+
if field not in contact_data:
|
311 |
+
contact_data[field] = ''
|
312 |
|
313 |
+
cursor.execute("SELECT id FROM contacts WHERE email = ?", (email,))
|
314 |
+
contact = cursor.fetchone()
|
315 |
|
316 |
+
if contact:
|
317 |
+
update_fields = [f"{field} = ?" for field in fields if contact_data[field] != '']
|
318 |
+
update_values = [contact_data[field] for field in fields if contact_data[field] != '']
|
319 |
+
update_values.append(contact[0])
|
320 |
+
update_query = f"UPDATE contacts SET {', '.join(update_fields)} WHERE id = ?"
|
321 |
+
cursor.execute(update_query, update_values)
|
322 |
+
else:
|
323 |
+
insert_query = f"INSERT INTO contacts ({', '.join(fields)}) VALUES ({', '.join(['?' for _ in fields])})"
|
324 |
+
cursor.execute(insert_query, tuple(contact_data[field] for field in fields))
|
325 |
+
|
326 |
+
conn.commit()
|
327 |
+
conn.close()
|
328 |
|
|
|
329 |
|
|
|
|
|
|
|
330 |
|
331 |
+
@app.route('/ver_cur', methods=['GET'])
|
332 |
+
def add_data_ver_cur():
|
333 |
+
global current_curator_index
|
334 |
|
335 |
+
veref_on_off = request.args.get('ver', '0')
|
336 |
+
curator_on_off = request.args.get('cur', '0')
|
337 |
+
gen_pass_start = request.args.get('pass', '0')
|
338 |
+
db_name = request.args.get('db', 'data_gc.db') # Получаем имя базы данных из запроса
|
339 |
|
340 |
+
template_key = request.args.get('template_key', 'avp')
|
341 |
+
mapping_template_cur = mapp_templates.get(template_key, mt_avp)
|
342 |
|
343 |
+
user_data = {mapping_template_cur[key]: request.args.get(key, "") for key in mapping_template_cur}
|
344 |
|
345 |
+
logging.debug(f"Received data: {user_data}")
|
346 |
+
|
347 |
+
if curator_on_off == "1":
|
348 |
+
user_data['curator'] = curators[current_curator_index]
|
349 |
+
current_curator_index = (current_curator_index + 1) % len(curators)
|
350 |
+
else:
|
351 |
+
user_data['curator'] = user_data.get('curator', '')
|
352 |
+
|
353 |
+
if veref_on_off == "1":
|
354 |
+
phone_number = user_data.get('phone', '')
|
355 |
+
if not phone_number:
|
356 |
+
logging.error("Phone number is empty")
|
357 |
+
return jsonify({'status': 'error', 'message': 'Phone number is empty'}), 400
|
358 |
+
|
359 |
+
phone_verification_response = verify_phone_number(phone_number)
|
360 |
+
if phone_verification_response is not None:
|
361 |
+
user_data['ws_st'] = '1' if phone_verification_response else '0'
|
362 |
+
else:
|
363 |
+
user_data['ws_st'] = user_data.get('ws_st', '')
|
364 |
+
|
365 |
+
if gen_pass_start == "1":
|
366 |
+
user_data['pr5'] = generate_password()
|
367 |
+
else:
|
368 |
+
conn = sqlite3.connect(db_name)
|
369 |
+
cursor = conn.cursor()
|
370 |
+
cursor.execute("SELECT pr5 FROM contacts WHERE email = ?", (user_data['email'],))
|
371 |
+
result = cursor.fetchone()
|
372 |
+
if result and result[0]:
|
373 |
+
user_data['pr5'] = result[0]
|
374 |
+
conn.close()
|
375 |
+
|
376 |
+
try:
|
377 |
+
add_or_update_contact(user_data, db_name)
|
378 |
+
return jsonify({'status': 'success', 'message': f'User added with curator {user_data.get("curator", "not assigned")}', 'password': user_data['pr5']})
|
379 |
+
except Exception as e:
|
380 |
+
logging.error(f"Error adding user: {e}")
|
381 |
+
return jsonify({'status': 'error', 'message': str(e)}), 500
|
382 |
|
383 |
|
384 |
|
|
|
713 |
# ОБНОВЛЯЕМ CSV-файла
|
714 |
DATABASE2 = 'data_gc.db'
|
715 |
|
716 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
717 |
|
718 |
def parse_csv_data(data):
|
719 |
parsed_data = []
|