DmitrMakeev
commited on
Commit
•
95f2e24
1
Parent(s):
6696607
Update app.py
Browse files
app.py
CHANGED
@@ -1737,6 +1737,153 @@ def add_data_ver_cur():
|
|
1737 |
|
1738 |
|
1739 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1740 |
|
1741 |
|
1742 |
|
|
|
1737 |
|
1738 |
|
1739 |
|
1740 |
+
|
1741 |
+
|
1742 |
+
|
1743 |
+
|
1744 |
+
|
1745 |
+
|
1746 |
+
|
1747 |
+
|
1748 |
+
|
1749 |
+
|
1750 |
+
|
1751 |
+
|
1752 |
+
|
1753 |
+
|
1754 |
+
|
1755 |
+
|
1756 |
+
|
1757 |
+
|
1758 |
+
|
1759 |
+
|
1760 |
+
|
1761 |
+
# Шаблон сопоставления для кураторов
|
1762 |
+
mapping_template_cur = {
|
1763 |
+
'name': 'name',
|
1764 |
+
'phone': 'phone',
|
1765 |
+
'email': 'email'
|
1766 |
+
}
|
1767 |
+
|
1768 |
+
DATABASE_NAME4 = 'data_gc.db'
|
1769 |
+
|
1770 |
+
|
1771 |
+
def verify_phone_number_v2(phone_number):
|
1772 |
+
print(f"verifikation_start: {verifikation_start}")
|
1773 |
+
|
1774 |
+
if verifikation_start == "1":
|
1775 |
+
full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
|
1776 |
+
print(f"Full URL: {full_url_ver}")
|
1777 |
+
|
1778 |
+
# Создаем payload для запроса
|
1779 |
+
payload = json.dumps({
|
1780 |
+
"phoneNumber": phone_number
|
1781 |
+
})
|
1782 |
+
headers = {
|
1783 |
+
'Content-Type': 'application/json'
|
1784 |
+
}
|
1785 |
+
|
1786 |
+
# Отправляем POST-запрос
|
1787 |
+
response = requests.post(full_url_ver, headers=headers, data=payload)
|
1788 |
+
|
1789 |
+
# Печатаем статус ответа
|
1790 |
+
print(f"Response Status Code: {response.status_code}")
|
1791 |
+
|
1792 |
+
# Проверяем статус код ответа
|
1793 |
+
if response.status_code == 200:
|
1794 |
+
# Печатаем текст ответа от сервера
|
1795 |
+
response_body = response.json()
|
1796 |
+
print(f"Response Body: {response_body}")
|
1797 |
+
|
1798 |
+
# Извлекаем значение из JSON
|
1799 |
+
exists_whatsapp = response_body.get('existsWhatsapp', 'false')
|
1800 |
+
print(f"existsWhatsapp: {exists_whatsapp}")
|
1801 |
+
|
1802 |
+
# Возвращаем значение
|
1803 |
+
return exists_whatsapp
|
1804 |
+
else:
|
1805 |
+
print("Error: Unable to fetch data")
|
1806 |
+
return "Error"
|
1807 |
+
else:
|
1808 |
+
print("Verification not started")
|
1809 |
+
return "false" # Возвращаем значение, что WhatsApp не существует
|
1810 |
+
|
1811 |
+
def add_or_update_contact(contact_data):
|
1812 |
+
conn = sqlite3.connect(DATABASE_NAME4)
|
1813 |
+
cursor = conn.cursor()
|
1814 |
+
|
1815 |
+
email = contact_data.get('email')
|
1816 |
+
if not email:
|
1817 |
+
logging.error(f"Missing email in contact data: {contact_data}")
|
1818 |
+
return
|
1819 |
+
|
1820 |
+
# Добавление текущей даты и времени
|
1821 |
+
utc_now = datetime.utcnow()
|
1822 |
+
msk_tz = pytz.timezone('Europe/Moscow')
|
1823 |
+
msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
|
1824 |
+
contact_data['data_t'] = msk_now.strftime('%Y-%m-%d %H:%M:%S')
|
1825 |
+
|
1826 |
+
cursor.execute("SELECT id FROM contacts WHERE email = ?", (email,))
|
1827 |
+
contact = cursor.fetchone()
|
1828 |
+
|
1829 |
+
fields = [
|
1830 |
+
'name', 'phone', 'email', 'vk_id', 'chat_id', 'ws_st', 'ws_stop', 'web_st', 'fin_prog',
|
1831 |
+
'b_city', 'b_fin', 'b_ban', 'b_ign', 'b_baners', 'b_butt', 'b_mess', 'shop_st', 'curator',
|
1832 |
+
'pr1', 'pr2', 'pr3', 'pr4', 'pr5', 'ad_url', 'key_pr', 'n_con', 'canal', 'data_t'
|
1833 |
+
]
|
1834 |
+
|
1835 |
+
placeholders = ", ".join([f"{field} = ?" for field in fields])
|
1836 |
+
|
1837 |
+
if contact:
|
1838 |
+
update_query = f"UPDATE contacts SET {placeholders} WHERE id = ?"
|
1839 |
+
cursor.execute(update_query, (*[contact_data.get(field, '') for field in fields], contact[0]))
|
1840 |
+
else:
|
1841 |
+
insert_query = f"INSERT INTO contacts ({', '.join(fields)}) VALUES ({', '.join(['?' for _ in fields])})"
|
1842 |
+
cursor.execute(insert_query, tuple(contact_data.get(field, '') for field in fields))
|
1843 |
+
|
1844 |
+
conn.commit()
|
1845 |
+
conn.close()
|
1846 |
+
|
1847 |
+
@app.route('/add_data_bot', methods=['GET'])
|
1848 |
+
def add_data_bot():
|
1849 |
+
# Получаем данные пользователя из параметров запроса
|
1850 |
+
user_data = {key: request.args.get(key, "") for key in mapping_template_cur.keys()}
|
1851 |
+
|
1852 |
+
# Верификация номера телефона
|
1853 |
+
phone_verification_response = verify_phone_number_v2(user_data['phone'])
|
1854 |
+
if phone_verification_response is not None:
|
1855 |
+
user_data['ws_st'] = phone_verification_response
|
1856 |
+
|
1857 |
+
try:
|
1858 |
+
# Добавляем пользователя в базу данных
|
1859 |
+
add_or_update_contact(user_data)
|
1860 |
+
return jsonify({'status': 'success', 'message': 'User added'})
|
1861 |
+
except Exception as e:
|
1862 |
+
logging.error(f"Error adding user: {e}")
|
1863 |
+
return jsonify({'status': 'error', 'message': str(e)}), 500
|
1864 |
+
|
1865 |
+
|
1866 |
+
|
1867 |
+
|
1868 |
+
|
1869 |
+
|
1870 |
+
|
1871 |
+
|
1872 |
+
|
1873 |
+
|
1874 |
+
|
1875 |
+
|
1876 |
+
|
1877 |
+
|
1878 |
+
|
1879 |
+
|
1880 |
+
|
1881 |
+
|
1882 |
+
|
1883 |
+
|
1884 |
+
|
1885 |
+
|
1886 |
+
|
1887 |
|
1888 |
|
1889 |
|