DmitrMakeev commited on
Commit
9142d0c
·
verified ·
1 Parent(s): d913fc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -27
app.py CHANGED
@@ -48,7 +48,7 @@ code_executed = False
48
  status = "active"
49
 
50
 
51
-
52
 
53
 
54
 
@@ -1632,16 +1632,15 @@ mapping_template_cur = {
1632
  }
1633
 
1634
  DATABASE_NAME3 = 'data_gc.db'
1635
- verifikation_start = "1" # Пример значения, которое должно быть установлено из конфигурации
1636
-
1637
-
1638
  def verify_phone_number(phone_number):
1639
- print(f"Starting phone verification for number: {phone_number}")
1640
  print(f"verifikation_start: {verifikation_start}")
1641
 
1642
  if verifikation_start == "1":
 
 
1643
  full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
1644
- print(f"Full URL for verification: {full_url_ver}")
1645
 
1646
  # Создаем payload для запроса
1647
  payload = json.dumps({
@@ -1655,13 +1654,13 @@ def verify_phone_number(phone_number):
1655
  response = requests.post(full_url_ver, headers=headers, data=payload)
1656
 
1657
  # Печатаем статус ответа
1658
- print(f"Response Status Code from verification server: {response.status_code}")
1659
 
1660
  # Проверяем статус код ответа
1661
  if response.status_code == 200:
1662
  # Печатаем текст ответа от сервера
1663
  response_body = response.json()
1664
- print(f"Response Body from verification server: {response_body}")
1665
 
1666
  # Извлекаем значение из JSON
1667
  exists_whatsapp = response_body.get('existsWhatsapp', 'false')
@@ -1670,35 +1669,44 @@ def verify_phone_number(phone_number):
1670
  # Возвращаем значение
1671
  return exists_whatsapp
1672
  else:
1673
- print("Error: Unable to fetch data from verification server")
1674
  return "Error"
1675
  else:
1676
  print("Verification not started")
1677
  return "false" # Возвращаем значение, что WhatsApp не существует
1678
-
1679
- def add_user_to_db(db_name, user_data):
1680
- conn = sqlite3.connect(db_name)
1681
  cursor = conn.cursor()
1682
 
1683
- # Преобразование данных пользователя на основе шаблона сопоставления
1684
- transformed_data = {db_column: user_data.get(json_key, "") for json_key, db_column in mapping_template_cur.items()}
 
 
1685
 
1686
- # Добавление недостающих полей с пустыми строками
1687
- required_fields = [
1688
- "ad_url", "b_ban", "b_baners", "b_butt", "b_city", "b_fin", "b_ign", "b_mess",
1689
- "canal", "chat_id", "curator", "data_t", "fin_prog", "key_pr", "n_con",
1690
- "pr1", "pr2", "pr3", "pr4", "pr5", "shop_st", "vk_id", "web_st", "ws_st", "ws_stop"
 
 
 
 
 
 
 
 
1691
  ]
1692
- for field in required_fields:
1693
- if field not in transformed_data:
1694
- transformed_data[field] = ""
1695
 
1696
- columns = ', '.join(transformed_data.keys())
1697
- placeholders = ', '.join('?' for _ in transformed_data)
1698
- insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
1699
- insert_values = list(transformed_data.values())
 
 
1700
 
1701
- cursor.execute(insert_query, insert_values)
1702
  conn.commit()
1703
  conn.close()
1704
 
 
48
  status = "active"
49
 
50
 
51
+ verifikation_start = "1"
52
 
53
 
54
 
 
1632
  }
1633
 
1634
  DATABASE_NAME3 = 'data_gc.db'
 
 
 
1635
  def verify_phone_number(phone_number):
1636
+
1637
  print(f"verifikation_start: {verifikation_start}")
1638
 
1639
  if verifikation_start == "1":
1640
+
1641
+
1642
  full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
1643
+ print(f"Full URL: {full_url_ver}")
1644
 
1645
  # Создаем payload для запроса
1646
  payload = json.dumps({
 
1654
  response = requests.post(full_url_ver, headers=headers, data=payload)
1655
 
1656
  # Печатаем статус ответа
1657
+ print(f"Response Status Code: {response.status_code}")
1658
 
1659
  # Проверяем статус код ответа
1660
  if response.status_code == 200:
1661
  # Печатаем текст ответа от сервера
1662
  response_body = response.json()
1663
+ print(f"Response Body: {response_body}")
1664
 
1665
  # Извлекаем значение из JSON
1666
  exists_whatsapp = response_body.get('existsWhatsapp', 'false')
 
1669
  # Возвращаем значение
1670
  return exists_whatsapp
1671
  else:
1672
+ print("Error: Unable to fetch data")
1673
  return "Error"
1674
  else:
1675
  print("Verification not started")
1676
  return "false" # Возвращаем значение, что WhatsApp не существует
1677
+ def add_or_update_contact(contact_data):
1678
+ conn = sqlite3.connect(DATABASE_NAME3)
 
1679
  cursor = conn.cursor()
1680
 
1681
+ email = contact_data.get('email')
1682
+ if not email:
1683
+ logging.error(f"Missing email in contact data: {contact_data}")
1684
+ return
1685
 
1686
+ # Добавление текущей даты и времени
1687
+ utc_now = datetime.utcnow()
1688
+ msk_tz = pytz.timezone('Europe/Moscow')
1689
+ msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
1690
+ contact_data['data_t'] = msk_now.strftime('%Y-%m-%d %H:%M:%S')
1691
+
1692
+ cursor.execute("SELECT id FROM contacts WHERE email = ?", (email,))
1693
+ contact = cursor.fetchone()
1694
+
1695
+ fields = [
1696
+ 'name', 'phone', 'email', 'vk_id', 'chat_id', 'ws_st', 'ws_stop', 'web_st', 'fin_prog',
1697
+ 'b_city', 'b_fin', 'b_ban', 'b_ign', 'b_baners', 'b_butt', 'b_mess', 'shop_st', 'curator',
1698
+ 'pr1', 'pr2', 'pr3', 'pr4', 'pr5', 'ad_url', 'key_pr', 'n_con', 'canal', 'data_t'
1699
  ]
1700
+
1701
+ placeholders = ", ".join([f"{field} = ?" for field in fields])
 
1702
 
1703
+ if contact:
1704
+ update_query = f"UPDATE contacts SET {placeholders} WHERE id = ?"
1705
+ cursor.execute(update_query, (*[contact_data.get(field, '') for field in fields], contact[0]))
1706
+ else:
1707
+ insert_query = f"INSERT INTO contacts ({', '.join(fields)}) VALUES ({', '.join(['?' for _ in fields])})"
1708
+ cursor.execute(insert_query, tuple(contact_data.get(field, '') for field in fields))
1709
 
 
1710
  conn.commit()
1711
  conn.close()
1712