blob: 522de9ead83807f2cbb0d0230ecf0ac532905ff1 [file] [log] [blame]
Suraj Shetty863b93c2019-05-21 07:57:06 +05301import frappe
Suraj Shetty1eeb89f2019-05-22 06:37:43 +05302import requests
Suraj Shettyaf2eac42019-09-17 15:53:23 +05303from frappe import _
Suraj Shetty1eeb89f2019-05-22 06:37:43 +05304
5# api/method/erpnext.erpnext_integrations.exotel_integration.handle_incoming_call
Suraj Shettyc8c17422019-06-07 10:22:50 +05306# api/method/erpnext.erpnext_integrations.exotel_integration.handle_end_call
7# api/method/erpnext.erpnext_integrations.exotel_integration.handle_missed_call
Suraj Shetty863b93c2019-05-21 07:57:06 +05308
Ankush Menat494bd9e2022-03-28 18:52:46 +05309
Suraj Shetty863b93c2019-05-21 07:57:06 +053010@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053011def handle_incoming_call(**kwargs):
Suraj Shettyaf2eac42019-09-17 15:53:23 +053012 try:
13 exotel_settings = get_exotel_settings()
Ankush Menat494bd9e2022-03-28 18:52:46 +053014 if not exotel_settings.enabled:
15 return
Suraj Shetty863b93c2019-05-21 07:57:06 +053016
Suraj Shettyaf2eac42019-09-17 15:53:23 +053017 call_payload = kwargs
Ankush Menat494bd9e2022-03-28 18:52:46 +053018 status = call_payload.get("Status")
19 if status == "free":
Suraj Shettyaf2eac42019-09-17 15:53:23 +053020 return
Suraj Shetty07fe2992019-05-22 15:48:57 +053021
Suraj Shettyaf2eac42019-09-17 15:53:23 +053022 call_log = get_call_log(call_payload)
23 if not call_log:
24 create_call_log(call_payload)
25 else:
26 update_call_log(call_payload, call_log=call_log)
27 except Exception as e:
28 frappe.db.rollback()
Ankush Menat494bd9e2022-03-28 18:52:46 +053029 frappe.log_error(title=_("Error in Exotel incoming call"))
Suraj Shettyaf2eac42019-09-17 15:53:23 +053030 frappe.db.commit()
Suraj Shetty07fe2992019-05-22 15:48:57 +053031
Ankush Menat494bd9e2022-03-28 18:52:46 +053032
Suraj Shettybd03a512019-05-27 15:30:41 +053033@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053034def handle_end_call(**kwargs):
Ankush Menat494bd9e2022-03-28 18:52:46 +053035 update_call_log(kwargs, "Completed")
36
Suraj Shettye9bfecf2019-06-03 12:27:02 +053037
38@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053039def handle_missed_call(**kwargs):
Subin Tom8d301182022-02-23 12:59:38 +053040 status = ""
Subin Tom5b794962022-02-25 16:52:25 +053041 call_type = kwargs.get("CallType")
42 dial_call_status = kwargs.get("DialCallStatus")
Subin Tom8d301182022-02-23 12:59:38 +053043
Subin Tom5b794962022-02-25 16:52:25 +053044 if call_type == "incomplete" and dial_call_status == "no-answer":
Ankush Menat7ef54802022-03-28 19:55:39 +053045 status = "No Answer"
Subin Tom5b794962022-02-25 16:52:25 +053046 elif call_type == "client-hangup" and dial_call_status == "canceled":
Ankush Menat7ef54802022-03-28 19:55:39 +053047 status = "Canceled"
Subin Tom5b794962022-02-25 16:52:25 +053048 elif call_type == "incomplete" and dial_call_status == "failed":
Ankush Menat7ef54802022-03-28 19:55:39 +053049 status = "Failed"
Subin Tom8d301182022-02-23 12:59:38 +053050
51 update_call_log(kwargs, status)
Suraj Shettye9bfecf2019-06-03 12:27:02 +053052
Ankush Menat494bd9e2022-03-28 18:52:46 +053053
54def update_call_log(call_payload, status="Ringing", call_log=None):
Suraj Shettyf5dd4942019-07-16 11:07:25 +053055 call_log = call_log or get_call_log(call_payload)
Subin Tom8d301182022-02-23 12:59:38 +053056
57 # for a new sid, call_log and get_call_log will be empty so create a new log
58 if not call_log:
59 call_log = create_call_log(call_payload)
Suraj Shettybd03a512019-05-27 15:30:41 +053060 if call_log:
Suraj Shettyc8c17422019-06-07 10:22:50 +053061 call_log.status = status
Ankush Menat494bd9e2022-03-28 18:52:46 +053062 call_log.to = call_payload.get("DialWhomNumber")
63 call_log.duration = call_payload.get("DialCallDuration") or 0
64 call_log.recording_url = call_payload.get("RecordingUrl")
Suraj Shettybd03a512019-05-27 15:30:41 +053065 call_log.save(ignore_permissions=True)
66 frappe.db.commit()
Suraj Shettyc8c17422019-06-07 10:22:50 +053067 return call_log
Suraj Shetty863b93c2019-05-21 07:57:06 +053068
Ankush Menat494bd9e2022-03-28 18:52:46 +053069
Suraj Shetty502565f2019-07-01 14:28:59 +053070def get_call_log(call_payload):
Suraj Shetty39abfae2022-04-04 07:28:41 +053071 call_log_id = call_payload.get("CallSid")
72 if frappe.db.exists("Call Log", call_log_id):
73 return frappe.get_doc("Call Log", call_log_id)
Ankush Menat494bd9e2022-03-28 18:52:46 +053074
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053075
Suraj Shetty502565f2019-07-01 14:28:59 +053076def create_call_log(call_payload):
Ankush Menat494bd9e2022-03-28 18:52:46 +053077 call_log = frappe.new_doc("Call Log")
78 call_log.id = call_payload.get("CallSid")
79 call_log.to = call_payload.get("DialWhomNumber")
80 call_log.medium = call_payload.get("To")
81 call_log.status = "Ringing"
82 setattr(call_log, "from", call_payload.get("CallFrom"))
Suraj Shetty502565f2019-07-01 14:28:59 +053083 call_log.save(ignore_permissions=True)
84 frappe.db.commit()
85 return call_log
Suraj Shetty340ccb62019-06-17 10:16:38 +053086
Ankush Menat494bd9e2022-03-28 18:52:46 +053087
Suraj Shetty07fe2992019-05-22 15:48:57 +053088@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053089def get_call_status(call_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +053090 endpoint = get_exotel_endpoint("Calls/{call_id}.json".format(call_id=call_id))
Suraj Shetty06f80342019-06-13 17:13:54 +053091 response = requests.get(endpoint)
Ankush Menat494bd9e2022-03-28 18:52:46 +053092 status = response.json().get("Call", {}).get("Status")
Suraj Shetty07fe2992019-05-22 15:48:57 +053093 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053094
Ankush Menat494bd9e2022-03-28 18:52:46 +053095
Suraj Shetty07fe2992019-05-22 15:48:57 +053096@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053097def make_a_call(from_number, to_number, caller_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +053098 endpoint = get_exotel_endpoint("Calls/connect.json?details=true")
99 response = requests.post(
100 endpoint, data={"From": from_number, "To": to_number, "CallerId": caller_id}
101 )
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530102
103 return response.json()
104
Ankush Menat494bd9e2022-03-28 18:52:46 +0530105
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530106def get_exotel_settings():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530107 return frappe.get_single("Exotel Settings")
108
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530109
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530110def whitelist_numbers(numbers, caller_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530111 endpoint = get_exotel_endpoint("CustomerWhitelist")
112 response = requests.post(
113 endpoint,
114 data={
115 "VirtualNumber": caller_id,
116 "Number": numbers,
117 },
118 )
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530119
Suraj Shetty06f80342019-06-13 17:13:54 +0530120 return response
121
Ankush Menat494bd9e2022-03-28 18:52:46 +0530122
Suraj Shetty06f80342019-06-13 17:13:54 +0530123def get_all_exophones():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530124 endpoint = get_exotel_endpoint("IncomingPhoneNumbers")
Suraj Shetty06f80342019-06-13 17:13:54 +0530125 response = requests.post(endpoint)
126 return response
127
Ankush Menat494bd9e2022-03-28 18:52:46 +0530128
Suraj Shetty06f80342019-06-13 17:13:54 +0530129def get_exotel_endpoint(action):
130 settings = get_exotel_settings()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530131 return "https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}".format(
132 api_key=settings.api_key, api_token=settings.api_token, sid=settings.account_sid, action=action
Suraj Shettyaf2eac42019-09-17 15:53:23 +0530133 )