DmitrMakeev
commited on
Commit
•
49408bd
1
Parent(s):
5a990fd
Update app.py
Browse files
app.py
CHANGED
@@ -1619,16 +1619,13 @@ def gc_forms():
|
|
1619 |
|
1620 |
# Список кураторов
|
1621 |
curators = ["Anna", "Ekaterina", "Ivan", "Maria", "Sergey", "Olga", "Alex", "Natalia", "Dmitry", "Elena"]
|
1622 |
-
|
1623 |
-
# Переменная для отслеживания текущего куратора
|
1624 |
current_curator_index = 0
|
1625 |
|
1626 |
-
# Шаблоны сопоставления
|
1627 |
mt_avp = {
|
1628 |
'name': 'name',
|
1629 |
'phone': 'phone',
|
1630 |
'email': 'email',
|
1631 |
-
'
|
1632 |
}
|
1633 |
|
1634 |
mt_bhelp = {
|
@@ -1652,7 +1649,6 @@ mt_tl = {
|
|
1652 |
'cur': 'curator'
|
1653 |
}
|
1654 |
|
1655 |
-
# Словарь для сопоставления ключей с шаблонами
|
1656 |
mapp_templates = {
|
1657 |
'avp': mt_avp,
|
1658 |
'bhelp': mt_bhelp,
|
@@ -1662,47 +1658,25 @@ mapp_templates = {
|
|
1662 |
|
1663 |
DATABASE_NAME3 = 'data_gc.db'
|
1664 |
|
1665 |
-
def verify_phone_number(phone_number):
|
1666 |
-
print(f"verifikation_start: {verifikation_start}")
|
1667 |
|
|
|
1668 |
if verifikation_start == "1":
|
1669 |
full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
|
1670 |
-
|
1671 |
-
|
1672 |
-
|
1673 |
-
payload = json.dumps({
|
1674 |
-
"phoneNumber": phone_number
|
1675 |
-
})
|
1676 |
-
headers = {
|
1677 |
-
'Content-Type': 'application/json'
|
1678 |
-
}
|
1679 |
|
1680 |
-
# Отправляем POST-запрос
|
1681 |
response = requests.post(full_url_ver, headers=headers, data=payload)
|
1682 |
-
|
1683 |
-
# Печатаем статус ответа
|
1684 |
-
print(f"Response Status Code: {response.status_code}")
|
1685 |
-
|
1686 |
-
# Проверяем статус код ответа
|
1687 |
if response.status_code == 200:
|
1688 |
-
# Печатаем текст ответа от сервера
|
1689 |
response_body = response.json()
|
1690 |
-
|
1691 |
-
|
1692 |
-
# Извлекаем значение из JSON
|
1693 |
-
exists_whatsapp = response_body.get('existsWhatsapp', 'false')
|
1694 |
-
print(f"existsWhatsapp: {exists_whatsapp}")
|
1695 |
-
|
1696 |
-
# Возвращаем значение
|
1697 |
-
return exists_whatsapp
|
1698 |
else:
|
1699 |
-
print("Error: Unable to fetch data")
|
1700 |
return "Error"
|
1701 |
else:
|
1702 |
-
|
1703 |
-
return "false" # Возвращаем значение, что WhatsApp не существует
|
1704 |
|
1705 |
-
def add_or_update_contact(contact_data
|
1706 |
conn = sqlite3.connect(DATABASE_NAME3)
|
1707 |
cursor = conn.cursor()
|
1708 |
|
@@ -1711,18 +1685,11 @@ def add_or_update_contact(contact_data, mapping_template_cur):
|
|
1711 |
logging.error(f"Missing email in contact data: {contact_data}")
|
1712 |
return
|
1713 |
|
1714 |
-
# Добавление текущей даты и времени
|
1715 |
utc_now = datetime.utcnow()
|
1716 |
msk_tz = pytz.timezone('Europe/Moscow')
|
1717 |
msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
|
1718 |
contact_data['data_t'] = msk_now.strftime('%Y-%m-%d %H:%M:%S')
|
1719 |
|
1720 |
-
# Обработка параметра 'cur' в зависимости от шаблона
|
1721 |
-
if 'cur' in contact_data:
|
1722 |
-
cur_key = next((value for key, value in mapping_template_cur.items() if value == 'cur'), None)
|
1723 |
-
if cur_key:
|
1724 |
-
contact_data[cur_key] = contact_data.pop('cur')
|
1725 |
-
|
1726 |
cursor.execute("SELECT id FROM contacts WHERE email = ?", (email,))
|
1727 |
contact = cursor.fetchone()
|
1728 |
|
@@ -1731,7 +1698,7 @@ def add_or_update_contact(contact_data, mapping_template_cur):
|
|
1731 |
'b_city', 'b_fin', 'b_ban', 'b_ign', 'b_baners', 'b_butt', 'b_mess', 'shop_st', 'curator',
|
1732 |
'pr1', 'pr2', 'pr3', 'pr4', 'pr5', 'ad_url', 'key_pr', 'n_con', 'canal', 'data_t'
|
1733 |
]
|
1734 |
-
|
1735 |
placeholders = ", ".join([f"{field} = ?" for field in fields])
|
1736 |
|
1737 |
if contact:
|
@@ -1748,28 +1715,20 @@ def add_or_update_contact(contact_data, mapping_template_cur):
|
|
1748 |
def add_data_ver_cur():
|
1749 |
global current_curator_index
|
1750 |
|
1751 |
-
# Получаем ключ для выбора шаблона из параметров запроса
|
1752 |
template_key = request.args.get('template_key', 'avp')
|
1753 |
-
|
1754 |
-
# Выбираем соответствующий шаблон
|
1755 |
mapping_template_cur = mapp_templates.get(template_key, mt_avp)
|
1756 |
|
1757 |
-
|
1758 |
-
user_data =
|
1759 |
-
|
1760 |
-
# Добавляем параметр 'cur' в user_data
|
1761 |
-
user_data['cur'] = request.args.get('cur', "")
|
1762 |
|
1763 |
-
|
1764 |
-
phone_verification_response = verify_phone_number(user_data['phone'])
|
1765 |
if phone_verification_response is not None:
|
1766 |
user_data['ws_st'] = phone_verification_response
|
1767 |
|
1768 |
try:
|
1769 |
-
|
1770 |
-
add_or_update_contact(user_data, mapping_template_cur)
|
1771 |
current_curator_index = (current_curator_index + 1) % len(curators)
|
1772 |
-
return jsonify({'status': 'success', 'message': f'User added with curator {user_data["
|
1773 |
except Exception as e:
|
1774 |
logging.error(f"Error adding user: {e}")
|
1775 |
return jsonify({'status': 'error', 'message': str(e)}), 500
|
|
|
1619 |
|
1620 |
# Список кураторов
|
1621 |
curators = ["Anna", "Ekaterina", "Ivan", "Maria", "Sergey", "Olga", "Alex", "Natalia", "Dmitry", "Elena"]
|
|
|
|
|
1622 |
current_curator_index = 0
|
1623 |
|
|
|
1624 |
mt_avp = {
|
1625 |
'name': 'name',
|
1626 |
'phone': 'phone',
|
1627 |
'email': 'email',
|
1628 |
+
'cur': 'pr5'
|
1629 |
}
|
1630 |
|
1631 |
mt_bhelp = {
|
|
|
1649 |
'cur': 'curator'
|
1650 |
}
|
1651 |
|
|
|
1652 |
mapp_templates = {
|
1653 |
'avp': mt_avp,
|
1654 |
'bhelp': mt_bhelp,
|
|
|
1658 |
|
1659 |
DATABASE_NAME3 = 'data_gc.db'
|
1660 |
|
|
|
|
|
1661 |
|
1662 |
+
def verify_phone_number(phone_number):
|
1663 |
if verifikation_start == "1":
|
1664 |
full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
|
1665 |
+
|
1666 |
+
payload = json.dumps({"phoneNumber": phone_number})
|
1667 |
+
headers = {'Content-Type': 'application/json'}
|
|
|
|
|
|
|
|
|
|
|
|
|
1668 |
|
|
|
1669 |
response = requests.post(full_url_ver, headers=headers, data=payload)
|
1670 |
+
|
|
|
|
|
|
|
|
|
1671 |
if response.status_code == 200:
|
|
|
1672 |
response_body = response.json()
|
1673 |
+
return response_body.get('existsWhatsapp', 'false')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1674 |
else:
|
|
|
1675 |
return "Error"
|
1676 |
else:
|
1677 |
+
return "false"
|
|
|
1678 |
|
1679 |
+
def add_or_update_contact(contact_data):
|
1680 |
conn = sqlite3.connect(DATABASE_NAME3)
|
1681 |
cursor = conn.cursor()
|
1682 |
|
|
|
1685 |
logging.error(f"Missing email in contact data: {contact_data}")
|
1686 |
return
|
1687 |
|
|
|
1688 |
utc_now = datetime.utcnow()
|
1689 |
msk_tz = pytz.timezone('Europe/Moscow')
|
1690 |
msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
|
1691 |
contact_data['data_t'] = msk_now.strftime('%Y-%m-%d %H:%M:%S')
|
1692 |
|
|
|
|
|
|
|
|
|
|
|
|
|
1693 |
cursor.execute("SELECT id FROM contacts WHERE email = ?", (email,))
|
1694 |
contact = cursor.fetchone()
|
1695 |
|
|
|
1698 |
'b_city', 'b_fin', 'b_ban', 'b_ign', 'b_baners', 'b_butt', 'b_mess', 'shop_st', 'curator',
|
1699 |
'pr1', 'pr2', 'pr3', 'pr4', 'pr5', 'ad_url', 'key_pr', 'n_con', 'canal', 'data_t'
|
1700 |
]
|
1701 |
+
|
1702 |
placeholders = ", ".join([f"{field} = ?" for field in fields])
|
1703 |
|
1704 |
if contact:
|
|
|
1715 |
def add_data_ver_cur():
|
1716 |
global current_curator_index
|
1717 |
|
|
|
1718 |
template_key = request.args.get('template_key', 'avp')
|
|
|
|
|
1719 |
mapping_template_cur = mapp_templates.get(template_key, mt_avp)
|
1720 |
|
1721 |
+
user_data = {mapping_template_cur[key]: request.args.get(key, "") for key in mapping_template_cur}
|
1722 |
+
user_data['curator'] = curators[current_curator_index]
|
|
|
|
|
|
|
1723 |
|
1724 |
+
phone_verification_response = verify_phone_number(user_data.get('phone', ''))
|
|
|
1725 |
if phone_verification_response is not None:
|
1726 |
user_data['ws_st'] = phone_verification_response
|
1727 |
|
1728 |
try:
|
1729 |
+
add_or_update_contact(user_data)
|
|
|
1730 |
current_curator_index = (current_curator_index + 1) % len(curators)
|
1731 |
+
return jsonify({'status': 'success', 'message': f'User added with curator {user_data["curator"]}'})
|
1732 |
except Exception as e:
|
1733 |
logging.error(f"Error adding user: {e}")
|
1734 |
return jsonify({'status': 'error', 'message': str(e)}), 500
|