Spaces:
Sleeping
Sleeping
| from flask import request, jsonify | |
| import os | |
| import asyncio | |
| import aiohttp | |
| api_key = os.getenv('api_key') | |
| base_url = os.getenv('base_url') | |
| token = os.getenv('token') | |
| list_id = os.getenv('list_id') | |
| parameter_id = os.getenv('parameter_id') | |
| async def async_add_user(data): | |
| headers = { | |
| 'Authorization': f'Bearer {token}', | |
| 'Content-Type': 'application/json' | |
| } | |
| async with aiohttp.ClientSession() as session: | |
| async with session.post(f"{base_url}lists/{list_id}/recipients", json=data, headers=headers) as response: | |
| return response.status | |
| def add_user(): | |
| err = 0 | |
| if api_key == request.args.get('api'): | |
| # Получаем данные из глобальных переменных | |
| names = globs.name | |
| emails = globs.email | |
| phones = globs.phone | |
| pr1s = globs.pr1 | |
| pr2s = globs.pr2 | |
| pr3s = globs.pr3 | |
| grups_id = globs.grup_id | |
| # Создаем данные для отправки | |
| data = { | |
| 'email': emails, | |
| 'values': [ | |
| { | |
| 'value': names, | |
| 'parameter_id': parameter_id | |
| }, | |
| { | |
| 'value': phones, | |
| 'parameter_id': parameter_id + 1 # Предполагаем, что есть уникальный ID для каждого параметра | |
| }, | |
| { | |
| 'value': pr1s, | |
| 'parameter_id': parameter_id + 2 | |
| }, | |
| { | |
| 'value': pr2s, | |
| 'parameter_id': parameter_id + 3 | |
| }, | |
| { | |
| 'value': pr3s, | |
| 'parameter_id': parameter_id + 4 | |
| } | |
| ] | |
| } | |
| # Запускаем асинхронный запрос в отдельном потоке | |
| loop = asyncio.new_event_loop() | |
| asyncio.set_event_loop(loop) | |
| result = loop.run_until_complete(async_add_user(data)) | |
| loop.close() | |
| err = 1 if result == 200 else 0 | |
| response_data = { | |
| "status": "success" if err == 1 else "error", | |
| "err": err | |
| } | |
| return jsonify(response_data) |