Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 1 | import frappe |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 2 | import requests |
| 3 | |
| 4 | # api/method/erpnext.erpnext_integrations.exotel_integration.handle_incoming_call |
Suraj Shetty | c8c1742 | 2019-06-07 10:22:50 +0530 | [diff] [blame] | 5 | # api/method/erpnext.erpnext_integrations.exotel_integration.handle_end_call |
| 6 | # api/method/erpnext.erpnext_integrations.exotel_integration.handle_missed_call |
Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 7 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 8 | |
Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 9 | @frappe.whitelist(allow_guest=True) |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 10 | def handle_incoming_call(**kwargs): |
Suraj Shetty | af2eac4 | 2019-09-17 15:53:23 +0530 | [diff] [blame] | 11 | try: |
| 12 | exotel_settings = get_exotel_settings() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 13 | if not exotel_settings.enabled: |
| 14 | return |
Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 15 | |
Suraj Shetty | af2eac4 | 2019-09-17 15:53:23 +0530 | [diff] [blame] | 16 | call_payload = kwargs |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 17 | status = call_payload.get("Status") |
| 18 | if status == "free": |
Suraj Shetty | af2eac4 | 2019-09-17 15:53:23 +0530 | [diff] [blame] | 19 | return |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 20 | |
Suraj Shetty | af2eac4 | 2019-09-17 15:53:23 +0530 | [diff] [blame] | 21 | 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 Mehta | 548afba | 2022-05-02 15:04:26 +0530 | [diff] [blame] | 28 | exotel_settings.log_error("Error in Exotel incoming call") |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 29 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 30 | |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 31 | @frappe.whitelist(allow_guest=True) |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 32 | def handle_end_call(**kwargs): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 33 | update_call_log(kwargs, "Completed") |
| 34 | |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 35 | |
| 36 | @frappe.whitelist(allow_guest=True) |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 37 | def handle_missed_call(**kwargs): |
Subin Tom | 8d30118 | 2022-02-23 12:59:38 +0530 | [diff] [blame] | 38 | status = "" |
Subin Tom | 5b79496 | 2022-02-25 16:52:25 +0530 | [diff] [blame] | 39 | call_type = kwargs.get("CallType") |
| 40 | dial_call_status = kwargs.get("DialCallStatus") |
Subin Tom | 8d30118 | 2022-02-23 12:59:38 +0530 | [diff] [blame] | 41 | |
Subin Tom | 5b79496 | 2022-02-25 16:52:25 +0530 | [diff] [blame] | 42 | if call_type == "incomplete" and dial_call_status == "no-answer": |
Ankush Menat | 7ef5480 | 2022-03-28 19:55:39 +0530 | [diff] [blame] | 43 | status = "No Answer" |
Subin Tom | 5b79496 | 2022-02-25 16:52:25 +0530 | [diff] [blame] | 44 | elif call_type == "client-hangup" and dial_call_status == "canceled": |
Ankush Menat | 7ef5480 | 2022-03-28 19:55:39 +0530 | [diff] [blame] | 45 | status = "Canceled" |
Subin Tom | 5b79496 | 2022-02-25 16:52:25 +0530 | [diff] [blame] | 46 | elif call_type == "incomplete" and dial_call_status == "failed": |
Ankush Menat | 7ef5480 | 2022-03-28 19:55:39 +0530 | [diff] [blame] | 47 | status = "Failed" |
Subin Tom | 8d30118 | 2022-02-23 12:59:38 +0530 | [diff] [blame] | 48 | |
| 49 | update_call_log(kwargs, status) |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 50 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 51 | |
| 52 | def update_call_log(call_payload, status="Ringing", call_log=None): |
Suraj Shetty | f5dd494 | 2019-07-16 11:07:25 +0530 | [diff] [blame] | 53 | call_log = call_log or get_call_log(call_payload) |
Subin Tom | 8d30118 | 2022-02-23 12:59:38 +0530 | [diff] [blame] | 54 | |
| 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 Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 58 | if call_log: |
Suraj Shetty | c8c1742 | 2019-06-07 10:22:50 +0530 | [diff] [blame] | 59 | call_log.status = status |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 60 | 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 Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 63 | call_log.save(ignore_permissions=True) |
| 64 | frappe.db.commit() |
Suraj Shetty | c8c1742 | 2019-06-07 10:22:50 +0530 | [diff] [blame] | 65 | return call_log |
Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 66 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 67 | |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 68 | def get_call_log(call_payload): |
Suraj Shetty | 39abfae | 2022-04-04 07:28:41 +0530 | [diff] [blame] | 69 | 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 Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 72 | |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 73 | |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 74 | def create_call_log(call_payload): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 75 | 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 Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 81 | call_log.save(ignore_permissions=True) |
| 82 | frappe.db.commit() |
| 83 | return call_log |
Suraj Shetty | 340ccb6 | 2019-06-17 10:16:38 +0530 | [diff] [blame] | 84 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 85 | |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 86 | @frappe.whitelist() |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 87 | def get_call_status(call_id): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 88 | endpoint = get_exotel_endpoint("Calls/{call_id}.json".format(call_id=call_id)) |
Suraj Shetty | 06f8034 | 2019-06-13 17:13:54 +0530 | [diff] [blame] | 89 | response = requests.get(endpoint) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 90 | status = response.json().get("Call", {}).get("Status") |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 91 | return status |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 92 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 93 | |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 94 | @frappe.whitelist() |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 95 | def make_a_call(from_number, to_number, caller_id): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 96 | 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 Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 100 | |
| 101 | return response.json() |
| 102 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 103 | |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 104 | def get_exotel_settings(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 105 | return frappe.get_single("Exotel Settings") |
| 106 | |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 107 | |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 108 | def whitelist_numbers(numbers, caller_id): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 109 | endpoint = get_exotel_endpoint("CustomerWhitelist") |
| 110 | response = requests.post( |
| 111 | endpoint, |
| 112 | data={ |
| 113 | "VirtualNumber": caller_id, |
| 114 | "Number": numbers, |
| 115 | }, |
| 116 | ) |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 117 | |
Suraj Shetty | 06f8034 | 2019-06-13 17:13:54 +0530 | [diff] [blame] | 118 | return response |
| 119 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 120 | |
Suraj Shetty | 06f8034 | 2019-06-13 17:13:54 +0530 | [diff] [blame] | 121 | def get_all_exophones(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 122 | endpoint = get_exotel_endpoint("IncomingPhoneNumbers") |
Suraj Shetty | 06f8034 | 2019-06-13 17:13:54 +0530 | [diff] [blame] | 123 | response = requests.post(endpoint) |
| 124 | return response |
| 125 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 126 | |
Suraj Shetty | 06f8034 | 2019-06-13 17:13:54 +0530 | [diff] [blame] | 127 | def get_exotel_endpoint(action): |
| 128 | settings = get_exotel_settings() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 129 | 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 Shetty | af2eac4 | 2019-09-17 15:53:23 +0530 | [diff] [blame] | 131 | ) |