To hear prompts in Spanish press 1.
"To hear prompts in Spanish press 1."
On the surface this a fairly simple and common prompt. But when you start to think about how to implement this it gets a little more complicated. There are two responses to this prompt. The user can press 1 or simply do nothing. If the user presses 1 then they should hear the prompts in Spanish but if they do nothing then things should proceed without any user input.
Normally a QuestionAnswer activity has a grammar that expects the user to choose something but the prompt above allows the user to do nothing as one of the responses. If the user does nothing (i.e. silence) then the normal response for a QuestionAnswer activity is to play the silence prompt and wait for a key press.
So how do you handle a prompt doesn’t always require input? Well I’m going to show you one way of doing it. Note that this approach also works for a barge in prompt where you want to give the user the ability to skip a prompt.
First you need to create a new speech application and include a grxml grammar. In the grammar setup a DTMF rule that only accepts a “1” key press.
Drop a SpeechSequence into the designer then add a QuestionAnswer activity and name them using whatever naming conventions you have. Add text to the QA either using the Property Builder or in the TurnStarting event then attach the DTMF grammar. You now have the basics down and the QA will work if the user presses the “1” key. But how do you handle the “do nothing” choice? To handle that we will need to add some more stuff.
Next add an IfElse with two branches.
Then right click on the SpeechSequence and choose “View SpeechEvents” as shown below.
In the event handler add a ConsecutiveNoInputsSpeechEvent and set MaximumNoInputs to 1. By using a ConsecutiveNoInputsSpeechEvent the event will fire as soon as we get a silence or a noreco on the QA. This will catch the case where the user chooses to “do nothing” or presses any key other than “1”. This doesn’t override the normal behavior of the QA which is to replay the prompts so we need some way of getting out of the QA and we can do this by dropping a GoTo into the handler and setting its target to the IfElse we created earlier. This will get us out of the QA and allow the workflow to continue. Your handler should look like this:

Once we are out of the handler we need to check to see if the user pressed “1” and we do that in the IfElse we created earlier. Here we encounter a slight problem. The normal way an IfElse works is that it checks the branches from left to right until it finds a matching condition, hits the Else branch or runs out of branches. We can’t just use the normal QuestionAnswer.RecognitionResult logic that we are all so used to. Since we have jumped out of the QA when we had a silence or noreco event the QA’s Recognition object is null and we have to handle that case in the first branch or we risk blowing up our code. You can do that like in the first branch conditional code this:
private void ifSpanishNoInput(object sender, ConditionalEventArgs e)
{
if (askSpanish.RecognitionResult == null)
e.Result = true;
else
e.Result = false;
}
From here it is simple: All you need to do is add a code block to the Else branch and put this code in the _ExecuteEvent
this.TelephonySession.CurrentUICulture = new CultureInfo("es-US");
That’s all there is to it. The QA will now allow the caller to either press the “1” key for Spanish or simply do nothing and the call will continue after setting the TTS to Spanish. You will also need to set your prompts to Spanish but I’ll leave that exercise for you. I will give you a hint though – I use AppendAudio and .wav files for most of my stuff so I just switch from an English directory of .wav files to the Spanish directory and append the prompts from there.
As I said this technique wil work any time you need a prompt that you want to barge in on or a prompt (QA) that only has one input. I've given you enough inormation so that you can get something like this up and running in your code but if you have any questions just let me know.