Amazon Lex

From VESupport

Jump to: navigation, search

VoiceElements now supports Amazon Lex as of Version 8.6.2.0

What is Amazon Lex?

Amazon Lex is a platform that allows you to build your own conversational interface using the learning technologies of Alexa. VoiceElements allows you to stream the voice input of a call directly into this interface so that your callers can speak directly to the interface. At each step of the conversation Amazon Lex provides information about the conversation directly into your voice application. Perhaps more widely known as ChatBots, this integration will allow you to greatly enhance your customer's experience while using your IVR.

To read up on Amazon Lex click here: [1] and here: [2]

How easy is it to attach a call to Amazon Lex?

The following snippet demonstrates how you can engage Amazon Lex. Responses from Amazon are returned as KeyValuePairs:

     TerminationCode tc = m_VoiceResource.BotEngage(botMode, null, botMode == "ElicitIntent" ? bHello : null);
     if (!m_VoiceResource.TerminationCodeFlag(TerminationCode.Normal)) return;
     if (m_VoiceResource.BotResponse == null) return;
 
     foreach (KeyValuePair<string, object> kvp in m_VoiceResource.BotResponse)
     {
        Log.Write($"key: {kvp.Key} -- value: {kvp.Value}");
     }

How do I set my credentials for Amazon Lex?

The following snippet demonstrates how to prepare for calling a chatbot:

               m_VoiceResource.BotParameters = new Dictionary<string, string>();
               m_VoiceResource.BotParameters.Add("amazonID", "**************");
               m_VoiceResource.BotParameters.Add("amazonPW", "*************************");
               m_VoiceResource.BotParameters.Add("amazonRegion", "USEast1");
               m_VoiceResource.BotParameters.Add("amazonBotAlias", "TestOne");
               m_VoiceResource.BotParameters.Add("amazonBotName", "BookTrip");
               m_VoiceResource.BotParameters.Add("amazonUserId", Guid.NewGuid().ToString());

How can I get started?

Here is a working script that you can add to the Getting started sample that come with VoiceElements:

       public void RunScript()
       {
           try
           {
               // Answer the phone.
               m_ChannelResource.Answer();
 
               Log.Write("Using VR: {0}", m_VoiceResource.DeviceName);
 
               m_VoiceResource.MaximumTime = 30;
 
               bool bError = true;
 
               m_VoiceResource.BotParameters = new Dictionary<string, string>();
               m_VoiceResource.BotParameters.Add("amazonID", "YOURID");
               m_VoiceResource.BotParameters.Add("amazonPW", "YOURPW");
               m_VoiceResource.BotParameters.Add("amazonRegion", "USEast1");
               m_VoiceResource.BotParameters.Add("amazonBotAlias", "TestOne");
               m_VoiceResource.BotParameters.Add("amazonBotName", "BookTrip");
               m_VoiceResource.BotParameters.Add("amazonUserId", Guid.NewGuid().ToString());
 
               // Change this if you want the prompt to play to completion before it starts streaming the users voice
               m_VoiceResource.DetectBargeIn = true;
 
               //string botMode = "ElicitIntent";  // If you set this, then it will hit amazon first to get it's first prompt.
               string botMode = "PlayFile"; // This says to play a file that you have recorded first.
 
               while (true)
               {
 
                   bError = true;
                   TerminationCode tc = m_VoiceResource.BotEngage(botMode, botMode == "PlayFile" ? @"D:\AmazonTest\HowCanIHelpYouToday.wav" : null, null);
                   if (!m_VoiceResource.TerminationCodeFlag(TerminationCode.Normal)) break;
                   if (m_VoiceResource.BotResponse == null) break;
 
                   foreach (KeyValuePair<string, object> kvp in m_VoiceResource.BotResponse)
                   {
                       Log.Write($"key: {kvp.Key} -- value: {kvp.Value}");
                   }
 
                   string dialogState = m_VoiceResource.BotResponse["DialogState"] as string;
 
                   if (dialogState == null) break;
                   if (dialogState == "Failed") break;
 
                   if (dialogState == "Fulfilled" || dialogState == "ReadyForFulfillment")
                   {
                       bError = false;
                       break;
                   }
 
                   botMode = "PlayBotStream";
 
               }
 
               if (bError)
               {
                   m_VoiceResource.Codec = Codec.Automatic;
                   m_VoiceResource.DataFormat = DataFormat.Wave;
                   m_VoiceResource.PlayTTS("Sorry there was an error.");
               }
               else
               {
                   m_VoiceResource.Codec = Codec.Automatic;
                   m_VoiceResource.DataFormat = DataFormat.Wave;
                   m_VoiceResource.PlayTTS("Gotcha Covered.");
               }
 
               m_VoiceResource.PlayTTS("Goodbye");
               return;
 
 
           }
           catch (ElementsException ee)
           {
               // These are Telephony Specific exceptions, such an the caller hanging up the phone during a play or record.
               if (ee is HangupException)
                   Log.Write("The Caller Hungup!");
               else
                   Log.WriteException(ee, "Script Elements Exception");
           }
           catch (Exception ex)
           {
               // This would be a general logic exception, such as a null reference violation or other .NET exception.
               Log.WriteException(ex, "InboundIVR Exception");
           }
           finally
           {
 
               // Disconnect the call (I.E. Hangup)
               m_ChannelResource.Disconnect();
 
               // Dispose of the channel resource, (this will dispose of its attached voice resource automatically)
               m_ChannelResource.Dispose();
 
               // Set these values to null so they cant be referenced anymore.  Once the call is disposed, the resources cannot be utilized.
               m_ChannelResource = null;
               m_VoiceResource = null;
               m_TelephonyServer = null;
           }
       }


