2019-05-21 02:27:06 +00:00
|
|
|
import frappe
|
2019-05-22 01:07:43 +00:00
|
|
|
import requests
|
|
|
|
|
|
|
|
# api/method/erpnext.erpnext_integrations.exotel_integration.handle_incoming_call
|
2019-06-07 04:52:50 +00:00
|
|
|
# api/method/erpnext.erpnext_integrations.exotel_integration.handle_end_call
|
|
|
|
# api/method/erpnext.erpnext_integrations.exotel_integration.handle_missed_call
|
2019-05-21 02:27:06 +00:00
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
2019-07-01 08:58:59 +00:00
|
|
|
def handle_incoming_call(**kwargs):
|
2019-05-22 10:18:57 +00:00
|
|
|
exotel_settings = get_exotel_settings()
|
|
|
|
if not exotel_settings.enabled: return
|
|
|
|
|
2019-07-01 08:58:59 +00:00
|
|
|
call_payload = kwargs
|
|
|
|
status = call_payload.get('Status')
|
2019-05-27 05:08:43 +00:00
|
|
|
if status == 'free':
|
2019-05-22 10:18:57 +00:00
|
|
|
return
|
|
|
|
|
2019-07-01 08:58:59 +00:00
|
|
|
call_log = get_call_log(call_payload)
|
|
|
|
if not call_log:
|
|
|
|
create_call_log(call_payload)
|
2019-05-21 02:27:06 +00:00
|
|
|
|
2019-05-27 10:00:41 +00:00
|
|
|
@frappe.whitelist(allow_guest=True)
|
2019-07-01 08:58:59 +00:00
|
|
|
def handle_end_call(**kwargs):
|
2019-06-17 04:46:38 +00:00
|
|
|
update_call_log(kwargs, 'Completed')
|
2019-06-03 06:57:02 +00:00
|
|
|
|
|
|
|
@frappe.whitelist(allow_guest=True)
|
2019-07-01 08:58:59 +00:00
|
|
|
def handle_missed_call(**kwargs):
|
2019-06-17 04:46:38 +00:00
|
|
|
update_call_log(kwargs, 'Missed')
|
2019-06-03 06:57:02 +00:00
|
|
|
|
2019-06-06 05:54:31 +00:00
|
|
|
def update_call_log(call_payload, status):
|
2019-07-01 08:58:59 +00:00
|
|
|
call_log = get_call_log(call_payload)
|
2019-05-27 10:00:41 +00:00
|
|
|
if call_log:
|
2019-06-07 04:52:50 +00:00
|
|
|
call_log.status = status
|
|
|
|
call_log.duration = call_payload.get('DialCallDuration') or 0
|
2019-06-17 02:36:14 +00:00
|
|
|
call_log.recording_url = call_payload.get('RecordingUrl')
|
2019-05-27 10:00:41 +00:00
|
|
|
call_log.save(ignore_permissions=True)
|
|
|
|
frappe.db.commit()
|
2019-06-07 04:52:50 +00:00
|
|
|
return call_log
|
2019-05-27 10:00:41 +00:00
|
|
|
|
2019-07-01 08:58:59 +00:00
|
|
|
def get_call_log(call_payload):
|
2019-06-06 05:48:16 +00:00
|
|
|
call_log = frappe.get_all('Call Log', {
|
2019-06-07 04:52:50 +00:00
|
|
|
'id': call_payload.get('CallSid'),
|
2019-05-22 01:07:43 +00:00
|
|
|
}, limit=1)
|
|
|
|
|
2019-06-06 05:48:16 +00:00
|
|
|
if call_log:
|
|
|
|
return frappe.get_doc('Call Log', call_log[0].name)
|
2019-05-22 01:07:43 +00:00
|
|
|
|
2019-07-01 08:58:59 +00:00
|
|
|
def create_call_log(call_payload):
|
|
|
|
call_log = frappe.new_doc('Call Log')
|
|
|
|
call_log.id = call_payload.get('CallSid')
|
|
|
|
call_log.to = call_payload.get('CallTo')
|
|
|
|
call_log.medium = call_payload.get('To')
|
|
|
|
call_log.status = 'Ringing'
|
|
|
|
setattr(call_log, 'from', call_payload.get('CallFrom'))
|
|
|
|
call_log.save(ignore_permissions=True)
|
|
|
|
frappe.db.commit()
|
|
|
|
return call_log
|
2019-06-17 04:46:38 +00:00
|
|
|
|
2019-05-22 10:18:57 +00:00
|
|
|
@frappe.whitelist()
|
2019-05-22 01:07:43 +00:00
|
|
|
def get_call_status(call_id):
|
2019-06-13 11:43:54 +00:00
|
|
|
endpoint = get_exotel_endpoint('Calls/{call_id}.json'.format(call_id=call_id))
|
|
|
|
response = requests.get(endpoint)
|
2019-05-22 10:18:57 +00:00
|
|
|
status = response.json().get('Call', {}).get('Status')
|
|
|
|
return status
|
2019-05-22 01:07:43 +00:00
|
|
|
|
2019-05-22 10:18:57 +00:00
|
|
|
@frappe.whitelist()
|
2019-05-22 01:07:43 +00:00
|
|
|
def make_a_call(from_number, to_number, caller_id):
|
2019-06-13 11:43:54 +00:00
|
|
|
endpoint = get_exotel_endpoint('Calls/connect.json?details=true')
|
|
|
|
response = requests.post(endpoint, data={
|
2019-05-22 01:07:43 +00:00
|
|
|
'From': from_number,
|
|
|
|
'To': to_number,
|
|
|
|
'CallerId': caller_id
|
|
|
|
})
|
|
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
def get_exotel_settings():
|
2019-06-03 06:57:02 +00:00
|
|
|
return frappe.get_single('Exotel Settings')
|
|
|
|
|
|
|
|
def whitelist_numbers(numbers, caller_id):
|
2019-06-13 11:43:54 +00:00
|
|
|
endpoint = get_exotel_endpoint('CustomerWhitelist')
|
|
|
|
response = requests.post(endpoint, data={
|
2019-06-03 06:57:02 +00:00
|
|
|
'VirtualNumber': caller_id,
|
|
|
|
'Number': numbers,
|
|
|
|
})
|
|
|
|
|
2019-06-13 11:43:54 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
def get_all_exophones():
|
|
|
|
endpoint = get_exotel_endpoint('IncomingPhoneNumbers')
|
|
|
|
response = requests.post(endpoint)
|
|
|
|
return response
|
|
|
|
|
|
|
|
def get_exotel_endpoint(action):
|
|
|
|
settings = get_exotel_settings()
|
|
|
|
return 'https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/{action}'.format(
|
|
|
|
api_key=settings.api_key,
|
|
|
|
api_token=settings.api_token,
|
|
|
|
sid=settings.account_sid,
|
|
|
|
action=action
|
|
|
|
)
|