Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

Twilio - How to check if the gathered speech results having expected values if not mark it as failed

Need help in validation of the speechresults: I am trying to validating if we are getting correct speech results in the twiml function. I am able to fetch the speech results and perform certain action on them.. but I want to fail my test when it is not matching. How to achieve this.. Thanks in advance. My sample code

exports.handler = function(context, event, callback) 
{
     let twiml = new Twilio.twiml.VoiceResponse();
     console.log("Event = " + JSON.stringify(event));
     let ivrSpeech = (event.SpeechResult ? event.SpeechResult : "").toLowerCase();
     let servicetype = event.servicetype;
     console.log("Call type" + servicetype);
     let IVRType = event.ivrgoal;
    if (ivrSpeech && ivrSpeech.length > 0)
        {
             if(servicetype.includes("XX")) 
                {
                  if (ivrSpeech.includes("Call may be recorded and monitored for quality purpose"))
                        {
                             twiml.pause({ length: 10 });
                           
                        }
                   else 
                        {
                         XXX - fail my test 
                        }

                 }
         }
   callback(null, twiml)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Twilio developer evangelist here.

First, you have one issue with your code that means you will always fail the check.

When you get the ivrSpeech you call .toLowerCase() on it:

     let ivrSpeech = (event.SpeechResult ? event.SpeechResult : "").toLowerCase();

But then you test against a string with a capital letter in it:

if (ivrSpeech.includes("Call may be recorded and monitored for quality purpose"))

So you should change that to start with.

Otherwise, I am not sure what you mean when you say you want to "fail my test" when it doesn't match. You have the code written out pretty well so far, but I don't quite understand what you want to do in the else condition.

One option is to hang up the call, which you would achieve like this:

    if (ivrSpeech && ivrSpeech.length > 0)
        {
             if(servicetype.includes("XX")) 
                {
                  if (ivrSpeech.includes("call may be recorded and monitored for quality purpose"))
                        {
                             twiml.pause({ length: 10 });
                           
                        }
                   else 
                        {
                             twiml.hangup();
                        }

                 }
         }
   callback(null, twiml)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...