import frappe
import requests

# api/method/erpnext.erpnext_integrations.exotel_integration.handle_incoming_call
# api/method/erpnext.erpnext_integrations.exotel_integration.handle_end_call
# api/method/erpnext.erpnext_integrations.exotel_integration.handle_missed_call

@frappe.whitelist(allow_guest=True)
def handle_incoming_call(*args, **kwargs):
	exotel_settings = get_exotel_settings()
	if not exotel_settings.enabled: return

	status = kwargs.get('Status')
	if status == 'free':
		return

	call_log = get_call_log(kwargs)

@frappe.whitelist(allow_guest=True)
def handle_end_call(*args, **kwargs):
	call_log = update_call_log(kwargs, 'Completed')

@frappe.whitelist(allow_guest=True)
def handle_missed_call(*args, **kwargs):
	call_log = update_call_log(kwargs, 'Missed')
	frappe.publish_realtime('call_disconnected', call_log)

def update_call_log(call_payload, status):
	call_log = get_call_log(call_payload, False)
	if call_log:
		call_log.status = status
		call_log.duration = call_payload.get('DialCallDuration') or 0
		call_log.recording_url = call_payload.get('RecordingUrl')
		call_log.save(ignore_permissions=True)
		frappe.db.commit()
		return call_log


def get_call_log(call_payload, create_new_if_not_found=True):
	call_log = frappe.get_all('Call Log', {
		'id': call_payload.get('CallSid'),
	}, limit=1)

	if call_log:
		return frappe.get_doc('Call Log', call_log[0].name)
	elif create_new_if_not_found:
		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

@frappe.whitelist()
def get_call_status(call_id):
	endpoint = get_exotel_endpoint('Calls/{call_id}.json'.format(call_id=call_id))
	response = requests.get(endpoint)
	status = response.json().get('Call', {}).get('Status')
	return status

@frappe.whitelist()
def make_a_call(from_number, to_number, caller_id):
	endpoint = get_exotel_endpoint('Calls/connect.json?details=true')
	response = requests.post(endpoint, data={
		'From': from_number,
		'To': to_number,
		'CallerId': caller_id
	})

	return response.json()

def get_exotel_settings():
	return frappe.get_single('Exotel Settings')

@frappe.whitelist(allow_guest=True)
def get_phone_numbers():
	numbers = 'some number'
	whitelist_numbers(numbers, 'for number')
	return numbers

def whitelist_numbers(numbers, caller_id):
	endpoint = get_exotel_endpoint('CustomerWhitelist')
	response = requests.post(endpoint, data={
		'VirtualNumber': caller_id,
		'Number': numbers,
	})

	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
	)
@@ -490,29 +490,21 @@
 turn%0A%0A%09c
-all_log = get
+reate
 _call_lo
@@ -585,35 +585,24 @@
 **kwargs):%0A%09
-call_log = 
 update_call_
@@ -705,27 +705,16 @@
 args):%0A%09
-call_log = 
 update_c
@@ -742,64 +742,8 @@
 ed')
-%0A%09frappe.publish_realtime('call_disconnected', call_log)
 %0A%0Ade
@@ -1075,17 +1075,16 @@
 ll_log%0A%0A
-%0A
 def get_
@@ -1640,32 +1640,32 @@
 ppe.db.commit()%0A
-
 %09%09return call_lo
@@ -1667,16 +1667,48 @@
 ll_log%0A%0A
+create_call_log = get_call_log%0A%0A
 @frappe.

Remove unwanted code