calls.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from datetime import datetime, timezone
  2. from .phone import normalize
  3. def normalize_call(participant: dict, queue_dn: str) -> dict:
  4. raw = participant.get("party_caller_id")
  5. normalized = normalize(raw, "DE") if raw else {
  6. "e164": None,
  7. "valid": False,
  8. }
  9. return {
  10. "callId": participant.get("callid"),
  11. "legId": participant.get("legid"),
  12. "queue": queue_dn,
  13. "direction": (
  14. "inbound"
  15. if participant.get("party_dn_type") == "Wexternalline"
  16. else "internal"
  17. ),
  18. "status": (participant.get("status") or "unknown").lower(),
  19. "caller": {
  20. "raw": raw,
  21. "e164": normalized.get("e164"),
  22. },
  23. "agent": None,
  24. "observedAt": datetime.now(timezone.utc).isoformat(),
  25. "source": {
  26. "partyDn": participant.get("party_dn"),
  27. "partyDnType": participant.get("party_dn_type"),
  28. "deviceId": participant.get("device_id"),
  29. "directControl": participant.get("direct_control"),
  30. },
  31. }