In this tutorial, we will combine a flow-based bot, and an AWS SDK BaaS to create a bot that can detect sentiment and language, and then respond in the matched language.
Since your bot will use three AWS services, you will need access keys, secrets with access to AWS Comprehend, and AWS Translate. You will also need the region for these resources.
You can then create a secret for your bot and connectors to use.
For this example we will use a classic flow bot.
Bot Designer
menu itemCreate Bot
Classic Flow Bot
Hi, How are you doing?
We will need to build three connectors from blueprints.
It is assumed you only created one secret for all three connectors. If you created more than one, update it in step 2 of each connector creation flow.
Create the three connectors using AWS SDK templates.(Select the secret you created in the first section of this tutorial)
Using the AWS Comprehend Detect Language, create a blueprint named ComprehendDetectLanguage
Request Mapping:{ "Text": {"type": "requestBody","requestBodyPath": "$.Text","inputPath": "$.Text"}}
Using the AWS Comprehend Detect Sentiment Blueprint, create a blueprint named ComprehendDetectSentiment
Using the AWS Translate Blueprint, create a blueprint named Translate
Great! You should now see the connectors in your API connectors List.
Prep your Flow
Open back up your sentiment bot. (Bot Designer -> Bot name)
On the bot detail page click Designer
Delete all the nodes except for the first Dialogue
node
Add in Sentiment Detection
Drop in an input node right after the Dialogue
Add a BaaS Dialogue
Label that node Detect Language
Set it’s Alias to ComprehendDetectLanguage
Configure the BaaS to pick up the message for the user, and assign it to the Text Property expected by the BaaS
This means that msg.payload.user.message
ends up pointing mapping to the Text
property in your BaaS
Save the language value
1. Drop a function node onto the canvas and connect the Language Detect BaaS node to it
2. Name it Save Language
3. Add in code to map the results of the Baas to msg data. `msg.languageCode = msg.baas.ComprehendDetectLanguage.response.Languages[0].LanguageCode
return msg;``
Add in Sentiment Detection.
Repeat the steps above to add in another BaaS then point it to ComprehendDetectSentiment
Configure this node to be named Detect Sentiment
AWS Comprehend Detect Sentiment can have a language specified along with the text to be processed.
1. To use the previously detected sentiment, we map msg.languageCode
to LanguageCode
2. Map msg.payload.user.message
to Text
to sent the user message to comprehend
Your node should now look like
Change the response based on the sentiment.
1. Now add a switch node to the end of your chain.
2. We want the switch node to evaluate a response from the BaaS. In this case: baas.ComprehendDetectSentiment.response.Sentiment
3. We will get back either POSITIVE, NEGATIVE, or NEUTRAL responses.
4. Configure the node to check all rules, then hit done.
Provide a Response and Translate it using AWS Translate
Drop in three BaaS nodes, one for each Sentiment
Configure each BaaS node:
- All of them will have the Alias set to the Translate
Baas
- A string property en
mapped to SourceLanguageCode
- A property msg property msg.languageCode
mapped to TargetLanguageCode
- A string property value for what you want the message to say based on the sentiment.
I am glad you are feeling so great
for POSITIVE
Translate the response to user
1. Drop in a Dialogue Node
2. Name it Response
3. In the message field, change the type to msg
and map the response from the translation BaaS. baas.Translate.response.TranslatedText
4. Connect the output node of your bot to the first dialogue node.
Deploy
to save your changesTest
button