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