psy2 / app.py
DmitrMakeev's picture
Update app.py
95f07ef verified
raw
history blame
2.27 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():
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([email, name, phone, grup_id, pr1, pr2]):
return jsonify({"status": "error", "message": "Some required data is missing"}), 400
# Создаем данные для отправки
data = {
"email": email,
"unconfirmed": True, # Создаем неподтвержденного получателя
"values": [
{"parameter_id": "name", "value": name},
{"parameter_id": "phone", "value": phone},
{"parameter_id": "utm1", "value": pr1},
{"parameter_id": "utm2", "value": pr2}
],
"tags": ["buyer", "regular customer"] # Пример тегов
}
# Отправляем запрос
headers = {
'Authorization': f'Bearer {token}',
'Content-Type': 'application/json'
}
response = requests.post(f"{base_url}lists/{grup_id}/recipients", json=data, headers=headers)
# Проверяем статус ответа
if response.status_code == 200:
return jsonify({"status": "success", "message": "Recipient created successfully"}), 200
else:
return jsonify({"status": "error", "message": f"Error: {response.status_code} - {response.text}"}), 400
else:
return jsonify({"status": "error", "message": "Invalid API key"}), 401
if __name__ == '__main__':
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))