a

Issues Bridging Twilio Calls between Customer and Agent

We are seeing issues bridging Twilio calls between the customer and agent with IVR, whisper, wait music, and call recording. Scenario: We have clients calling in. They enter an id to the Twilio based IVR. We play wait music. We call the agent. When the agent picks up the phone, we play the whisper of […]

We are seeing issues bridging Twilio calls between the customer and agent with IVR, whisper, wait music, and call recording.

Scenario:
We have clients calling in. They enter an id to the Twilio based IVR. We play wait music. We call the agent. When the agent picks up the phone, we play the whisper of the ID. We give the agent the opportunity to look up the number. We then bridge the call and launch call recording.
Issue:
In certain situations, we lose the inbound call. We’re not entirely sure why. We don’t see any errors.
Call Flow:
1. Calls the Agent. Enqueue’s the current customer call.
File: CallAgentService.java

    public Verb callAgent(ServletRequest req, Category twilioSettings, JSONObject member) throws Exception {
        String numberCalled = getNumberCalled(req, member);
        String callSid = req.getParameter(Constants.TWILIO_PARAM_CALLSID);
        CallStatus status = callStatusRepository.findByCode(Constants.CALL_STATUS_KNOWN_ID);
        String digits = req.getParameter(Constants.TWILIO_PARAM_DIGITS);
        if(StringUtils.isNotBlank(digits)) {
            TwilioConfigUtils.saveCallStat(callLogStatRepository, callSid, status, digits);
        } else {
            TwilioConfigUtils.saveCallStat(callLogStatRepository, callSid, status);
        }
        CallLog callLog = callLogRepository.findOne(callSid);
        saveCallLog(callLog, member);
        Enqueue enqueue = new Enqueue(callSid);
        enqueue.setWaitUrl(Constants.ENQUEUE_WAIT_URL);
        enqueue.setWaitUrlMethod(HttpMethod.GET.toString());
        Call call = TwilioConfigUtils.callAgent(numberCalled, twilioSettings, env);
        if (call != null) {
            TwilioConfigUtils.saveNewCall(call, callLog, callLogRepository);
            status = callStatusRepository.findByCode(Constants.CALL_STATUS_AGENT_START);
            TwilioConfigUtils.saveCallStat(callLogStatRepository, callSid, status);
            return enqueue;
        } else {
            Say sayError = new Say("We are sorry an error has occurred.");
            sayError.setLanguage(Constants.SPOKEN_LANGUAGE);
            return sayError;
        }
    }

2. Creates the outbound call to the agent with the whisper.
File: TwilioConfigUtils.java

    public static Call callAgent(String called, Category twilioSettings, Environment env) {
        log.debug("Calling agent");
        try {
            String acctSid = getTwilioConfigByCode(twilioSettings, Constants.TWILIO_ACCOUNT_SID);
            String authToken = getTwilioConfigByCode(twilioSettings, Constants.TWILIO_AUTH_TOKEN);
            String phoneNumber = getTwilioConfigByCode(twilioSettings, Constants.TWILIO_AGENT_PHONE);
            TwilioRestClient client = new TwilioRestClient(acctSid, authToken);
            // Build a filter for the CallList
            List params = new ArrayList<>();
            String localhost = env.getProperty("rest.main.url");
            params.add(new BasicNameValuePair("Url", localhost + Constants.WHISPER_AGENT));
            params.add(new BasicNameValuePair("To", phoneNumber));
            params.add(new BasicNameValuePair("From", called));
            params.add(new BasicNameValuePair("Method", HttpMethod.GET.toString()));
            params.add(new BasicNameValuePair("Record", Boolean.TRUE.toString()));
            params.add(new BasicNameValuePair("StatusCallback", localhost + Constants.AUDIO_RECORD_CALLBACK));
            params.add(new BasicNameValuePair("StatusCallbackMethod", HttpMethod.GET.toString()));
            CallFactory callFactory = client.getAccount().getCallFactory();
            Call call = callFactory.create(params);
            return call;
        } catch (Exception e) {
            log.error(e);
        } finally {
            log.debug("Agent called");
        }
        return null;
    }


Agent Call on Twilio Logs

Twilio Log - Outbound Calls to Agent
Twilio Log - Outbound Calls to Agent


