psy2 / app.py
DmitrMakeev's picture
Update app.py
6e5ec17 verified
raw
history blame
2.29 kB
from flask import Flask, request, jsonify
import requests
import os
app = Flask(__name__)
api_key = os.getenv('api_key')
base_url = os.getenv('base_url') # URL сервера, например, 'https://api.notisend.ru/v1/email/'
token = os.getenv('token') # Ваш токен
@app.route('/save_db', methods=['GET'])
def save_db():
err = 0
if request.args.get('api') == api_key:
# Получаем данные из запроса
name = request.args.get('name')
email = request.args.get('email')
phone = request.args.get('phone')
grup_id = request.args.get('grup_id')
pr1 = request.args.get('pr1')
pr2 = request.args.get('pr2')
# Проверка значений переменных
if not all([name, email, phone, grup_id, pr1, pr2]):
return jsonify({"status": "error", "message": "Some required data is missing"}), 400
# Создаем данные для отправки
data = {
'list_id': grup_id,
'email': email,
'values': [
{'value': name, 'parameter_id': 'name'},
{'value': phone, 'parameter_id': 'phone'},
{'value': pr1, 'parameter_id': 'utm1'},
{'value': pr2, 'parameter_id': 'utm2'}
]
}
# Отправляем запрос
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
response = requests.post(f"{base_url}lists/{data['list_id']}/recipients", json=data, headers=headers)
# Проверяем статус ответа
if response.status_code == 200:
err = 1
message = "Operation completed successfully"
else:
message = f"Error: {response.status_code} - {response.text}"
err = 0
return jsonify({"status": "success" if err == 1 else "error", "message": message}), 200 if err == 1 else 400
else:
return jsonify({"status": "error", "message": "Invalid API key"}), 401
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))