Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 1 | import frappe |
Suraj Shetty | 87645e9 | 2019-06-06 11:24:31 +0530 | [diff] [blame] | 2 | from erpnext.crm.doctype.utils import get_document_with_phone_number, get_employee_emails_for_popup |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 3 | import requests |
| 4 | |
| 5 | # api/method/erpnext.erpnext_integrations.exotel_integration.handle_incoming_call |
Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 6 | |
| 7 | @frappe.whitelist(allow_guest=True) |
| 8 | def handle_incoming_call(*args, **kwargs): |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 9 | exotel_settings = get_exotel_settings() |
| 10 | if not exotel_settings.enabled: return |
Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 11 | |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 12 | status = kwargs.get('Status') |
| 13 | |
Suraj Shetty | 39a4d59 | 2019-05-27 10:38:43 +0530 | [diff] [blame] | 14 | if status == 'free': |
| 15 | # call disconnected for agent |
| 16 | # "and get_call_status(kwargs.get('CallSid')) in ['in-progress']" - additional check to ensure if the call was redirected |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 17 | return |
| 18 | |
| 19 | call_log = get_call_log(kwargs) |
| 20 | |
Suraj Shetty | 87645e9 | 2019-06-06 11:24:31 +0530 | [diff] [blame] | 21 | employee_emails = get_employee_emails_for_popup() |
| 22 | for email in employee_emails: |
| 23 | frappe.publish_realtime('show_call_popup', call_log, user=email) |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 24 | |
| 25 | @frappe.whitelist(allow_guest=True) |
| 26 | def handle_end_call(*args, **kwargs): |
Suraj Shetty | 87645e9 | 2019-06-06 11:24:31 +0530 | [diff] [blame] | 27 | frappe.publish_realtime('call_disconnected', data=kwargs.get('CallSid')) |
| 28 | update_call_log(kwargs, 'Completed') |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 29 | |
| 30 | @frappe.whitelist(allow_guest=True) |
| 31 | def handle_missed_call(*args, **kwargs): |
Suraj Shetty | 87645e9 | 2019-06-06 11:24:31 +0530 | [diff] [blame] | 32 | frappe.publish_realtime('call_disconnected', data=kwargs.get('CallSid')) |
| 33 | update_call_log(kwargs, 'Missed') |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 34 | |
Suraj Shetty | 87645e9 | 2019-06-06 11:24:31 +0530 | [diff] [blame] | 35 | def update_call_log(call_payload, status): |
| 36 | call_log = get_call_log(call_payload, False) |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 37 | if call_log: |
Suraj Shetty | 87645e9 | 2019-06-06 11:24:31 +0530 | [diff] [blame] | 38 | call_log.call_status = status |
| 39 | call_log.call_duration = call_payload.get('DialCallDuration') or 0 |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 40 | call_log.save(ignore_permissions=True) |
| 41 | frappe.db.commit() |
Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 42 | |
Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 43 | |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 44 | def get_call_log(call_payload, create_new_if_not_found=True): |
Suraj Shetty | 44c0e9d | 2019-06-06 11:18:16 +0530 | [diff] [blame] | 45 | call_log = frappe.get_all('Call Log', { |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 46 | 'call_id': call_payload.get('CallSid'), |
| 47 | }, limit=1) |
| 48 | |
Suraj Shetty | 44c0e9d | 2019-06-06 11:18:16 +0530 | [diff] [blame] | 49 | if call_log: |
| 50 | return frappe.get_doc('Call Log', call_log[0].name) |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 51 | elif create_new_if_not_found: |
Suraj Shetty | 44c0e9d | 2019-06-06 11:18:16 +0530 | [diff] [blame] | 52 | call_log = frappe.new_doc('Call Log') |
| 53 | call_log.call_id = call_payload.get('CallSid') |
| 54 | call_log.call_from = call_payload.get('CallFrom') |
| 55 | call_log.status = 'Ringing' |
| 56 | call_log.save(ignore_permissions=True) |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 57 | frappe.db.commit() |
Suraj Shetty | 44c0e9d | 2019-06-06 11:18:16 +0530 | [diff] [blame] | 58 | return call_log |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 59 | |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 60 | @frappe.whitelist() |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 61 | def get_call_status(call_id): |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 62 | print(call_id) |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 63 | settings = get_exotel_settings() |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 64 | response = requests.get('https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/erpnext/Calls/{call_id}.json'.format( |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 65 | api_key=settings.api_key, |
| 66 | api_token=settings.api_token, |
| 67 | call_id=call_id |
| 68 | )) |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 69 | status = response.json().get('Call', {}).get('Status') |
| 70 | return status |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 71 | |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 72 | @frappe.whitelist() |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 73 | def make_a_call(from_number, to_number, caller_id): |
| 74 | settings = get_exotel_settings() |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 75 | response = requests.post('https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/Calls/connect.json?details=true'.format( |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 76 | api_key=settings.api_key, |
| 77 | api_token=settings.api_token, |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 78 | sid=settings.account_sid |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 79 | ), data={ |
| 80 | 'From': from_number, |
| 81 | 'To': to_number, |
| 82 | 'CallerId': caller_id |
| 83 | }) |
| 84 | |
| 85 | return response.json() |
| 86 | |
| 87 | def get_exotel_settings(): |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 88 | return frappe.get_single('Exotel Settings') |
| 89 | |
| 90 | @frappe.whitelist(allow_guest=True) |
| 91 | def get_phone_numbers(): |
| 92 | numbers = 'some number' |
| 93 | whitelist_numbers(numbers, 'for number') |
| 94 | return numbers |
| 95 | |
| 96 | def whitelist_numbers(numbers, caller_id): |
| 97 | settings = get_exotel_settings() |
| 98 | query = 'https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/CustomerWhitelist'.format( |
| 99 | api_key=settings.api_key, |
| 100 | api_token=settings.api_token, |
| 101 | sid=settings.account_sid |
| 102 | ) |
| 103 | response = requests.post(query, data={ |
| 104 | 'VirtualNumber': caller_id, |
| 105 | 'Number': numbers, |
| 106 | }) |
| 107 | |
| 108 | return response |