Update app.py
Browse files
app.py
CHANGED
|
@@ -48,7 +48,7 @@ code_executed = False
|
|
| 48 |
status = "active"
|
| 49 |
|
| 50 |
|
| 51 |
-
|
| 52 |
|
| 53 |
|
| 54 |
|
|
@@ -1617,14 +1617,29 @@ def gc_forms():
|
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
-
DATABASE_NAME3 = 'data_gc.db'
|
| 1621 |
-
def verify_phone_number(phone_number):
|
| 1622 |
|
| 1623 |
-
print(f"verifikation_start: {verifikation_start}")
|
| 1624 |
|
| 1625 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1626 |
|
| 1627 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1628 |
full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
|
| 1629 |
print(f"Full URL: {full_url_ver}")
|
| 1630 |
|
|
@@ -1660,177 +1675,6 @@ def verify_phone_number(phone_number):
|
|
| 1660 |
else:
|
| 1661 |
print("Verification not started")
|
| 1662 |
return "false" # Возвращаем значение, что WhatsApp не существует
|
| 1663 |
-
def add_or_update_contact(contact_data):
|
| 1664 |
-
conn = sqlite3.connect(DATABASE_NAME3)
|
| 1665 |
-
cursor = conn.cursor()
|
| 1666 |
-
|
| 1667 |
-
email = contact_data.get('email')
|
| 1668 |
-
if not email:
|
| 1669 |
-
logging.error(f"Missing email in contact data: {contact_data}")
|
| 1670 |
-
return
|
| 1671 |
-
|
| 1672 |
-
# Добавление текущей даты и времени
|
| 1673 |
-
utc_now = datetime.utcnow()
|
| 1674 |
-
msk_tz = pytz.timezone('Europe/Moscow')
|
| 1675 |
-
msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
|
| 1676 |
-
contact_data['data_t'] = msk_now.strftime('%Y-%m-%d %H:%M:%S')
|
| 1677 |
-
|
| 1678 |
-
cursor.execute("SELECT id FROM contacts WHERE email = ?", (email,))
|
| 1679 |
-
contact = cursor.fetchone()
|
| 1680 |
-
|
| 1681 |
-
fields = [
|
| 1682 |
-
'name', 'phone', 'email', 'vk_id', 'chat_id', 'ws_st', 'ws_stop', 'web_st', 'fin_prog',
|
| 1683 |
-
'b_city', 'b_fin', 'b_ban', 'b_ign', 'b_baners', 'b_butt', 'b_mess', 'shop_st', 'curator',
|
| 1684 |
-
'pr1', 'pr2', 'pr3', 'pr4', 'pr5', 'ad_url', 'key_pr', 'n_con', 'canal', 'data_t'
|
| 1685 |
-
]
|
| 1686 |
-
|
| 1687 |
-
placeholders = ", ".join([f"{field} = ?" for field in fields])
|
| 1688 |
-
|
| 1689 |
-
if contact:
|
| 1690 |
-
update_query = f"UPDATE contacts SET {placeholders} WHERE id = ?"
|
| 1691 |
-
cursor.execute(update_query, (*[contact_data.get(field, '') for field in fields], contact[0]))
|
| 1692 |
-
else:
|
| 1693 |
-
insert_query = f"INSERT INTO contacts ({', '.join(fields)}) VALUES ({', '.join(['?' for _ in fields])})"
|
| 1694 |
-
cursor.execute(insert_query, tuple(contact_data.get(field, '') for field in fields))
|
| 1695 |
-
|
| 1696 |
-
conn.commit()
|
| 1697 |
-
conn.close()
|
| 1698 |
-
|
| 1699 |
-
@app.route('/add_data_ver', methods=['GET'])
|
| 1700 |
-
def add_data_ver():
|
| 1701 |
-
contact_data = {
|
| 1702 |
-
'name': request.args.get('name', ''),
|
| 1703 |
-
'phone': request.args.get('phone', ''),
|
| 1704 |
-
'email': request.args.get('email', ''),
|
| 1705 |
-
'vk_id': request.args.get('vk_id', ''),
|
| 1706 |
-
'chat_id': request.args.get('chat_id', ''),
|
| 1707 |
-
'ws_st': request.args.get('ws_st', ''),
|
| 1708 |
-
'ws_stop': request.args.get('ws_stop', ''),
|
| 1709 |
-
'web_st': request.args.get('web_st', ''),
|
| 1710 |
-
'fin_prog': request.args.get('fin_prog', '5'),
|
| 1711 |
-
'b_city': request.args.get('b_city', ''),
|
| 1712 |
-
'b_fin': request.args.get('b_fin', ''),
|
| 1713 |
-
'b_ban': request.args.get('b_ban', ''),
|
| 1714 |
-
'b_ign': request.args.get('b_ign', ''),
|
| 1715 |
-
'b_baners': request.args.get('b_baners', ''),
|
| 1716 |
-
'b_butt': request.args.get('b_butt', ''),
|
| 1717 |
-
'b_mess': request.args.get('b_mess', ''),
|
| 1718 |
-
'shop_st': request.args.get('shop_st', ''),
|
| 1719 |
-
'curator': request.args.get('curator', ''),
|
| 1720 |
-
'pr1': request.args.get('pr1', ''),
|
| 1721 |
-
'pr2': request.args.get('pr2', ''),
|
| 1722 |
-
'pr3': request.args.get('pr3', ''),
|
| 1723 |
-
'pr4': request.args.get('pr4', ''),
|
| 1724 |
-
'pr5': request.args.get('pr5', ''),
|
| 1725 |
-
'ad_url': request.args.get('ad_url', ''),
|
| 1726 |
-
'key_pr': request.args.get('key_pr', ''),
|
| 1727 |
-
'n_con': request.args.get('n_con', ''),
|
| 1728 |
-
'canal': request.args.get('canal', ''),
|
| 1729 |
-
'data_t': '' # Будет заполнено в функции add_or_update_contact
|
| 1730 |
-
}
|
| 1731 |
-
|
| 1732 |
-
phone_verification_response = verify_phone_number(contact_data['phone'])
|
| 1733 |
-
if phone_verification_response is not None:
|
| 1734 |
-
contact_data['ws_st'] = phone_verification_response
|
| 1735 |
-
|
| 1736 |
-
try:
|
| 1737 |
-
add_or_update_contact(contact_data)
|
| 1738 |
-
return jsonify({'status': 'success'})
|
| 1739 |
-
except Exception as e:
|
| 1740 |
-
logging.error(f"Error adding/updating contact: {e}")
|
| 1741 |
-
return jsonify({'status': 'error', 'message': str(e)}), 500
|
| 1742 |
-
|
| 1743 |
-
|
| 1744 |
-
|
| 1745 |
-
|
| 1746 |
-
|
| 1747 |
-
|
| 1748 |
-
|
| 1749 |
-
|
| 1750 |
-
|
| 1751 |
-
|
| 1752 |
-
|
| 1753 |
-
|
| 1754 |
-
|
| 1755 |
-
|
| 1756 |
-
|
| 1757 |
-
|
| 1758 |
-
|
| 1759 |
-
|
| 1760 |
-
|
| 1761 |
-
# Пример шаблона сопоставления для настройки переменных записываемых в базу данных. - Не проверин
|
| 1762 |
-
mapping_template3 = {
|
| 1763 |
-
'name': 'name',
|
| 1764 |
-
'phone': 'phone',
|
| 1765 |
-
'email': 'email'
|
| 1766 |
-
}
|
| 1767 |
-
|
| 1768 |
-
def add_user_bot(db_name, user_data, mapping_template3):
|
| 1769 |
-
conn = sqlite3.connect(db_name)
|
| 1770 |
-
cursor = conn.cursor()
|
| 1771 |
-
|
| 1772 |
-
# Проверка наличия пользователя по email
|
| 1773 |
-
email = user_data.get('email')
|
| 1774 |
-
cursor.execute("SELECT 1 FROM contacts WHERE email = ?", (email,))
|
| 1775 |
-
if cursor.fetchone() is not None:
|
| 1776 |
-
logging.warning(f"User with email {email} already exists. Skipping insert.")
|
| 1777 |
-
conn.close()
|
| 1778 |
-
return
|
| 1779 |
-
|
| 1780 |
-
# Преобразование данных пользователя на основе шаблона сопоставления
|
| 1781 |
-
transformed_data = {db_column: user_data.get(json_key, "") for json_key, db_column in mapping_template3.items()}
|
| 1782 |
-
|
| 1783 |
-
# Добавление недостающих полей с пустыми строками
|
| 1784 |
-
required_fields = [
|
| 1785 |
-
"ad_url", "b_ban", "b_baners", "b_butt", "b_city", "b_fin", "b_ign", "b_mess",
|
| 1786 |
-
"canal", "chat_id", "curator", "data_t", "fin_prog", "key_pr", "n_con",
|
| 1787 |
-
"pr1", "pr2", "pr3", "pr4", "pr5", "shop_st", "vk_id", "web_st", "ws_st", "ws_stop"
|
| 1788 |
-
]
|
| 1789 |
-
for field in required_fields:
|
| 1790 |
-
if field not in transformed_data:
|
| 1791 |
-
transformed_data[field] = ""
|
| 1792 |
-
|
| 1793 |
-
columns = ', '.join(transformed_data.keys())
|
| 1794 |
-
placeholders = ', '.join('?' for _ in transformed_data)
|
| 1795 |
-
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
| 1796 |
-
insert_values = list(transformed_data.values())
|
| 1797 |
-
|
| 1798 |
-
cursor.execute(insert_query, insert_values)
|
| 1799 |
-
conn.commit()
|
| 1800 |
-
conn.close()
|
| 1801 |
-
|
| 1802 |
-
@app.route('/add_user_bot', methods=['GET'])
|
| 1803 |
-
def add_user_bot_route():
|
| 1804 |
-
db_name = request.args.get('db_name', 'data_gc.db') # Имя базы данных из параметра запроса
|
| 1805 |
-
user_data = {key: request.args.get(key, "") for key in mapping_template3.keys()}
|
| 1806 |
-
|
| 1807 |
-
logging.debug(f"User data: {user_data}")
|
| 1808 |
-
|
| 1809 |
-
try:
|
| 1810 |
-
add_user_bot(db_name, user_data, mapping_template3)
|
| 1811 |
-
return jsonify({'status': 'success', 'message': f'User added to {db_name}'})
|
| 1812 |
-
except Exception as e:
|
| 1813 |
-
logging.error(f"Error adding user: {e}")
|
| 1814 |
-
return jsonify({'status': 'error', 'message': str(e)}), 500
|
| 1815 |
-
|
| 1816 |
-
|
| 1817 |
-
|
| 1818 |
-
# Конец примера шаблона сопоставления для настройки переменных записываемых в базу данных.
|
| 1819 |
-
|
| 1820 |
-
|
| 1821 |
-
# Список кураторов
|
| 1822 |
-
curators = ["Anna", "Ekaterina", "Ivan", "Maria", "Sergey", "Olga", "Alex", "Natalia", "Dmitry", "Elena"]
|
| 1823 |
-
|
| 1824 |
-
# Переменная для отслеживания текущего куратора
|
| 1825 |
-
current_curator_index = 0
|
| 1826 |
-
|
| 1827 |
-
# Шаблон сопоставления для кураторов
|
| 1828 |
-
mapping_template_cur = {
|
| 1829 |
-
'name': 'name',
|
| 1830 |
-
'phone': 'phone',
|
| 1831 |
-
'email': 'email',
|
| 1832 |
-
'curator': 'curator'
|
| 1833 |
-
}
|
| 1834 |
|
| 1835 |
def add_user_to_db(db_name, user_data):
|
| 1836 |
conn = sqlite3.connect(db_name)
|
|
@@ -1858,8 +1702,8 @@ def add_user_to_db(db_name, user_data):
|
|
| 1858 |
conn.commit()
|
| 1859 |
conn.close()
|
| 1860 |
|
| 1861 |
-
@app.route('/
|
| 1862 |
-
def
|
| 1863 |
global current_curator_index
|
| 1864 |
|
| 1865 |
# Получаем данные пользователя из параметров запроса
|
|
@@ -1868,14 +1712,19 @@ def add_user_cur_route():
|
|
| 1868 |
# Назначаем текущего куратора
|
| 1869 |
user_data['curator'] = curators[current_curator_index]
|
| 1870 |
|
| 1871 |
-
#
|
| 1872 |
-
|
| 1873 |
-
|
| 1874 |
-
|
| 1875 |
-
current_curator_index = (current_curator_index + 1) % len(curators)
|
| 1876 |
-
|
| 1877 |
-
return jsonify({'status': 'success', 'message': f'User added with curator {user_data["curator"]}'})
|
| 1878 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
|
|
|
| 48 |
status = "active"
|
| 49 |
|
| 50 |
|
| 51 |
+
|
| 52 |
|
| 53 |
|
| 54 |
|
|
|
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
|
|
|
|
|
|
| 1620 |
|
|
|
|
| 1621 |
|
| 1622 |
+
curators = ["Anna", "Ekaterina", "Ivan", "Maria", "Sergey", "Olga", "Alex", "Natalia", "Dmitry", "Elena"]
|
| 1623 |
+
|
| 1624 |
+
# Переменная для отслеживания текущего куратора
|
| 1625 |
+
current_curator_index = 0
|
| 1626 |
+
|
| 1627 |
+
# Шаблон сопоставления для кураторов
|
| 1628 |
+
mapping_template_cur = {
|
| 1629 |
+
'name': 'name',
|
| 1630 |
+
'phone': 'phone',
|
| 1631 |
+
'email': 'email',
|
| 1632 |
+
'curator': 'curator'
|
| 1633 |
+
}
|
| 1634 |
|
| 1635 |
|
| 1636 |
+
verifikation_start = "1" # Пример значения, которое должно быть установлено из конфигурации
|
| 1637 |
+
|
| 1638 |
+
|
| 1639 |
+
def verify_phone_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: {full_url_ver}")
|
| 1645 |
|
|
|
|
| 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)
|
|
|
|
| 1702 |
conn.commit()
|
| 1703 |
conn.close()
|
| 1704 |
|
| 1705 |
+
@app.route('/add_data_ver_cur', methods=['GET'])
|
| 1706 |
+
def add_data_ver_cur():
|
| 1707 |
global current_curator_index
|
| 1708 |
|
| 1709 |
# Получаем данные пользователя из параметров запроса
|
|
|
|
| 1712 |
# Назначаем текущего куратора
|
| 1713 |
user_data['curator'] = curators[current_curator_index]
|
| 1714 |
|
| 1715 |
+
# Верификация номера телефона
|
| 1716 |
+
phone_verification_response = verify_phone_number(user_data['phone'])
|
| 1717 |
+
if phone_verification_response is not None:
|
| 1718 |
+
user_data['ws_st'] = phone_verification_response
|
|
|
|
|
|
|
|
|
|
| 1719 |
|
| 1720 |
+
try:
|
| 1721 |
+
# Добавляем пользователя в базу данных
|
| 1722 |
+
add_user_to_db(DATABASE_NAME3, user_data)
|
| 1723 |
+
current_curator_index = (current_curator_index + 1) % len(curators)
|
| 1724 |
+
return jsonify({'status': 'success', 'message': f'User added with curator {user_data["curator"]}'})
|
| 1725 |
+
except Exception as e:
|
| 1726 |
+
logging.error(f"Error adding user: {e}")
|
| 1727 |
+
return jsonify({'status': 'error', 'message': str(e)}), 500
|
| 1728 |
|
| 1729 |
|
| 1730 |
|