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 | |
| 8 | @frappe.whitelist(allow_guest=True) |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 9 | def handle_incoming_call(**kwargs): |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 10 | exotel_settings = get_exotel_settings() |
| 11 | if not exotel_settings.enabled: return |
Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 12 | |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 13 | call_payload = kwargs |
| 14 | status = call_payload.get('Status') |
Suraj Shetty | 39a4d59 | 2019-05-27 10:38:43 +0530 | [diff] [blame] | 15 | if status == 'free': |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 16 | return |
| 17 | |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 18 | call_log = get_call_log(call_payload) |
| 19 | if not call_log: |
| 20 | create_call_log(call_payload) |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 21 | |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 22 | @frappe.whitelist(allow_guest=True) |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 23 | def handle_end_call(**kwargs): |
Suraj Shetty | 340ccb6 | 2019-06-17 10:16:38 +0530 | [diff] [blame] | 24 | update_call_log(kwargs, 'Completed') |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 25 | |
| 26 | @frappe.whitelist(allow_guest=True) |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 27 | def handle_missed_call(**kwargs): |
Suraj Shetty | 340ccb6 | 2019-06-17 10:16:38 +0530 | [diff] [blame] | 28 | update_call_log(kwargs, 'Missed') |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 29 | |
Suraj Shetty | 87645e9 | 2019-06-06 11:24:31 +0530 | [diff] [blame] | 30 | def update_call_log(call_payload, status): |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 31 | call_log = get_call_log(call_payload) |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 32 | if call_log: |
Suraj Shetty | c8c1742 | 2019-06-07 10:22:50 +0530 | [diff] [blame] | 33 | call_log.status = status |
| 34 | call_log.duration = call_payload.get('DialCallDuration') or 0 |
Suraj Shetty | be1dddd | 2019-06-17 08:06:14 +0530 | [diff] [blame] | 35 | call_log.recording_url = call_payload.get('RecordingUrl') |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 36 | call_log.save(ignore_permissions=True) |
| 37 | frappe.db.commit() |
Suraj Shetty | c8c1742 | 2019-06-07 10:22:50 +0530 | [diff] [blame] | 38 | return call_log |
Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 39 | |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 40 | def get_call_log(call_payload): |
Suraj Shetty | 44c0e9d | 2019-06-06 11:18:16 +0530 | [diff] [blame] | 41 | call_log = frappe.get_all('Call Log', { |
Suraj Shetty | c8c1742 | 2019-06-07 10:22:50 +0530 | [diff] [blame] | 42 | 'id': call_payload.get('CallSid'), |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 43 | }, limit=1) |
| 44 | |
Suraj Shetty | 44c0e9d | 2019-06-06 11:18:16 +0530 | [diff] [blame] | 45 | if call_log: |
| 46 | return frappe.get_doc('Call Log', call_log[0].name) |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 47 | |
Suraj Shetty | 502565f | 2019-07-01 14:28:59 +0530 | [diff] [blame] | 48 | def create_call_log(call_payload): |
| 49 | call_log = frappe.new_doc('Call Log') |
| 50 | call_log.id = call_payload.get('CallSid') |
| 51 | call_log.to = call_payload.get('CallTo') |
| 52 | call_log.medium = call_payload.get('To') |
| 53 | call_log.status = 'Ringing' |
| 54 | setattr(call_log, 'from', call_payload.get('CallFrom')) |
| 55 | call_log.save(ignore_permissions=True) |
| 56 | frappe.db.commit() |
| 57 | return call_log |
Suraj Shetty | 340ccb6 | 2019-06-17 10:16:38 +0530 | [diff] [blame] | 58 | |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 59 | @frappe.whitelist() |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 60 | def get_call_status(call_id): |
Suraj Shetty | 06f8034 | 2019-06-13 17:13:54 +0530 | [diff] [blame] | 61 | endpoint = get_exotel_endpoint('Calls/{call_id}.json'.format(call_id=call_id)) |
| 62 | response = requests.get(endpoint) |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 63 | status = response.json().get('Call', {}).get('Status') |
| 64 | return status |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 65 | |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 66 | @frappe.whitelist() |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 67 | def make_a_call(from_number, to_number, caller_id): |
Suraj Shetty | 06f8034 | 2019-06-13 17:13:54 +0530 | [diff] [blame] | 68 | endpoint = get_exotel_endpoint('Calls/connect.json?details=true') |
| 69 | response = requests.post(endpoint, data={ |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 70 | 'From': from_number, |
| 71 | 'To': to_number, |
| 72 | 'CallerId': caller_id |
| 73 | }) |
| 74 | |
| 75 | return response.json() |
| 76 | |
| 77 | def get_exotel_settings(): |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 78 | return frappe.get_single('Exotel Settings') |
| 79 | |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 80 | def whitelist_numbers(numbers, caller_id): |
Suraj Shetty | 06f8034 | 2019-06-13 17:13:54 +0530 | [diff] [blame] | 81 | endpoint = get_exotel_endpoint('CustomerWhitelist') |
| 82 | response = requests.post(endpoint, data={ |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 83 | 'VirtualNumber': caller_id, |
| 84 | 'Number': numbers, |
| 85 | }) |
| 86 | |
Suraj Shetty | 06f8034 | 2019-06-13 17:13:54 +0530 | [diff] [blame] | 87 | return response |
| 88 | |
| 89 | def get_all_exophones(): |
| 90 | endpoint = get_exotel_endpoint('IncomingPhoneNumbers') |
| 91 | response = requests.post(endpoint) |
| 92 | return response |
| 93 | |
| 94 | def get_exotel_endpoint(action): |
| 95 | settings = get_exotel_settings() |
| 96 | return 'https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}'.format( |
| 97 | api_key=settings.api_key, |
| 98 | api_token=settings.api_token, |
| 99 | sid=settings.account_sid, |
| 100 | action=action |
| 101 | ) |