fix: update call status in realtime

This commit is contained in:
Shariq Ansari 2024-01-13 13:43:23 +05:30
parent 4325386be9
commit 4b9a31c2a3
2 changed files with 25 additions and 21 deletions

View File

@ -83,7 +83,7 @@ def update_call_log(call_sid, status=None):
call_details = twilio.get_call_info(call_sid) call_details = twilio.get_call_info(call_sid)
call_log = frappe.get_doc("CRM Call Log", call_sid) call_log = frappe.get_doc("CRM Call Log", call_sid)
call_log.status = status or TwilioCallDetails.get_call_status(call_details.status) call_log.status = TwilioCallDetails.get_call_status(status or call_details.status)
call_log.duration = call_details.duration call_log.duration = call_details.duration
call_log.start_time = get_datetime_from_timestamp(call_details.start_time) call_log.start_time = get_datetime_from_timestamp(call_details.start_time)
call_log.end_time = get_datetime_from_timestamp(call_details.end_time) call_log.end_time = get_datetime_from_timestamp(call_details.end_time)
@ -105,6 +105,15 @@ def update_recording_info(**kwargs):
except: except:
frappe.log_error(title=_("Failed to capture Twilio recording")) frappe.log_error(title=_("Failed to capture Twilio recording"))
@frappe.whitelist(allow_guest=True)
def update_call_status_info(**kwargs):
try:
args = frappe._dict(kwargs)
parent_call_sid = args.ParentCallSid
update_call_log(parent_call_sid, status=args.CallStatus)
except:
frappe.log_error(title=_("Failed to update Twilio call status"))
@frappe.whitelist(allow_guest=True) @frappe.whitelist(allow_guest=True)
def get_call_info(**kwargs): def get_call_info(**kwargs):
"""This is a webhook called when the outgoing call status changes. """This is a webhook called when the outgoing call status changes.
@ -163,18 +172,11 @@ def get_lead_or_deal_from_number(call):
return data[0].name if data else None return data[0].name if data else None
doctype = "CRM Lead" doctype = "CRM Lead"
doc = None number = call.get('to') if call.type == 'Outgoing' else call.get('from')
to_number = call.get('to')
from_number = call.get('from') doc = find_record(doctype, number) or None
if call.type == 'Outgoing':
doc = find_record(doctype, to_number)
if not doc: if not doc:
doctype = "CRM Deal" doctype = "CRM Deal"
doc = find_record(doctype, to_number) doc = find_record(doctype, number)
else:
doc = find_record(doctype, from_number)
if not doc:
doctype = "CRM Deal"
doc = find_record(doctype, from_number)
return doc, doctype return doc, doctype

View File

@ -72,8 +72,8 @@ class Twilio:
url_path = "/api/method/crm.twilio.api.update_recording_info" url_path = "/api/method/crm.twilio.api.update_recording_info"
return get_public_url(url_path) return get_public_url(url_path)
def get_call_status_callback_url(self): def get_update_call_status_callback_url(self):
url_path = "/api/method/crm.twilio.api.get_call_info" url_path = "/api/method/crm.twilio.api.update_call_status_info"
return get_public_url(url_path) return get_public_url(url_path)
def generate_twilio_dial_response(self, from_number: str, to_number: str): def generate_twilio_dial_response(self, from_number: str, to_number: str):
@ -89,15 +89,12 @@ class Twilio:
dial.number( dial.number(
to_number, to_number,
status_callback_event='initiated ringing answered completed', status_callback_event='initiated ringing answered completed',
status_callback=self.get_call_status_callback_url(), status_callback=self.get_update_call_status_callback_url(),
status_callback_method='POST' status_callback_method='POST'
) )
resp.append(dial) resp.append(dial)
return resp return resp
def get_call_info(self, call_sid):
return self.twilio_client.calls(call_sid).fetch()
def generate_twilio_client_response(self, client, ring_tone='at'): def generate_twilio_client_response(self, client, ring_tone='at'):
"""Generates voice call instructions to forward the call to agents computer. """Generates voice call instructions to forward the call to agents computer.
""" """
@ -108,7 +105,12 @@ class Twilio:
recording_status_callback=self.get_recording_status_callback_url(), recording_status_callback=self.get_recording_status_callback_url(),
recording_status_callback_event='completed' recording_status_callback_event='completed'
) )
dial.client(client) dial.client(
client,
status_callback_event='initiated ringing answered completed',
status_callback=self.get_update_call_status_callback_url(),
status_callback_method='POST'
)
resp.append(dial) resp.append(dial)
return resp return resp