blob: fd9f74e8c9b70ce61fd9c1dcedecfa1f233a3997 [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 Shetty07fe2992019-05-22 15:48:57 +053029
Ankush Menat494bd9e2022-03-28 18:52:46 +053030
Suraj Shettybd03a512019-05-27 15:30:41 +053031@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053032def handle_end_call(**kwargs):
Ankush Menat494bd9e2022-03-28 18:52:46 +053033 update_call_log(kwargs, "Completed")
34
Suraj Shettye9bfecf2019-06-03 12:27:02 +053035
36@frappe.whitelist(allow_guest=True)
Suraj Shetty502565f2019-07-01 14:28:59 +053037def handle_missed_call(**kwargs):
Subin Tom8d301182022-02-23 12:59:38 +053038 status = ""
Subin Tom5b794962022-02-25 16:52:25 +053039 call_type = kwargs.get("CallType")
40 dial_call_status = kwargs.get("DialCallStatus")
Subin Tom8d301182022-02-23 12:59:38 +053041
Subin Tom5b794962022-02-25 16:52:25 +053042 if call_type == "incomplete" and dial_call_status == "no-answer":
Ankush Menat7ef54802022-03-28 19:55:39 +053043 status = "No Answer"
Subin Tom5b794962022-02-25 16:52:25 +053044 elif call_type == "client-hangup" and dial_call_status == "canceled":
Ankush Menat7ef54802022-03-28 19:55:39 +053045 status = "Canceled"
Subin Tom5b794962022-02-25 16:52:25 +053046 elif call_type == "incomplete" and dial_call_status == "failed":
Ankush Menat7ef54802022-03-28 19:55:39 +053047 status = "Failed"
Subin Tom8d301182022-02-23 12:59:38 +053048
49 update_call_log(kwargs, status)
Suraj Shettye9bfecf2019-06-03 12:27:02 +053050
Ankush Menat494bd9e2022-03-28 18:52:46 +053051
52def update_call_log(call_payload, status="Ringing", call_log=None):
Suraj Shettyf5dd4942019-07-16 11:07:25 +053053 call_log = call_log or get_call_log(call_payload)
Subin Tom8d301182022-02-23 12:59:38 +053054
55 # for a new sid, call_log and get_call_log will be empty so create a new log
56 if not call_log:
57 call_log = create_call_log(call_payload)
Suraj Shettybd03a512019-05-27 15:30:41 +053058 if call_log:
Suraj Shettyc8c17422019-06-07 10:22:50 +053059 call_log.status = status
Ankush Menat494bd9e2022-03-28 18:52:46 +053060 call_log.to = call_payload.get("DialWhomNumber")
61 call_log.duration = call_payload.get("DialCallDuration") or 0
62 call_log.recording_url = call_payload.get("RecordingUrl")
Suraj Shettybd03a512019-05-27 15:30:41 +053063 call_log.save(ignore_permissions=True)
64 frappe.db.commit()
Suraj Shettyc8c17422019-06-07 10:22:50 +053065 return call_log
Suraj Shetty863b93c2019-05-21 07:57:06 +053066
Ankush Menat494bd9e2022-03-28 18:52:46 +053067
Suraj Shetty502565f2019-07-01 14:28:59 +053068def get_call_log(call_payload):
Suraj Shetty39abfae2022-04-04 07:28:41 +053069 call_log_id = call_payload.get("CallSid")
70 if frappe.db.exists("Call Log", call_log_id):
71 return frappe.get_doc("Call Log", call_log_id)
Ankush Menat494bd9e2022-03-28 18:52:46 +053072
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053073
Suraj Shetty502565f2019-07-01 14:28:59 +053074def create_call_log(call_payload):
Ankush Menat494bd9e2022-03-28 18:52:46 +053075 call_log = frappe.new_doc("Call Log")
76 call_log.id = call_payload.get("CallSid")
77 call_log.to = call_payload.get("DialWhomNumber")
78 call_log.medium = call_payload.get("To")
79 call_log.status = "Ringing"
80 setattr(call_log, "from", call_payload.get("CallFrom"))
Suraj Shetty502565f2019-07-01 14:28:59 +053081 call_log.save(ignore_permissions=True)
82 frappe.db.commit()
83 return call_log
Suraj Shetty340ccb62019-06-17 10:16:38 +053084
Ankush Menat494bd9e2022-03-28 18:52:46 +053085
Suraj Shetty07fe2992019-05-22 15:48:57 +053086@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053087def get_call_status(call_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +053088 endpoint = get_exotel_endpoint("Calls/{call_id}.json".format(call_id=call_id))
Suraj Shetty06f80342019-06-13 17:13:54 +053089 response = requests.get(endpoint)
Ankush Menat494bd9e2022-03-28 18:52:46 +053090 status = response.json().get("Call", {}).get("Status")
Suraj Shetty07fe2992019-05-22 15:48:57 +053091 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053092
Ankush Menat494bd9e2022-03-28 18:52:46 +053093
Suraj Shetty07fe2992019-05-22 15:48:57 +053094@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053095def make_a_call(from_number, to_number, caller_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +053096 endpoint = get_exotel_endpoint("Calls/connect.json?details=true")
97 response = requests.post(
98 endpoint, data={"From": from_number, "To": to_number, "CallerId": caller_id}
99 )
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530100
101 return response.json()
102
Ankush Menat494bd9e2022-03-28 18:52:46 +0530103
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530104def get_exotel_settings():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530105 return frappe.get_single("Exotel Settings")
106
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530107
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530108def whitelist_numbers(numbers, caller_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530109 endpoint = get_exotel_endpoint("CustomerWhitelist")
110 response = requests.post(
111 endpoint,
112 data={
113 "VirtualNumber": caller_id,
114 "Number": numbers,
115 },
116 )
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530117
Suraj Shetty06f80342019-06-13 17:13:54 +0530118 return response
119
Ankush Menat494bd9e2022-03-28 18:52:46 +0530120
Suraj Shetty06f80342019-06-13 17:13:54 +0530121def get_all_exophones():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530122 endpoint = get_exotel_endpoint("IncomingPhoneNumbers")
Suraj Shetty06f80342019-06-13 17:13:54 +0530123 response = requests.post(endpoint)
124 return response
125
Ankush Menat494bd9e2022-03-28 18:52:46 +0530126
Suraj Shetty06f80342019-06-13 17:13:54 +0530127def get_exotel_endpoint(action):
128 settings = get_exotel_settings()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530129 return "https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}".format(
130 api_key=settings.api_key, api_token=settings.api_token, sid=settings.account_sid, action=action
Suraj Shettyaf2eac42019-09-17 15:53:23 +0530131 )