2019-05-21 07:57:06 +05:30
|
|
|
import frappe
|
2019-05-22 06:37:43 +05:30
|
|
|
import requests
|
|
|
|
|
|
|
|
# api/method/erpnext.erpnext_integrations.exotel_integration.handle_incoming_call
|
2019-06-07 10:22:50 +05:30
|
|
|
# api/method/erpnext.erpnext_integrations.exotel_integration.handle_end_call
|
|
|
|
# api/method/erpnext.erpnext_integrations.exotel_integration.handle_missed_call
|
2019-05-21 07:57:06 +05:30
|
|
|
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2019-05-21 07:57:06 +05:30
|
|
|
@frappe.whitelist(allow_guest=True)
|
2019-07-01 14:28:59 +05:30
|
|
|
def handle_incoming_call(**kwargs):
|
2019-09-17 15:53:23 +05:30
|
|
|
try:
|
|
|
|
exotel_settings = get_exotel_settings()
|
2022-03-28 18:52:46 +05:30
|
|
|
if not exotel_settings.enabled:
|
|
|
|
return
|
2019-09-17 15:53:23 +05:30
|
|
|
|
|
|
|
call_payload = kwargs
|
2022-03-28 18:52:46 +05:30
|
|
|
status = call_payload.get("Status")
|
|
|
|
if status == "free":
|
2019-09-17 15:53:23 +05:30
|
|
|
return
|
|
|
|
|
|
|
|
call_log = get_call_log(call_payload)
|
|
|
|
if not call_log:
|
|
|
|
create_call_log(call_payload)
|
|
|
|
else:
|
|
|
|
update_call_log(call_payload, call_log=call_log)
|
|
|
|
except Exception as e:
|
|
|
|
frappe.db.rollback()
|
2022-05-02 15:04:26 +05:30
|
|
|
exotel_settings.log_error("Error in Exotel incoming call")
|
2019-05-21 07:57:06 +05:30
|
|
|
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2019-05-27 15:30:41 +05:30
|
|
|
@frappe.whitelist(allow_guest=True)
|
2019-07-01 14:28:59 +05:30
|
|
|
def handle_end_call(**kwargs):
|
2022-03-28 18:52:46 +05:30
|
|
|
update_call_log(kwargs, "Completed")
|
|
|
|
|
2019-06-03 12:27:02 +05:30
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
2019-07-01 14:28:59 +05:30
|
|
|
def handle_missed_call(**kwargs):
|
2022-02-23 12:59:38 +05:30
|
|
|
status = ""
|
2022-02-25 16:52:25 +05:30
|
|
|
call_type = kwargs.get("CallType")
|
|
|
|
dial_call_status = kwargs.get("DialCallStatus")
|
2022-02-23 12:59:38 +05:30
|
|
|
|
2022-02-25 16:52:25 +05:30
|
|
|
if call_type == "incomplete" and dial_call_status == "no-answer":
|
2022-03-28 19:55:39 +05:30
|
|
|
status = "No Answer"
|
2022-02-25 16:52:25 +05:30
|
|
|
elif call_type == "client-hangup" and dial_call_status == "canceled":
|
2022-03-28 19:55:39 +05:30
|
|
|
status = "Canceled"
|
2022-02-25 16:52:25 +05:30
|
|
|
elif call_type == "incomplete" and dial_call_status == "failed":
|
2022-03-28 19:55:39 +05:30
|
|
|
status = "Failed"
|
2022-02-23 12:59:38 +05:30
|
|
|
|
|
|
|
update_call_log(kwargs, status)
|
2019-06-03 12:27:02 +05:30
|
|
|
|
2022-03-28 18:52:46 +05:30
|
|
|
|
|
|
|
def update_call_log(call_payload, status="Ringing", call_log=None):
|
2019-07-16 11:07:25 +05:30
|
|
|
call_log = call_log or get_call_log(call_payload)
|
2022-02-23 12:59:38 +05:30
|
|
|
|
|
|
|
# for a new sid, call_log and get_call_log will be empty so create a new log
|
|
|
|
if not call_log:
|
|
|
|
call_log = create_call_log(call_payload)
|
2019-05-27 15:30:41 +05:30
|
|
|
if call_log:
|
2019-06-07 10:22:50 +05:30
|
|
|
call_log.status = status
|
2022-03-28 18:52:46 +05:30
|
|
|
call_log.to = call_payload.get("DialWhomNumber")
|
|
|
|
call_log.duration = call_payload.get("DialCallDuration") or 0
|
|
|
|
call_log.recording_url = call_payload.get("RecordingUrl")
|
2019-05-27 15:30:41 +05:30
|
|
|
call_log.save(ignore_permissions=True)
|
|
|
|
frappe.db.commit()
|
2019-06-07 10:22:50 +05:30
|
|
|
return call_log
|
2019-05-27 15:30:41 +05:30
|
|
|
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2019-07-01 14:28:59 +05:30
|
|
|
def get_call_log(call_payload):
|
2022-04-04 07:28:41 +05:30
|
|
|
call_log_id = call_payload.get("CallSid")
|
|
|
|
if frappe.db.exists("Call Log", call_log_id):
|
|
|
|
return frappe.get_doc("Call Log", call_log_id)
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2019-05-22 06:37:43 +05:30
|
|
|
|
2019-07-01 14:28:59 +05:30
|
|
|
def create_call_log(call_payload):
|
2022-03-28 18:52:46 +05:30
|
|
|
call_log = frappe.new_doc("Call Log")
|
|
|
|
call_log.id = call_payload.get("CallSid")
|
|
|
|
call_log.to = call_payload.get("DialWhomNumber")
|
|
|
|
call_log.medium = call_payload.get("To")
|
|
|
|
call_log.status = "Ringing"
|
|
|
|
setattr(call_log, "from", call_payload.get("CallFrom"))
|
2019-07-01 14:28:59 +05:30
|
|
|
call_log.save(ignore_permissions=True)
|
|
|
|
frappe.db.commit()
|
|
|
|
return call_log
|
2019-06-17 10:16:38 +05:30
|
|
|
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2019-05-22 15:48:57 +05:30
|
|
|
@frappe.whitelist()
|
2019-05-22 06:37:43 +05:30
|
|
|
def get_call_status(call_id):
|
2022-03-28 18:52:46 +05:30
|
|
|
endpoint = get_exotel_endpoint("Calls/{call_id}.json".format(call_id=call_id))
|
2019-06-13 17:13:54 +05:30
|
|
|
response = requests.get(endpoint)
|
2022-03-28 18:52:46 +05:30
|
|
|
status = response.json().get("Call", {}).get("Status")
|
2019-05-22 15:48:57 +05:30
|
|
|
return status
|
2019-05-22 06:37:43 +05:30
|
|
|
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2019-05-22 15:48:57 +05:30
|
|
|
@frappe.whitelist()
|
2019-05-22 06:37:43 +05:30
|
|
|
def make_a_call(from_number, to_number, caller_id):
|
2022-03-28 18:52:46 +05:30
|
|
|
endpoint = get_exotel_endpoint("Calls/connect.json?details=true")
|
|
|
|
response = requests.post(
|
|
|
|
endpoint, data={"From": from_number, "To": to_number, "CallerId": caller_id}
|
|
|
|
)
|
2019-05-22 06:37:43 +05:30
|
|
|
|
|
|
|
return response.json()
|
|
|
|
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2019-05-22 06:37:43 +05:30
|
|
|
def get_exotel_settings():
|
2022-03-28 18:52:46 +05:30
|
|
|
return frappe.get_single("Exotel Settings")
|
|
|
|
|
2019-06-03 12:27:02 +05:30
|
|
|
|
|
|
|
def whitelist_numbers(numbers, caller_id):
|
2022-03-28 18:52:46 +05:30
|
|
|
endpoint = get_exotel_endpoint("CustomerWhitelist")
|
|
|
|
response = requests.post(
|
|
|
|
endpoint,
|
|
|
|
data={
|
|
|
|
"VirtualNumber": caller_id,
|
|
|
|
"Number": numbers,
|
|
|
|
},
|
|
|
|
)
|
2019-06-03 12:27:02 +05:30
|
|
|
|
2019-06-13 17:13:54 +05:30
|
|
|
return response
|
|
|
|
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2019-06-13 17:13:54 +05:30
|
|
|
def get_all_exophones():
|
2022-03-28 18:52:46 +05:30
|
|
|
endpoint = get_exotel_endpoint("IncomingPhoneNumbers")
|
2019-06-13 17:13:54 +05:30
|
|
|
response = requests.post(endpoint)
|
|
|
|
return response
|
|
|
|
|
2022-03-28 18:52:46 +05:30
|
|
|
|
2019-06-13 17:13:54 +05:30
|
|
|
def get_exotel_endpoint(action):
|
|
|
|
settings = get_exotel_settings()
|
2022-03-28 18:52:46 +05:30
|
|
|
return "https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}".format(
|
|
|
|
api_key=settings.api_key, api_token=settings.api_token, sid=settings.account_sid, action=action
|
2019-09-17 15:53:23 +05:30
|
|
|
)
|