blob: 0d40667e32a6a9d654e8e80e1ef67ba26b1e2b4b [file] [log] [blame]
Suraj Shetty863b93c2019-05-21 07:57:06 +05301import frappe
Suraj Shetty1eeb89f2019-05-22 06:37:43 +05302import requests
3
4# api/method/erpnext.erpnext_integrations.exotel_integration.handle_incoming_call
Suraj Shettyc8c17422019-06-07 10:22:50 +05305# api/method/erpnext.erpnext_integrations.exotel_integration.handle_end_call
6# api/method/erpnext.erpnext_integrations.exotel_integration.handle_missed_call
Suraj Shetty863b93c2019-05-21 07:57:06 +05307
Ankush Menat494bd9e2022-03-28 18:52:46 +05308
Suraj Shetty863b93c2019-05-21 07:57:06 +05309@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053010def handle_incoming_call(**kwargs):
Suraj Shettyaf2eac42019-09-17 15:53:23 +053011 try:
12 exotel_settings = get_exotel_settings()
Ankush Menat494bd9e2022-03-28 18:52:46 +053013 if not exotel_settings.enabled:
14 return
Suraj Shetty863b93c2019-05-21 07:57:06 +053015
Suraj Shettyaf2eac42019-09-17 15:53:23 +053016 call_payload = kwargs
Ankush Menat494bd9e2022-03-28 18:52:46 +053017 status = call_payload.get("Status")
18 if status == "free":
Suraj Shettyaf2eac42019-09-17 15:53:23 +053019 return
Suraj Shetty07fe2992019-05-22 15:48:57 +053020
Suraj Shettyaf2eac42019-09-17 15:53:23 +053021 call_log = get_call_log(call_payload)
22 if not call_log:
23 create_call_log(call_payload)
24 else:
25 update_call_log(call_payload, call_log=call_log)
26 except Exception as e:
27 frappe.db.rollback()
Rushabh Mehta548afba2022-05-02 15:04:26 +053028 exotel_settings.log_error("Error in Exotel incoming call")
Suraj Shetty122f1c02022-08-24 18:24:39 +053029 frappe.db.commit()
Suraj Shetty07fe2992019-05-22 15:48:57 +053030
Ankush Menat494bd9e2022-03-28 18:52:46 +053031
Suraj Shettybd03a512019-05-27 15:30:41 +053032@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053033def handle_end_call(**kwargs):
Ankush Menat494bd9e2022-03-28 18:52:46 +053034 update_call_log(kwargs, "Completed")
35
Suraj Shettye9bfecf2019-06-03 12:27:02 +053036
37@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053038def handle_missed_call(**kwargs):
Subin Tom8d301182022-02-23 12:59:38 +053039 status = ""
Subin Tom5b794962022-02-25 16:52:25 +053040 call_type = kwargs.get("CallType")
41 dial_call_status = kwargs.get("DialCallStatus")
Subin Tom8d301182022-02-23 12:59:38 +053042
Subin Tom5b794962022-02-25 16:52:25 +053043 if call_type == "incomplete" and dial_call_status == "no-answer":
Ankush Menat7ef54802022-03-28 19:55:39 +053044 status = "No Answer"
Subin Tom5b794962022-02-25 16:52:25 +053045 elif call_type == "client-hangup" and dial_call_status == "canceled":
Ankush Menat7ef54802022-03-28 19:55:39 +053046 status = "Canceled"
Subin Tom5b794962022-02-25 16:52:25 +053047 elif call_type == "incomplete" and dial_call_status == "failed":
Ankush Menat7ef54802022-03-28 19:55:39 +053048 status = "Failed"
Subin Tom8d301182022-02-23 12:59:38 +053049
50 update_call_log(kwargs, status)
Suraj Shettye9bfecf2019-06-03 12:27:02 +053051
Ankush Menat494bd9e2022-03-28 18:52:46 +053052
53def update_call_log(call_payload, status="Ringing", call_log=None):
Suraj Shettyf5dd4942019-07-16 11:07:25 +053054 call_log = call_log or get_call_log(call_payload)
Subin Tom8d301182022-02-23 12:59:38 +053055
56 # for a new sid, call_log and get_call_log will be empty so create a new log
57 if not call_log:
58 call_log = create_call_log(call_payload)
Suraj Shettybd03a512019-05-27 15:30:41 +053059 if call_log:
Suraj Shettyc8c17422019-06-07 10:22:50 +053060 call_log.status = status
Ankush Menat494bd9e2022-03-28 18:52:46 +053061 call_log.to = call_payload.get("DialWhomNumber")
62 call_log.duration = call_payload.get("DialCallDuration") or 0
63 call_log.recording_url = call_payload.get("RecordingUrl")
Suraj Shettybd03a512019-05-27 15:30:41 +053064 call_log.save(ignore_permissions=True)
65 frappe.db.commit()
Suraj Shettyc8c17422019-06-07 10:22:50 +053066 return call_log
Suraj Shetty863b93c2019-05-21 07:57:06 +053067
Ankush Menat494bd9e2022-03-28 18:52:46 +053068
Suraj Shetty502565f2019-07-01 14:28:59 +053069def get_call_log(call_payload):
Suraj Shetty39abfae2022-04-04 07:28:41 +053070 call_log_id = call_payload.get("CallSid")
71 if frappe.db.exists("Call Log", call_log_id):
72 return frappe.get_doc("Call Log", call_log_id)
Ankush Menat494bd9e2022-03-28 18:52:46 +053073
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053074
Sabu Siyad8e271fd2022-12-28 08:11:28 +053075def map_custom_field(call_payload, call_log):
76 field_value = call_payload.get("CustomField")
77
78 if not field_value:
79 return call_log
80
81 settings = get_exotel_settings()
82 target_doctype = settings.target_doctype
83 mapping_enabled = settings.map_custom_field_to_doctype
84
85 if not mapping_enabled or not target_doctype:
86 return call_log
87
88 call_log.append("links", {"link_doctype": target_doctype, "link_name": field_value})
89
90 return call_log
91
92
Suraj Shetty502565f2019-07-01 14:28:59 +053093def create_call_log(call_payload):
Ankush Menat494bd9e2022-03-28 18:52:46 +053094 call_log = frappe.new_doc("Call Log")
95 call_log.id = call_payload.get("CallSid")
96 call_log.to = call_payload.get("DialWhomNumber")
97 call_log.medium = call_payload.get("To")
98 call_log.status = "Ringing"
99 setattr(call_log, "from", call_payload.get("CallFrom"))
Sabu Siyad8e271fd2022-12-28 08:11:28 +0530100 map_custom_field(call_payload, call_log)
Suraj Shetty502565f2019-07-01 14:28:59 +0530101 call_log.save(ignore_permissions=True)
102 frappe.db.commit()
103 return call_log
Suraj Shetty340ccb62019-06-17 10:16:38 +0530104
Ankush Menat494bd9e2022-03-28 18:52:46 +0530105
Suraj Shetty07fe2992019-05-22 15:48:57 +0530106@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530107def get_call_status(call_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530108 endpoint = get_exotel_endpoint("Calls/{call_id}.json".format(call_id=call_id))
Suraj Shetty06f80342019-06-13 17:13:54 +0530109 response = requests.get(endpoint)
Ankush Menat494bd9e2022-03-28 18:52:46 +0530110 status = response.json().get("Call", {}).get("Status")
Suraj Shetty07fe2992019-05-22 15:48:57 +0530111 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530112
Ankush Menat494bd9e2022-03-28 18:52:46 +0530113
Suraj Shetty07fe2992019-05-22 15:48:57 +0530114@frappe.whitelist()
Sabu Siyad8e271fd2022-12-28 08:11:28 +0530115def make_a_call(from_number, to_number, caller_id, **kwargs):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530116 endpoint = get_exotel_endpoint("Calls/connect.json?details=true")
117 response = requests.post(
Sabu Siyad8e271fd2022-12-28 08:11:28 +0530118 endpoint, data={"From": from_number, "To": to_number, "CallerId": caller_id, **kwargs}
Ankush Menat494bd9e2022-03-28 18:52:46 +0530119 )
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530120
121 return response.json()
122
Ankush Menat494bd9e2022-03-28 18:52:46 +0530123
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530124def get_exotel_settings():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530125 return frappe.get_single("Exotel Settings")
126
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530127
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530128def whitelist_numbers(numbers, caller_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530129 endpoint = get_exotel_endpoint("CustomerWhitelist")
130 response = requests.post(
131 endpoint,
132 data={
133 "VirtualNumber": caller_id,
134 "Number": numbers,
135 },
136 )
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530137
Suraj Shetty06f80342019-06-13 17:13:54 +0530138 return response
139
Ankush Menat494bd9e2022-03-28 18:52:46 +0530140
Suraj Shetty06f80342019-06-13 17:13:54 +0530141def get_all_exophones():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530142 endpoint = get_exotel_endpoint("IncomingPhoneNumbers")
Suraj Shetty06f80342019-06-13 17:13:54 +0530143 response = requests.post(endpoint)
144 return response
145
Ankush Menat494bd9e2022-03-28 18:52:46 +0530146
Suraj Shetty06f80342019-06-13 17:13:54 +0530147def get_exotel_endpoint(action):
148 settings = get_exotel_settings()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530149 return "https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}".format(
150 api_key=settings.api_key, api_token=settings.api_token, sid=settings.account_sid, action=action
Suraj Shettyaf2eac42019-09-17 15:53:23 +0530151 )