blob: fd0f78357599eaf47ef1e272f96acca1bae9d971 [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
Suraj Shetty502565f2019-07-01 14:28:59 +053075def create_call_log(call_payload):
Ankush Menat494bd9e2022-03-28 18:52:46 +053076 call_log = frappe.new_doc("Call Log")
77 call_log.id = call_payload.get("CallSid")
78 call_log.to = call_payload.get("DialWhomNumber")
79 call_log.medium = call_payload.get("To")
80 call_log.status = "Ringing"
81 setattr(call_log, "from", call_payload.get("CallFrom"))
Suraj Shetty502565f2019-07-01 14:28:59 +053082 call_log.save(ignore_permissions=True)
83 frappe.db.commit()
84 return call_log
Suraj Shetty340ccb62019-06-17 10:16:38 +053085
Ankush Menat494bd9e2022-03-28 18:52:46 +053086
Suraj Shetty07fe2992019-05-22 15:48:57 +053087@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053088def get_call_status(call_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +053089 endpoint = get_exotel_endpoint("Calls/{call_id}.json".format(call_id=call_id))
Suraj Shetty06f80342019-06-13 17:13:54 +053090 response = requests.get(endpoint)
Ankush Menat494bd9e2022-03-28 18:52:46 +053091 status = response.json().get("Call", {}).get("Status")
Suraj Shetty07fe2992019-05-22 15:48:57 +053092 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +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 make_a_call(from_number, to_number, caller_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +053097 endpoint = get_exotel_endpoint("Calls/connect.json?details=true")
98 response = requests.post(
99 endpoint, data={"From": from_number, "To": to_number, "CallerId": caller_id}
100 )
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530101
102 return response.json()
103
Ankush Menat494bd9e2022-03-28 18:52:46 +0530104
Suraj Shetty1eeb89f2019-05-22 06:37:43 +0530105def get_exotel_settings():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530106 return frappe.get_single("Exotel Settings")
107
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530108
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530109def whitelist_numbers(numbers, caller_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530110 endpoint = get_exotel_endpoint("CustomerWhitelist")
111 response = requests.post(
112 endpoint,
113 data={
114 "VirtualNumber": caller_id,
115 "Number": numbers,
116 },
117 )
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530118
Suraj Shetty06f80342019-06-13 17:13:54 +0530119 return response
120
Ankush Menat494bd9e2022-03-28 18:52:46 +0530121
Suraj Shetty06f80342019-06-13 17:13:54 +0530122def get_all_exophones():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530123 endpoint = get_exotel_endpoint("IncomingPhoneNumbers")
Suraj Shetty06f80342019-06-13 17:13:54 +0530124 response = requests.post(endpoint)
125 return response
126
Ankush Menat494bd9e2022-03-28 18:52:46 +0530127
Suraj Shetty06f80342019-06-13 17:13:54 +0530128def get_exotel_endpoint(action):
129 settings = get_exotel_settings()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530130 return "https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}".format(
131 api_key=settings.api_key, api_token=settings.api_token, sid=settings.account_sid, action=action
Suraj Shettyaf2eac42019-09-17 15:53:23 +0530132 )