заливка базы
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
import logging
|
||||
import config
|
||||
import json
|
||||
import jwt
|
||||
import time
|
||||
from aiohttp import ClientSession
|
||||
import aiohttp
|
||||
|
||||
|
||||
def loggingDict(title: str, data: dict) -> None:
|
||||
logging.info(
|
||||
f"{title}: %s",
|
||||
json.dumps(data, indent=4, ensure_ascii=False).encode("utf-8").decode("utf-8"),
|
||||
)
|
||||
|
||||
|
||||
def medods_token():
|
||||
if config.MEDODS_AUTH_VERSION == 1:
|
||||
return config.MEDODS_V1_TOKEN
|
||||
elif config.MEDODS_AUTH_VERSION == 2:
|
||||
iat = int(time.time()) - 10
|
||||
exp = iat + 60
|
||||
payload = {"iss": config.MEDODS_V2_IDENTITY, "iat": iat, "exp": exp}
|
||||
print(payload)
|
||||
token = jwt.encode(payload, config.MEDODS_V2_SECRETKEY, algorithm="HS512")
|
||||
return token
|
||||
|
||||
|
||||
async def send_post_request(body: dict):
|
||||
|
||||
async def post_request(
|
||||
url: str,
|
||||
headers: dict = None,
|
||||
json: dict = None,
|
||||
**kwargs,
|
||||
):
|
||||
|
||||
try:
|
||||
async with ClientSession() as session:
|
||||
async with session.post(
|
||||
url, json=json, headers=headers, **kwargs
|
||||
) as response:
|
||||
response.raise_for_status()
|
||||
return await response.json()
|
||||
except aiohttp.ClientError as e:
|
||||
if config.DEBUG:
|
||||
logging.error(f"Request failed: {str(e)}")
|
||||
return None
|
||||
|
||||
data = {"call": body}
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": f"Bearer {medods_token()}",
|
||||
}
|
||||
if config.DEBUG:
|
||||
loggingDict("Sending data to Medods", body)
|
||||
r = await post_request(url=config.MEDODS_SERVER, headers=headers, json=data)
|
||||
try:
|
||||
res = {
|
||||
"id": r["call"]["id"],
|
||||
"status": r["call"]["status"],
|
||||
"call_session_id": r["call"]["call_session_id"],
|
||||
}
|
||||
except:
|
||||
res = None
|
||||
loggingDict("Recieved data from Medods:", res)
|
||||
else:
|
||||
await post_request(url=config.MEDODS_SERVER, headers=headers, json=data)
|
||||
|
||||
|
||||
async def incoming_call(call_session_id, contact_phone_number, called_phone_number):
|
||||
body = {
|
||||
"status": "incoming_call",
|
||||
"call_session_id": call_session_id,
|
||||
"contact_phone_number": contact_phone_number,
|
||||
"called_phone_number": called_phone_number,
|
||||
}
|
||||
await send_post_request(body)
|
||||
|
||||
|
||||
async def call_started(call_session_id, responsibles):
|
||||
body = {
|
||||
"status": "call_started",
|
||||
"call_session_id": call_session_id,
|
||||
"responsibles": responsibles,
|
||||
}
|
||||
await send_post_request(body)
|
||||
|
||||
|
||||
async def call_finished(call_session_id, duration):
|
||||
body = {
|
||||
"status": "call_finished",
|
||||
"call_session_id": call_session_id,
|
||||
"duration": duration,
|
||||
}
|
||||
await send_post_request(body)
|
||||
|
||||
|
||||
async def call_lost(call_session_id, responsibles):
|
||||
body = {
|
||||
"status": "call_lost",
|
||||
"call_session_id": call_session_id,
|
||||
"responsibles": responsibles,
|
||||
}
|
||||
await send_post_request(body)
|
||||
|
||||
|
||||
async def call_record_file(call_session_id, uniqueid):
|
||||
body = {
|
||||
"status": "call_record_file",
|
||||
"call_session_id": call_session_id,
|
||||
"file_link": f"{config.RECORDS_SERVER}{uniqueid}",
|
||||
}
|
||||
await send_post_request(body)
|
||||
Reference in New Issue
Block a user