blob: 1f5df67e1979da79305e7a41b3b69e85925a33ed [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):
Ankush Menat494bd9e2022-03-28 18:52:46 +053040 update_call_log(kwargs, "Missed")
Suraj Shettye9bfecf2019-06-03 12:27:02 +053041
Ankush Menat494bd9e2022-03-28 18:52:46 +053042
43def update_call_log(call_payload, status="Ringing", call_log=None):
Suraj Shettyf5dd4942019-07-16 11:07:25 +053044 call_log = call_log or get_call_log(call_payload)
Suraj Shettybd03a512019-05-27 15:30:41 +053045 if call_log:
Suraj Shettyc8c17422019-06-07 10:22:50 +053046 call_log.status = status
Ankush Menat494bd9e2022-03-28 18:52:46 +053047 call_log.to = call_payload.get("DialWhomNumber")
48 call_log.duration = call_payload.get("DialCallDuration") or 0
49 call_log.recording_url = call_payload.get("RecordingUrl")
Suraj Shettybd03a512019-05-27 15:30:41 +053050 call_log.save(ignore_permissions=True)
51 frappe.db.commit()
Suraj Shettyc8c17422019-06-07 10:22:50 +053052 return call_log
Suraj Shetty863b93c2019-05-21 07:57:06 +053053
Ankush Menat494bd9e2022-03-28 18:52:46 +053054
Suraj Shetty502565f2019-07-01 14:28:59 +053055def get_call_log(call_payload):
Ankush Menat494bd9e2022-03-28 18:52:46 +053056 call_log = frappe.get_all(
57 "Call Log",
58 {
59 "id": call_payload.get("CallSid"),
60 },
61 limit=1,
62 )
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053063
Suraj Shetty44c0e9d2019-06-06 11:18:16 +053064 if call_log:
Ankush Menat494bd9e2022-03-28 18:52:46 +053065 return frappe.get_doc("Call Log", call_log[0].name)
66
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053067
Suraj Shetty502565f2019-07-01 14:28:59 +053068def create_call_log(call_payload):
Ankush Menat494bd9e2022-03-28 18:52:46 +053069 call_log = frappe.new_doc("Call Log")
70 call_log.id = call_payload.get("CallSid")
71 call_log.to = call_payload.get("DialWhomNumber")
72 call_log.medium = call_payload.get("To")
73 call_log.status = "Ringing"
74 setattr(call_log, "from", call_payload.get("CallFrom"))
Suraj Shetty502565f2019-07-01 14:28:59 +053075 call_log.save(ignore_permissions=True)
76 frappe.db.commit()
77 return call_log
Suraj Shetty340ccb62019-06-17 10:16:38 +053078
Ankush Menat494bd9e2022-03-28 18:52:46 +053079
Suraj Shetty07fe2992019-05-22 15:48:57 +053080@frappe.whitelist()
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053081def get_call_status(call_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +053082 endpoint = get_exotel_endpoint("Calls/{call_id}.json".format(call_id=call_id))
Suraj Shetty06f80342019-06-13 17:13:54 +053083 response = requests.get(endpoint)
Ankush Menat494bd9e2022-03-28 18:52:46 +053084 status = response.json().get("Call", {}).get("Status")
Suraj Shetty07fe2992019-05-22 15:48:57 +053085 return status
Suraj Shetty1eeb89f2019-05-22 06:37:43 +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 make_a_call(from_number, to_number, caller_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +053090 endpoint = get_exotel_endpoint("Calls/connect.json?details=true")
91 response = requests.post(
92 endpoint, data={"From": from_number, "To": to_number, "CallerId": caller_id}
93 )
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053094
95 return response.json()
96
Ankush Menat494bd9e2022-03-28 18:52:46 +053097
Suraj Shetty1eeb89f2019-05-22 06:37:43 +053098def get_exotel_settings():
Ankush Menat494bd9e2022-03-28 18:52:46 +053099 return frappe.get_single("Exotel Settings")
100
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530101
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530102def whitelist_numbers(numbers, caller_id):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530103 endpoint = get_exotel_endpoint("CustomerWhitelist")
104 response = requests.post(
105 endpoint,
106 data={
107 "VirtualNumber": caller_id,
108 "Number": numbers,
109 },
110 )
Suraj Shettye9bfecf2019-06-03 12:27:02 +0530111
Suraj Shetty06f80342019-06-13 17:13:54 +0530112 return response
113
Ankush Menat494bd9e2022-03-28 18:52:46 +0530114
Suraj Shetty06f80342019-06-13 17:13:54 +0530115def get_all_exophones():
Ankush Menat494bd9e2022-03-28 18:52:46 +0530116 endpoint = get_exotel_endpoint("IncomingPhoneNumbers")
Suraj Shetty06f80342019-06-13 17:13:54 +0530117 response = requests.post(endpoint)
118 return response
119
Ankush Menat494bd9e2022-03-28 18:52:46 +0530120
Suraj Shetty06f80342019-06-13 17:13:54 +0530121def get_exotel_endpoint(action):
122 settings = get_exotel_settings()
Ankush Menat494bd9e2022-03-28 18:52:46 +0530123 return "https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}".format(
124 api_key=settings.api_key, api_token=settings.api_token, sid=settings.account_sid, action=action
Suraj Shettyaf2eac42019-09-17 15:53:23 +0530125 )