Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, request,
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
import pytz
|
|
@@ -18,33 +18,8 @@ list_id_1 = 560768
|
|
| 18 |
list_id_2 = 560832
|
| 19 |
list_id_3 = 560833
|
| 20 |
|
| 21 |
-
# Инициализация Flask приложения
|
| 22 |
app = Flask(__name__, template_folder="./")
|
| 23 |
|
| 24 |
-
# Функция для отправки запроса на сервер
|
| 25 |
-
def send_request(list_id, email, phone, name, param_name, param_phone, pr5, pr4, pr3, pr2, pr1, pr5_id, pr4_id, pr3_id, pr2_id, pr1_id):
|
| 26 |
-
data = {
|
| 27 |
-
"email": email,
|
| 28 |
-
"unconfirmed": False,
|
| 29 |
-
"values": [
|
| 30 |
-
{"parameter_id": pr5_id, "kind": "string", "list_id": list_id, "title": "pr5", "value": pr5},
|
| 31 |
-
{"parameter_id": pr4_id, "kind": "string", "list_id": list_id, "title": "pr4", "value": pr4},
|
| 32 |
-
{"parameter_id": pr3_id, "kind": "string", "list_id": list_id, "title": "pr3", "value": pr3},
|
| 33 |
-
{"parameter_id": pr2_id, "kind": "string", "list_id": list_id, "title": "pr2", "value": pr2},
|
| 34 |
-
{"parameter_id": pr1_id, "kind": "string", "list_id": list_id, "title": "pr1", "value": pr1},
|
| 35 |
-
{"parameter_id": param_phone, "kind": "string", "list_id": list_id, "title": "phone", "value": phone},
|
| 36 |
-
{"parameter_id": param_name, "kind": "string", "list_id": list_id, "title": "name", "value": name}
|
| 37 |
-
]
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
headers = {
|
| 41 |
-
'Authorization': f'Bearer {token}',
|
| 42 |
-
'Content-Type': 'application/json'
|
| 43 |
-
}
|
| 44 |
-
response = requests.post(f"{base_url}/email/lists/{list_id}/recipients", json=data, headers=headers)
|
| 45 |
-
return response.json()
|
| 46 |
-
|
| 47 |
-
# Функция для сохранения данных в SQLite3
|
| 48 |
def init_db():
|
| 49 |
conn = sqlite3.connect('data.db')
|
| 50 |
cursor = conn.cursor()
|
|
@@ -66,10 +41,6 @@ def init_db():
|
|
| 66 |
|
| 67 |
init_db()
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
def save_to_database(name, email, phone, pr5, pr4, pr3, pr2, pr1):
|
| 74 |
try:
|
| 75 |
conn = sqlite3.connect('data.db')
|
|
@@ -83,16 +54,71 @@ def save_to_database(name, email, phone, pr5, pr4, pr3, pr2, pr1):
|
|
| 83 |
except Exception as e:
|
| 84 |
print(f"Error saving to database: {e}")
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
# Определение маршрута для обработки входящих POST-запросов
|
| 96 |
@app.route('/incoming', methods=['POST'])
|
| 97 |
def handle_incoming_request():
|
| 98 |
datas = request.json
|
|
@@ -106,6 +132,9 @@ def handle_incoming_request():
|
|
| 106 |
pr2 = datas.get('pr2')
|
| 107 |
pr1 = datas.get('pr1')
|
| 108 |
|
|
|
|
|
|
|
|
|
|
| 109 |
# Устанавливаем временную зону Москвы
|
| 110 |
moscow_timezone = pytz.timezone('Europe/Moscow')
|
| 111 |
|
|
@@ -121,37 +150,22 @@ def handle_incoming_request():
|
|
| 121 |
response_1 = send_request(560768, email, phone, name, 393120, 393119, pr5, pr4, pr3, pr2, pr1, 393248, 393247, 393246, 393245, 393244)
|
| 122 |
response_2 = send_request(560832, email, phone, name, 393196, 393195, pr5, pr4, pr3, pr2, pr1, 393267, 393266, 393265, 393264, 393263)
|
| 123 |
response_3 = send_request(560833, email, phone, name, 393198, 393197, pr5, pr4, pr3, pr2, pr1, 393272, 393271, 393270, 393269, 393268)
|
| 124 |
-
|
| 125 |
-
# Сохраняем данные в базу данных SQLite3
|
| 126 |
-
save_to_database(name, email, phone, pr5, pr4, pr3, pr2, pr1)
|
| 127 |
-
|
| 128 |
# Возвращаем список ответов
|
| 129 |
-
return {'responses': [response_2,
|
| 130 |
-
|
| 131 |
elif current_day == 1: # Вторник
|
| 132 |
if current_hour < 19:
|
| 133 |
# Утром во вторник
|
| 134 |
-
# Комментарий: Отправка в лист 1, лист 2, лист 3
|
| 135 |
response_1 = send_request(560768, email, phone, name, 393120, 393119, pr5, pr4, pr3, pr2, pr1, 393248, 393247, 393246, 393245, 393244)
|
| 136 |
response_2 = send_request(560832, email, phone, name, 393196, 393195, pr5, pr4, pr3, pr2, pr1, 393267, 393266, 393265, 393264, 393263)
|
| 137 |
response_3 = send_request(560833, email, phone, name, 393198, 393197, pr5, pr4, pr3, pr2, pr1, 393272, 393271, 393270, 393269, 393268)
|
| 138 |
-
|
| 139 |
-
# Сохраняем данные в базу данных SQLite3
|
| 140 |
-
save_to_database(name, email, phone, pr5, pr4, pr3, pr2, pr1)
|
| 141 |
-
|
| 142 |
# Возвращаем список ответов
|
| 143 |
-
return {'responses': [response_1,
|
| 144 |
else:
|
| 145 |
# Вечером во вторник
|
| 146 |
-
# Комментарий: Отправка только лист 2, лист 3
|
| 147 |
response_2 = send_request(560832, email, phone, name, 393196, 393195, pr5, pr4, pr3, pr2, pr1, 393267, 393266, 393265, 393264, 393263)
|
| 148 |
response_3 = send_request(560833, email, phone, name, 393198, 393197, pr5, pr4, pr3, pr2, pr1, 393272, 393271, 393270, 393269, 393268)
|
| 149 |
-
|
| 150 |
-
# Сохраняем данные в базу данных SQLite3
|
| 151 |
-
save_to_database(name, email, phone, pr5, pr4, pr3, pr2, pr1)
|
| 152 |
-
|
| 153 |
# Возвращаем список ответов
|
| 154 |
-
return {'responses': [response_2,
|
| 155 |
|
| 156 |
|
| 157 |
|
|
@@ -792,7 +806,6 @@ def show_contacts():
|
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
-
|
| 796 |
|
| 797 |
|
| 798 |
if __name__ == '__main__':
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template_string
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
import pytz
|
|
|
|
| 18 |
list_id_2 = 560832
|
| 19 |
list_id_3 = 560833
|
| 20 |
|
|
|
|
| 21 |
app = Flask(__name__, template_folder="./")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def init_db():
|
| 24 |
conn = sqlite3.connect('data.db')
|
| 25 |
cursor = conn.cursor()
|
|
|
|
| 41 |
|
| 42 |
init_db()
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def save_to_database(name, email, phone, pr5, pr4, pr3, pr2, pr1):
|
| 45 |
try:
|
| 46 |
conn = sqlite3.connect('data.db')
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
print(f"Error saving to database: {e}")
|
| 56 |
|
| 57 |
+
def send_request(list_id, email, phone, name, param_name, param_phone, pr5, pr4, pr3, pr2, pr1, pr5_id, pr4_id, pr3_id, pr2_id, pr1_id):
|
| 58 |
+
"""Функция для отправки запроса на сервер."""
|
| 59 |
+
data = {
|
| 60 |
+
"email": email,
|
| 61 |
+
"unconfirmed": False,
|
| 62 |
+
"values": [
|
| 63 |
+
{
|
| 64 |
+
"parameter_id": pr5_id,
|
| 65 |
+
"kind": "string",
|
| 66 |
+
"list_id": list_id,
|
| 67 |
+
"title": "pr5",
|
| 68 |
+
"value": pr5
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"parameter_id": pr4_id,
|
| 72 |
+
"kind": "string",
|
| 73 |
+
"list_id": list_id,
|
| 74 |
+
"title": "pr4",
|
| 75 |
+
"value": pr4
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
"parameter_id": pr3_id,
|
| 79 |
+
"kind": "string",
|
| 80 |
+
"list_id": list_id,
|
| 81 |
+
"title": "pr3",
|
| 82 |
+
"value": pr3
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
"parameter_id": pr2_id,
|
| 86 |
+
"kind": "string",
|
| 87 |
+
"list_id": list_id,
|
| 88 |
+
"title": "pr2",
|
| 89 |
+
"value": pr2
|
| 90 |
+
},
|
| 91 |
+
{
|
| 92 |
+
"parameter_id": pr1_id,
|
| 93 |
+
"kind": "string",
|
| 94 |
+
"list_id": list_id,
|
| 95 |
+
"title": "pr1",
|
| 96 |
+
"value": pr1
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"parameter_id": param_phone,
|
| 100 |
+
"kind": "string",
|
| 101 |
+
"list_id": list_id,
|
| 102 |
+
"title": "phone",
|
| 103 |
+
"value": phone
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"parameter_id": param_name,
|
| 107 |
+
"kind": "string",
|
| 108 |
+
"list_id": list_id,
|
| 109 |
+
"title": "name",
|
| 110 |
+
"value": name
|
| 111 |
+
}
|
| 112 |
+
]
|
| 113 |
+
}
|
| 114 |
|
| 115 |
+
headers = {
|
| 116 |
+
'Authorization': f'Bearer {token}',
|
| 117 |
+
'Content-Type': 'application/json'
|
| 118 |
+
}
|
| 119 |
+
response = requests.post(f"{base_url}/email/lists/{list_id}/recipients", json=data, headers=headers)
|
| 120 |
+
return response.json()
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
@app.route('/incoming', methods=['POST'])
|
| 123 |
def handle_incoming_request():
|
| 124 |
datas = request.json
|
|
|
|
| 132 |
pr2 = datas.get('pr2')
|
| 133 |
pr1 = datas.get('pr1')
|
| 134 |
|
| 135 |
+
# Сохраняем данные в базу данных SQLite3
|
| 136 |
+
save_to_database(name, email, phone, pr5, pr4, pr3, pr2, pr1)
|
| 137 |
+
|
| 138 |
# Устанавливаем временную зону Москвы
|
| 139 |
moscow_timezone = pytz.timezone('Europe/Moscow')
|
| 140 |
|
|
|
|
| 150 |
response_1 = send_request(560768, email, phone, name, 393120, 393119, pr5, pr4, pr3, pr2, pr1, 393248, 393247, 393246, 393245, 393244)
|
| 151 |
response_2 = send_request(560832, email, phone, name, 393196, 393195, pr5, pr4, pr3, pr2, pr1, 393267, 393266, 393265, 393264, 393263)
|
| 152 |
response_3 = send_request(560833, email, phone, name, 393198, 393197, pr5, pr4, pr3, pr2, pr1, 393272, 393271, 393270, 393269, 393268)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
# Возвращаем список ответов
|
| 154 |
+
return {'responses': [response_1,response_2,response_3]}
|
|
|
|
| 155 |
elif current_day == 1: # Вторник
|
| 156 |
if current_hour < 19:
|
| 157 |
# Утром во вторник
|
|
|
|
| 158 |
response_1 = send_request(560768, email, phone, name, 393120, 393119, pr5, pr4, pr3, pr2, pr1, 393248, 393247, 393246, 393245, 393244)
|
| 159 |
response_2 = send_request(560832, email, phone, name, 393196, 393195, pr5, pr4, pr3, pr2, pr1, 393267, 393266, 393265, 393264, 393263)
|
| 160 |
response_3 = send_request(560833, email, phone, name, 393198, 393197, pr5, pr4, pr3, pr2, pr1, 393272, 393271, 393270, 393269, 393268)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
# Возвращаем список ответов
|
| 162 |
+
return {'responses': [response_1,response_2,response_3]}
|
| 163 |
else:
|
| 164 |
# Вечером во вторник
|
|
|
|
| 165 |
response_2 = send_request(560832, email, phone, name, 393196, 393195, pr5, pr4, pr3, pr2, pr1, 393267, 393266, 393265, 393264, 393263)
|
| 166 |
response_3 = send_request(560833, email, phone, name, 393198, 393197, pr5, pr4, pr3, pr2, pr1, 393272, 393271, 393270, 393269, 393268)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
# Возвращаем список ответов
|
| 168 |
+
return {'responses': [response_2,response_3]}
|
| 169 |
|
| 170 |
|
| 171 |
|
|
|
|
| 806 |
|
| 807 |
|
| 808 |
|
|
|
|
| 809 |
|
| 810 |
|
| 811 |
if __name__ == '__main__':
|