feat: Add get_call status & make a call function

This commit is contained in:
Suraj Shetty 2019-05-22 06:37:43 +05:30
parent 57bab84198
commit 1eeb89fb77
2 changed files with 52 additions and 13 deletions

View File

@ -16,6 +16,6 @@ class ExotelSettings(Document):
def verify_credentials(self): def verify_credentials(self):
if self.enabled: if self.enabled:
response = requests.get('https://api.exotel.com/v1/Accounts/{sid}' response = requests.get('https://api.exotel.com/v1/Accounts/{sid}'
.format(sid = self.account_sid), auth=(self.account_sid, self.api_token)) .format(sid = self.account_sid), auth=(self.api_key, self.api_token))
if response.status_code != 200: if response.status_code != 200:
frappe.throw(_("Invalid credentials")) frappe.throw(_("Invalid credentials"))

View File

@ -1,21 +1,24 @@
import frappe import frappe
from erpnext.crm.doctype.utils import get_document_with_phone_number from erpnext.crm.doctype.utils import get_document_with_phone_number
import requests
# api/method/erpnext.erpnext_integrations.exotel_integration.handle_incoming_call
@frappe.whitelist(allow_guest=True) @frappe.whitelist(allow_guest=True)
def handle_incoming_call(*args, **kwargs): def handle_incoming_call(*args, **kwargs):
incoming_phone_number = kwargs.get('CallFrom') # incoming_phone_number = kwargs.get('CallFrom')
contact = get_document_with_phone_number(incoming_phone_number) # contact = get_document_with_phone_number(incoming_phone_number)
last_communication = get_last_communication(incoming_phone_number, contact) # last_communication = get_last_communication(incoming_phone_number, contact)
call_log = create_call_log(kwargs) call_log = create_call_log(kwargs)
data = { data = frappe._dict({
'contact': contact, 'call_from': kwargs.get('CallFrom'),
'call_payload': kwargs, 'agent_email': kwargs.get('AgentEmail'),
'last_communication': last_communication, 'call_type': kwargs.get('Direction'),
'call_log': call_log 'call_log': call_log
} })
frappe.publish_realtime('incoming_call', data) frappe.publish_realtime('show_call_popup', data, user=data.agent_email)
def get_last_communication(phone_number, contact): def get_last_communication(phone_number, contact):
@ -23,6 +26,17 @@ def get_last_communication(phone_number, contact):
return {} return {}
def create_call_log(call_payload): def create_call_log(call_payload):
communication = frappe.get_all('Communication', {
'communication_medium': 'Phone',
'call_id': call_payload.get('CallSid'),
}, limit=1)
if communication:
log = frappe.get_doc('Communication', communication[0].name)
log.call_status = 'Connected'
log.save(ignore_permissions=True)
return log
communication = frappe.new_doc('Communication') communication = frappe.new_doc('Communication')
communication.subject = frappe._('Call from {}').format(call_payload.get("CallFrom")) communication.subject = frappe._('Call from {}').format(call_payload.get("CallFrom"))
communication.communication_medium = 'Phone' communication.communication_medium = 'Phone'
@ -33,9 +47,34 @@ def create_call_log(call_payload):
communication.status = 'Open' communication.status = 'Open'
communication.sent_or_received = 'Received' communication.sent_or_received = 'Received'
communication.content = 'call_payload' communication.content = 'call_payload'
communication.call_status = 'Incoming'
communication.communication_date = call_payload.get('StartTime') communication.communication_date = call_payload.get('StartTime')
# communication.sid = call_payload.get('CallSid') communication.call_id = call_payload.get('CallSid')
# communication.exophone = call_payload.get('CallTo')
# communication.call_receiver = call_payload.get('DialWhomNumber')
communication.save(ignore_permissions=True) communication.save(ignore_permissions=True)
return communication return communication
def get_call_status(call_id):
settings = get_exotel_settings()
response = requests.get('https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/erpnext/{sid}/{call_id}.json'.format(
api_key=settings.api_key,
api_token=settings.api_token,
call_id=call_id
))
return response.json()
@frappe.whitelist(allow_guest=True)
def make_a_call(from_number, to_number, caller_id):
settings = get_exotel_settings()
response = requests.post('https://{api_key}:{api_token}@api.exotel.com/v1/Accounts/{sid}/Calls/connect.json'.format(
api_key=settings.api_key,
api_token=settings.api_token,
), data={
'From': from_number,
'To': to_number,
'CallerId': caller_id
})
return response.json()
def get_exotel_settings():
return frappe.get_single('Exotel Settings')