3. Whispers to the Agent
File: WhisperAgentServlet.java

 @Override
    public void service(ServletRequest req, ServletResponse response) throws ServletException, IOException {
        log.debug("Entering WhisperAgentServlet");
        String callSid = req.getParameter(Constants.TWILIO_PARAM_CALLSID);
        CallLog callLog = callLogRepository.findOne(callSid);
        CallStatus status = callStatusRepository.findByCode(Constants.CALL_STATUS_AGENT_CONNECT);
        TwilioConfigUtils.saveCallStat(callLogStatRepository, callLog.getParentCallSid(), status);
        TwiMLResponse twiml = new TwiMLResponse();
        Long clientId = callLog.getClientId();
        try {
            if (callLog == null) {
                log.debug(twiml.toXML());
                response.setContentType(Constants.CONTENT_TYPE_XML);
                response.getWriter().print(twiml.toXML());
                return;
            }
            Category twilioSettings = categoryRepository.findCategoryByCodeAndClientId(Constants.TWILIO_SETTINGS, clientId);
            String msg;
            if (StringUtils.isNotBlank(callLog.getMemberName()) && StringUtils.isNotBlank(callLog.getMemberLastName())) {
                msg = TwilioConfigUtils.getTwilioConfigByCode(twilioSettings, Constants.TWILIO_WHISPER_AGENT_MENU_MEMBER_INFO);
                Map<String, String> map = new HashMap<>();
                map.put(Constants.TWILIO_MSG_VAR_FIRSTNAME, callLog.getMemberName());
                map.put(Constants.TWILIO_MSG_VAR_LASTNAME, callLog.getMemberLastName());
                String memberId = callLog.getCsMemberId();
                if (StringUtils.isBlank(memberId)) {
                    memberId = callLog.getClientMemberId();
                } else {
                    memberId = "1111" + StringUtils.leftPad(memberId, 8, '0');
                }
                StringBuilder sbMemberId = new StringBuilder();
                for (char c : memberId.toCharArray()) {
                    sbMemberId.append(c).append(", ");
                }
                map.put(Constants.TWILIO_MSG_VAR_MEMBER_ID, sbMemberId.toString().trim());
                msg = StrSubstitutor.replace(msg, map);
            } else {
                msg = TwilioConfigUtils.getTwilioConfigByCode(twilioSettings, Constants.TWILIO_WHISPER_AGENT_MENU_NO_MEMBER_INFO);
            }
            Say incomingCall = new Say(msg);
            incomingCall.setLanguage(Constants.SPOKEN_LANGUAGE);
            Dial dial = new Dial();
            Queue queue = new Queue(callLog.getParentCallSid());
            dial.append(queue);
            twiml.append(incomingCall);
            twiml.append(dial);
        } catch (TwiMLException e) {
            log.error(e);
            Say error = new Say("We are sorry an error has occurred while connecting.");
            error.setLanguage(Constants.SPOKEN_LANGUAGE);
            try {
                twiml = new TwiMLResponse();
                twiml.append(error);
            } catch (TwiMLException e1) {
                log.error(e1);
            }
            callLog = callLogRepository.findOne(callLog.getParentCallSid());
            Category twilioSettings = categoryRepository.findCategoryByCodeAndClientId(Constants.TWILIO_SETTINGS, clientId);
            TwilioConfigUtils.releaseQueueWithError(callLog, twilioSettings, env);
        }
        log.debug(twiml.toXML());
        response.setContentType(Constants.CONTENT_TYPE_XML);
        response.getWriter().print(twiml.toXML());
    }

Customer Call on Twilio Logs
Where does the Hangup event come from? When we do our tests we are still on the call?!??!

Twilio Log of Inbound Client Call with IVR
Twilio Log of Inbound Client Call with IVR
Joel Garcia
Joel Garcia

Joel Garcia has been building AllCode since 2015. He’s an innovative, hands-on executive with a proven record of designing, developing, and operating Software-as-a-Service (SaaS), mobile, and desktop solutions. Joel has expertise in HealthTech, VoIP, and cloud-based solutions. Joel has experience scaling multiple start-ups for successful exits to IMS Health and Golden Gate Capital, as well as working at mature, industry-leading software companies. He’s held executive engineering positions in San Francisco at TidalWave, LittleCast, Self Health Network, LiveVox acquired by Golden Gate Capital, and Med-Vantage acquired by IMS Health.

Related Articles

Navigating AWS Complexity

Navigating AWS Complexity

Amazon’s Web Services is a very complex platform. Streamlining and optimizing production workflows can be challenging for inexperienced users. However, the benefit of learning grants options for better efficiency, reliability, security, and cost-effectiveness for operations run on AWS.

While complexity can be difficult to navigate, it’s not impossible. With the right level of expertise, AWS complexity can be navigated with ease.

Generative AI and its Applications

Generative AI and its Applications

Generative AI has become increasingly popular within the last few years because of how it redefines content creation. With user input, it can automatically put out media that corresponds with and imitates what is provided to it. Despite the novelty of this technology and the potential security or ethical concerns, it’s a technology that shows promise.

What is Amazon Managed Grafana?

What is Amazon Managed Grafana?

Grafana stands out as a widely embraced open-source analytics and visualization platform, celebrated for its versatility in handling diverse data sources and delivering compelling dashboards and graphs. Renowned for its user-friendly interface, Grafana simplifies the process of data interpretation and enhances the overall experience by providing interactive visualizations.

Download our 10-Step Cloud Migration ChecklistYou'll get direct access to our full-length guide on Google Docs. From here, you will be able to make a copy, download the content, and share it with your team.