Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 1 | import frappe |
| 2 | from erpnext.crm.doctype.utils import get_document_with_phone_number |
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 | employee_email = kwargs.get('AgentEmail') |
| 13 | status = kwargs.get('Status') |
| 14 | |
Suraj Shetty | 39a4d59 | 2019-05-27 10:38:43 +0530 | [diff] [blame] | 15 | if status == 'free': |
| 16 | # call disconnected for agent |
| 17 | # "and get_call_status(kwargs.get('CallSid')) in ['in-progress']" - additional check to ensure if the call was redirected |
| 18 | frappe.publish_realtime('call_disconnected', user=employee_email) |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 19 | return |
| 20 | |
| 21 | call_log = get_call_log(kwargs) |
| 22 | |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 23 | data = frappe._dict({ |
| 24 | 'call_from': kwargs.get('CallFrom'), |
| 25 | 'agent_email': kwargs.get('AgentEmail'), |
| 26 | 'call_type': kwargs.get('Direction'), |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 27 | 'call_log': call_log, |
| 28 | 'call_status_method': 'erpnext.erpnext_integrations.exotel_integration.get_call_status' |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 29 | }) |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 30 | |
| 31 | frappe.publish_realtime('show_call_popup', data, user=data.agent_email) |
| 32 | |
| 33 | @frappe.whitelist(allow_guest=True) |
| 34 | def handle_end_call(*args, **kwargs): |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 35 | close_call_log(kwargs) |
| 36 | |
| 37 | @frappe.whitelist(allow_guest=True) |
| 38 | def handle_missed_call(*args, **kwargs): |
| 39 | close_call_log(kwargs) |
| 40 | |
| 41 | def close_call_log(call_payload): |
| 42 | call_log = get_call_log(call_payload) |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 43 | if call_log: |
| 44 | call_log.status = 'Closed' |
| 45 | call_log.save(ignore_permissions=True) |
| 46 | frappe.db.commit() |
Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 47 | |
Suraj Shetty | 863b93c | 2019-05-21 07:57:06 +0530 | [diff] [blame] | 48 | |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 49 | def get_call_log(call_payload, create_new_if_not_found=True): |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 50 | communication = frappe.get_all('Communication', { |
| 51 | 'communication_medium': 'Phone', |
| 52 | 'call_id': call_payload.get('CallSid'), |
| 53 | }, limit=1) |
| 54 | |
| 55 | if communication: |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 56 | communication = frappe.get_doc('Communication', communication[0].name) |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 57 | return communication |
| 58 | elif create_new_if_not_found: |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 59 | communication = frappe.new_doc('Communication') |
| 60 | communication.subject = frappe._('Call from {}').format(call_payload.get("CallFrom")) |
| 61 | communication.communication_medium = 'Phone' |
| 62 | communication.phone_no = call_payload.get("CallFrom") |
| 63 | communication.comment_type = 'Info' |
| 64 | communication.communication_type = 'Communication' |
| 65 | communication.sent_or_received = 'Received' |
| 66 | communication.communication_date = call_payload.get('StartTime') |
| 67 | communication.call_id = call_payload.get('CallSid') |
Suraj Shetty | bd03a51 | 2019-05-27 15:30:41 +0530 | [diff] [blame] | 68 | communication.status = 'Open' |
| 69 | communication.content = frappe._('Call from {}').format(call_payload.get("CallFrom")) |
| 70 | communication.save(ignore_permissions=True) |
| 71 | frappe.db.commit() |
| 72 | return communication |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 73 | |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 74 | @frappe.whitelist() |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 75 | def get_call_status(call_id): |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 76 | print(call_id) |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 77 | settings = get_exotel_settings() |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 78 | 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] | 79 | api_key=settings.api_key, |
| 80 | api_token=settings.api_token, |
| 81 | call_id=call_id |
| 82 | )) |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 83 | status = response.json().get('Call', {}).get('Status') |
| 84 | return status |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +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 make_a_call(from_number, to_number, caller_id): |
| 88 | settings = get_exotel_settings() |
Suraj Shetty | 07fe299 | 2019-05-22 15:48:57 +0530 | [diff] [blame] | 89 | 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] | 90 | api_key=settings.api_key, |
| 91 | api_token=settings.api_token, |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 92 | sid=settings.account_sid |
Suraj Shetty | 1eeb89f | 2019-05-22 06:37:43 +0530 | [diff] [blame] | 93 | ), data={ |
| 94 | 'From': from_number, |
| 95 | 'To': to_number, |
| 96 | 'CallerId': caller_id |
| 97 | }) |
| 98 | |
| 99 | return response.json() |
| 100 | |
| 101 | def get_exotel_settings(): |
Suraj Shetty | e9bfecf | 2019-06-03 12:27:02 +0530 | [diff] [blame] | 102 | return frappe.get_single('Exotel Settings') |
| 103 | |
| 104 | @frappe.whitelist(allow_guest=True) |
| 105 | def get_phone_numbers(): |
| 106 | numbers = 'some number' |
| 107 | whitelist_numbers(numbers, 'for number') |
| 108 | return numbers |
| 109 | |
| 110 | def whitelist_numbers(numbers, caller_id): |
| 111 | settings = get_exotel_settings() |
| 112 | query = 'https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/CustomerWhitelist'.format( |
| 113 | api_key=settings.api_key, |
| 114 | api_token=settings.api_token, |
| 115 | sid=settings.account_sid |
| 116 | ) |
| 117 | response = requests.post(query, data={ |
| 118 | 'VirtualNumber': caller_id, |
| 119 | 'Number': numbers, |
| 120 | }) |
| 121 | |
| 122 | return response |