Preparing your HmpElements Server

You will need to have HmpElements Server Version 10.2.2.0 You will need to add the following settings to the HMPElementsServer.exe.config file:


           <setting name="AmazonInterfacePortCount" serializeAs="String">
               <value>0</value>
           </setting>
           <setting name="AmazonInterfaceRecordingPath" serializeAs="String">
               <value />
           </setting>

The Port Count is the number of simultaneous operations you expect to have. 4 incoming calls = 4 AmazonInterfacePortCount. You could do more if you wanted more in the queue.

The recording path is to capture the audio of what is being sent to Amazon. It writes a RAW PCM 8000Hz, Mono, 16bit file. You can use Goldwave or your favorite audio editor to listen to them. There will be one file per utterance. So only turn this on if you need to. (You can change this on the fly, without needing to restart HMPElements).

Other Notes

You can ask for the stream that amazon received to be returned. Just set this:

               m_VoiceResource.BotParameters.Add("hmpSendBackStream", "true");

Setting this will return the stream in the m_VoiceResource.BotResponse collection as a MemoryStream. The Key Value is "SpokenStream".

You can now use a BotMode of "ElicitIntent" and pass a byte[] of the data you want to send to Amazon. This must be RAW (IE no wave headers), and in PCM, 16Bit, 8000Hz, Mono. For example this can be used up front with the first call to BotEngage and causes the BOT to say, "I can help you reserve a car or book a hotel. What would you like to do?"

To load a recording from a file do the following:

               FileStream fs = new FileStream(@"D:\Monroe\AmazonTest\Hello.raw", FileMode.Open, FileAccess.Read, FileShare.Read);
               MemoryStream ms = new MemoryStream();
               using (fs)
               {
                   fs.CopyTo(ms);
               }
 
               ms.Position = 0;
               byte[] bHello = ms.ToArray();

And use something like this to pass in the the stream:

     TerminationCode tc = m_VoiceResource.BotEngage(botMode, null, botMode == "ElicitIntent" ? bHello : null);

Bot Modes

Valid BotModes are: ElicitIntent, PlayBotStream, PlayFile

ElicitIntent engages the BOT and lets it determine what the correct intent is for the call. PlayBotStream moves forward through the conversation. PlayFile plays a file and then passes control to the Bot.

When using ElicitIntent, you can supply a file to send to amazon and they will play it as part of the initial conversation.

Please contact support if you have any questions. We can be reached via email at support@inventivelabs.com

Personal tools