blob: 82bcb8d0dba0b8227f168f971ed12081e9289cf9 [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):
Ankush Menat494bd9e2022-03-28 18:52:46 +053071 call_log = frappe.get_all(
72 "Call Log",
73 {
74 "id": call_payload.get("CallSid"),
75 },
76 limit=1,
77 )
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053078
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053079 if call_log:
Ankush Menat494bd9e2022-03-28 18:52:46 +053080 return frappe.get_doc("Call Log", call_log[0].name)
81
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053082
Suraj Shetty502565f2019-07-01 14:28:59 +053083def create_call_log(call_payload):
Ankush Menat494bd9e2022-03-28 18:52:46 +053084 call_log = frappe.new_doc("Call Log")
85 call_log.id = call_payload.get("CallSid")
86 call_log.to = call_payload.get("DialWhomNumber")
87 call_log.medium = call_payload.get("To")
88 call_log.status = "Ringing"
89 setattr(call_log, "from", call_payload.get("CallFrom"))
Suraj Shetty502565f2019-07-01 14:28:59 +053090 call_log.save(ignore_permissions=True)
91 frappe.db.commit()
92 return call_log
Suraj Shetty340ccb62019-06-17 10:16:38 +053093
Ankush Menat494bd9e2022-03-28 18:52:46 +053094
Suraj Shetty07fe2992019-05-22 15:48:57 +053095@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053096def get_call_status(call_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +053097 endpoint = get_exotel_endpoint("Calls/{call_id}.json".format(call_id=call_id))
Suraj Shetty06f80342019-06-13 17:13:54 +053098 response = requests.get(endpoint)
Ankush Menat494bd9e2022-03-28 18:52:46 +053099 status = response.json().get("Call", {}).get("Status")
Suraj Shetty07fe2992019-05-22 15:48:57 +0530100 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530101
Ankush Menat494bd9e2022-03-28 18:52:46 +0530102
Suraj Shetty07fe2992019-05-22 15:48:57 +0530103@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530104def make_a_call(from_number, to_number, caller_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530105 endpoint = get_exotel_endpoint("Calls/connect.json?details=true")
106 response = requests.post(
107 endpoint, data={"From": from_number, "To": to_number, "CallerId": caller_id}
108 )
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530109
110 return response.json()
111
Ankush Menat494bd9e2022-03-28 18:52:46 +0530112
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530113def get_exotel_settings():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530114 return frappe.get_single("Exotel Settings")
115
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530116
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530117def whitelist_numbers(numbers, caller_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530118 endpoint = get_exotel_endpoint("CustomerWhitelist")
119 response = requests.post(
120 endpoint,
121 data={
122 "VirtualNumber": caller_id,
123 "Number": numbers,
124 },
125 )
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530126
Suraj Shetty06f80342019-06-13 17:13:54 +0530127 return response
128
Ankush Menat494bd9e2022-03-28 18:52:46 +0530129
Suraj Shetty06f80342019-06-13 17:13:54 +0530130def get_all_exophones():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530131 endpoint = get_exotel_endpoint("IncomingPhoneNumbers")
Suraj Shetty06f80342019-06-13 17:13:54 +0530132 response = requests.post(endpoint)
133 return response
134
Ankush Menat494bd9e2022-03-28 18:52:46 +0530135
Suraj Shetty06f80342019-06-13 17:13:54 +0530136def get_exotel_endpoint(action):
137 settings = get_exotel_settings()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530138 return "https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}".format(
139 api_key=settings.api_key, api_token=settings.api_token, sid=settings.account_sid, action=action
Suraj Shettyaf2eac42019-09-17 15:53:23 +0530140 )