DmitrMakeev commited on
Commit
e96c5e1
1 Parent(s): 92675a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -0
app.py CHANGED
@@ -1627,6 +1627,160 @@ def gc_forms():
1627
 
1628
 
1629
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1630
 
1631
 
1632
 
 
1627
 
1628
 
1629
 
1630
+ # Функция для проверки номера телефона
1631
+ def verify_phone_number(phone_number):
1632
+ verifikation_start = os.getenv('verifikation_start')
1633
+
1634
+ if verifikation_start == "1":
1635
+
1636
+
1637
+ full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
1638
+
1639
+ payload = json.dumps({
1640
+ "phoneNumber": phone_number
1641
+ })
1642
+ headers = {
1643
+ 'Content-Type': 'application/json'
1644
+ }
1645
+
1646
+ response = requests.post(full_url_ver, headers=headers, data=payload)
1647
+
1648
+ if response.status_code == 200:
1649
+ result = response.json()
1650
+ return result
1651
+ else:
1652
+ return {"existsWhatsapp": False}
1653
+ else:
1654
+ return {"existsWhatsapp": False}
1655
+
1656
+ # Flask route для добавления или обновления контакта с проверкой номера
1657
+ @app.route('/add_data_ver', methods=['GET'])
1658
+ def add_data_ver():
1659
+ try:
1660
+ api_sys_control = request.args.get('api_sys')
1661
+
1662
+ if api_sys_control != api_key_sys:
1663
+ return "err api key", 200
1664
+
1665
+ name = request.args.get('name')
1666
+ phone = request.args.get('phone')
1667
+ email = request.args.get('email')
1668
+ pr1 = request.args.get('pr1')
1669
+ pr2 = request.args.get('pr2')
1670
+ pr3 = request.args.get('pr3')
1671
+ pr4 = request.args.get('pr4')
1672
+ pr5 = request.args.get('pr5')
1673
+ canal = request.args.get('canal', '')
1674
+
1675
+ if not name or not phone or not email:
1676
+ return "Parameters 'name', 'phone', and 'email' are required.", 400
1677
+
1678
+ # Очистка номера телефона от знака "+"
1679
+ if phone.startswith('+'):
1680
+ phone = phone[1:]
1681
+
1682
+ # Верификация номера телефона
1683
+ verification_result = verify_phone_number(phone)
1684
+ ws_status = verification_result.get('existsWhatsapp', False)
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
+ data_t = msk_now.strftime('%Y-%m-%d %H:%M:%S')
1691
+
1692
+ conn = sqlite3.connect('data_gc.db')
1693
+ cursor = conn.cursor()
1694
+ cursor.execute('SELECT * FROM contacts WHERE phone = ? OR email = ?', (phone, email))
1695
+ existing_contact = cursor.fetchone()
1696
+
1697
+ if existing_contact:
1698
+ update_query = '''
1699
+ UPDATE contacts SET
1700
+ name = ?, email = ?, vk_id = ?, chat_id = ?, ws_st = ?, ws_stop = ?,
1701
+ web_st = ?, fin_prog = ?, shop_st = ?, ad_url = ?, curator = ?, key_pr = ?, n_con = ?,
1702
+ canal = ?, data_t = ?
1703
+ '''
1704
+ params = [name, email, vk_id, chat_id, ws_status, ws_stop, web_statys, fin_progress,
1705
+ shop_statys_full, ad_url, curator, key_pr, n_con, canal, data_t]
1706
+
1707
+ if pr1 is not None:
1708
+ update_query += ", pr1 = ?"
1709
+ params.append(pr1)
1710
+ if pr2 is not None:
1711
+ update_query += ", pr2 = ?"
1712
+ params.append(pr2)
1713
+ if pr3 is not None:
1714
+ update_query += ", pr3 = ?"
1715
+ params.append(pr3)
1716
+ if pr4 is not None:
1717
+ update_query += ", pr4 = ?"
1718
+ params.append(pr4)
1719
+ if pr5 is not None:
1720
+ update_query += ", pr5 = ?"
1721
+ params.append(pr5)
1722
+
1723
+ update_query += " WHERE phone = ? OR email = ?"
1724
+ params.extend([phone, email])
1725
+
1726
+ cursor.execute(update_query, params)
1727
+ else:
1728
+ insert_query = '''
1729
+ INSERT INTO contacts (
1730
+ name, phone, email, vk_id, chat_id, ws_st, ws_stop, web_st, fin_prog,
1731
+ shop_st, ad_url, curator, key_pr, n_con, canal, data_t
1732
+ '''
1733
+ params = [name, phone, email, vk_id, chat_id, ws_status, ws_stop, web_statys, fin_progress,
1734
+ shop_statys_full, ad_url, curator, key_pr, n_con, canal, data_t]
1735
+
1736
+ if pr1 is not None:
1737
+ insert_query += ", pr1"
1738
+ params.append(pr1)
1739
+ if pr2 is not None:
1740
+ insert_query += ", pr2"
1741
+ params.append(pr2)
1742
+ if pr3 is not None:
1743
+ insert_query += ", pr3"
1744
+ params.append(pr3)
1745
+ if pr4 is not None:
1746
+ insert_query += ", pr4"
1747
+ params.append(pr4)
1748
+ if pr5 is not None:
1749
+ insert_query += ", pr5"
1750
+ params.append(pr5)
1751
+
1752
+ insert_query += ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?"
1753
+ placeholders = ["?"] * len(params)
1754
+
1755
+ if pr1 is not None:
1756
+ insert_query += ", ?"
1757
+ placeholders.append("?")
1758
+ if pr2 is not None:
1759
+ insert_query += ", ?"
1760
+ placeholders.append("?")
1761
+ if pr3 is not None:
1762
+ insert_query += ", ?"
1763
+ placeholders.append("?")
1764
+ if pr4 is not None:
1765
+ insert_query += ", ?"
1766
+ placeholders.append("?")
1767
+ if pr5 is not None:
1768
+ insert_query += ", ?"
1769
+ placeholders.append("?")
1770
+
1771
+ insert_query += ")"
1772
+ placeholders.append("?)")
1773
+ insert_query = insert_query.replace("?", ", ".join(placeholders))
1774
+
1775
+ cursor.execute(insert_query, params)
1776
+
1777
+ conn.commit()
1778
+ conn.close()
1779
+
1780
+ return f"Contact updated/added: {name} - {phone} - {email}", 200
1781
+ except Exception as e:
1782
+ print(f"Error adding/updating contact: {e}")
1783
+ return "Internal Server Error", 500
1784
 
1785
 
1786