Spaces:
Sleeping
Sleeping
from flask import Flask, request, render_template, jsonify | |
import requests | |
import datetime | |
import os | |
base_url = os.getenv('base_url') # URL сервера | |
token = os.getenv('token') # Ваш токен | |
api_key = os.getenv('api_key') # Ваш | |
app = Flask(__name__, template_folder="./") | |
def grup(): | |
return render_template('grups.html') | |
def send_request(): | |
api_token = request.form.get('api_token') | |
group_id = request.form.get('group_id') | |
url = f"https://api.notisend.ru/v1/email/lists/{group_id}/parameters" | |
headers = { | |
'Content-Type': 'application/json', | |
'Authorization': f'Bearer {api_token}' | |
} | |
response = requests.get(url, headers=headers) | |
data = response.json() | |
return jsonify(data) | |
def save_db(): | |
show_params = False | |
list_id = request.args.get('list_id') | |
name = request.args.get('name') | |
name_id = request.args.get('name_id') | |
email = request.args.get('email') | |
phone = request.args.get('phone') | |
phone_id = request.args.get('phone_id') | |
pr1 = request.args.get('pr1') | |
pr1_id = request.args.get('pr1_id') | |
pr2 = request.args.get('pr2') | |
pr2_id = request.args.get('pr2_id') | |
pr3 = request.args.get('pr3') | |
pr3_id = request.args.get('pr3_id') | |
pr4 = request.args.get('pr4') | |
pr4_id = request.args.get('pr4_id') | |
pr5 = request.args.get('pr5') | |
pr5_id = request.args.get('pr5_id') | |
data = { | |
"email": email, | |
"unconfirmed": False, | |
"values": [ | |
{ | |
"parameter_id": pr5_id, | |
"kind": "string", | |
"list_id": list_id, | |
"title": "pr5", | |
"value": pr5 | |
}, | |
{ | |
"parameter_id": pr4_id, | |
"kind": "string", | |
"list_id": list_id, | |
"title": "pr4", | |
"value": pr4 | |
}, | |
{ | |
"parameter_id": pr3_id, | |
"kind": "string", | |
"list_id": list_id, | |
"title": "pr3", | |
"value": pr3 | |
}, | |
{ | |
"parameter_id": pr2_id, | |
"kind": "string", | |
"list_id": list_id, | |
"title": "pr2", | |
"value": pr2 | |
}, | |
{ | |
"parameter_id": pr1_id, | |
"kind": "string", | |
"list_id": list_id, | |
"title": "pr1", | |
"value": pr1 | |
}, | |
{ | |
"parameter_id": phone_id, | |
"kind": "string", | |
"list_id": list_id, | |
"title": "phone", | |
"value": phone | |
}, | |
{ | |
"parameter_id": name_id, | |
"kind": "string", | |
"list_id": list_id, | |
"title": "name", | |
"value": name | |
} | |
] | |
} | |
headers = { | |
'Authorization': f'Bearer {token}', | |
'Content-Type': 'application/json' | |
} | |
response = requests.post(f"{base_url}/email/lists/{list_id}/recipients", json=data, headers=headers) | |
if show_params: | |
system_vars = { | |
'base_url': base_url, | |
'token': token | |
} | |
return jsonify({'system_variables': system_vars, 'request_parameters': request.args}) | |
else: | |
return response.text | |
# Здесь должно быть определение переменных base_url и token для Notisend API | |
def save_pl(): | |
list_id1 = '560768' | |
list_id2 = '560832' | |
list_id3 = '560833' | |
data = request.json | |
name = data.get('name') | |
email = data.get('email') | |
phone = data.get('phone') | |
# Определяем текущий день недели (0 - понедельник, 1 - вторник, ..., 6 - воскресенье) | |
current_day = datetime.datetime.today().weekday() | |
urls = [] | |
# Понедельник, пятница, суббота, воскресенье | |
if current_day in [0, 4, 5, 6]: | |
urls = [ | |
(f"{base_url}/email/lists/{list_id1}/recipients_mon_fri_sat_sun", { | |
"email": email, | |
"unconfirmed": True, | |
"values": [ | |
{ | |
"parameter_id": 393120, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "phone", | |
"value": phone | |
}, | |
{ | |
"parameter_id": 393119, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "name", | |
"value": name | |
} | |
] | |
}), | |
(f"{base_url}/email/lists/{list_id2}/recipients_mon_fri_sat_sun", { | |
"email": email, | |
"unconfirmed": True, | |
"values": [ | |
{ | |
"parameter_id": 393120, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "phone", | |
"value": phone | |
}, | |
{ | |
"parameter_id": 393119, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "name", | |
"value": name | |
} | |
] | |
}), | |
(f"{base_url}/email/lists/{list_id3}/recipients_mon_fri_sat_sun", { | |
"email": email, | |
"unconfirmed": True, | |
"values": [ | |
{ | |
"parameter_id": 393120, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "phone", | |
"value": phone | |
}, | |
{ | |
"parameter_id": 393119, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "name", | |
"value": name | |
} | |
] | |
}) | |
] | |
# Вторник | |
elif current_day == 1: | |
current_time = datetime.datetime.now().time() | |
if current_time < datetime.time(19, 0): # Утро до 19:00 | |
urls = [ | |
(f"{base_url}/email/lists/{list_id1}/recipients_tue_morning", { | |
"email": email, | |
"unconfirmed": True, | |
"values": [ | |
{ | |
"parameter_id": 393120, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "phone", | |
"value": phone | |
}, | |
{ | |
"parameter_id": 393119, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "name", | |
"value": name | |
} | |
] | |
}) | |
] | |
else: # Вечер после 19:00 | |
urls = [ | |
(f"{base_url}/email/lists/{list_id1}/recipients_tue_evening", { | |
"email": email, | |
"unconfirmed": False, | |
"values": [ | |
{ | |
"parameter_id": 393120, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "phone", | |
"value": phone | |
}, | |
{ | |
"parameter_id": 393119, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "name", | |
"value": name | |
} | |
] | |
}) | |
] | |
# Среда | |
elif current_day == 2: | |
current_time = datetime.datetime.now().time() | |
if current_time < datetime.time(19, 0): # Утро до 19:00 | |
urls = [ | |
(f"{base_url}/email/lists/{list_id1}/recipients_wed_morning", { | |
"email": email, | |
"unconfirmed": True, | |
"values": [ | |
{ | |
"parameter_id": 393120, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "phone", | |
"value": phone | |
}, | |
{ | |
"parameter_id": 393119, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "name", | |
"value": name | |
} | |
] | |
}) | |
] | |
else: # Вечер после 19:00 | |
urls = [ | |
(f"{base_url}/email/lists/{list_id1}/recipients_wed_evening", { | |
"email": email, | |
"unconfirmed": False, | |
"values": [ | |
{ | |
"parameter_id": 393120, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "phone", | |
"value": phone | |
}, | |
{ | |
"parameter_id": 393119, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "name", | |
"value": name | |
} | |
] | |
}) | |
] | |
# Четверг | |
elif current_day == 3: | |
current_time = datetime.datetime.now().time() | |
if current_time < datetime.time(19, 0): # Утро до 19:00 | |
urls = [ | |
(f"{base_url}/email/lists/{list_id1}/recipients_thu_morning", { | |
"email": email, | |
"unconfirmed": True, | |
"values": [ | |
{ | |
"parameter_id": 393120, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "phone", | |
"value": phone | |
}, | |
{ | |
"parameter_id": 393119, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "name", | |
"value": name | |
} | |
] | |
}) | |
] | |
else: # Вечер после 19:00 | |
urls = [ | |
(f"{base_url}/email/lists/{list_id1}/recipients_thu_evening", { | |
"email": email, | |
"unconfirmed": False, | |
"values": [ | |
{ | |
"parameter_id": 393120, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "phone", | |
"value": phone | |
}, | |
{ | |
"parameter_id": 393119, | |
"kind": "string", | |
"list_id": list_id1, | |
"title": "name", | |
"value": name | |
} | |
] | |
}) | |
] | |
# Отправляем запросы | |
responses = [] | |
for url, json_data in urls: | |
headers = { | |
'Authorization': f'Bearer {token}', | |
'Content-Type': 'application/json' | |
} | |
response = requests.post(url, json=json_data, headers=headers) | |
responses.append({ | |
'url': url, | |
'status_code': response.status_code, | |
'response_text': response.text | |
}) | |
return jsonify({'responses': responses}) | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860))) |