Use the AWS SDK to Translate Bots

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.

Set up your Secrets

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.

Build your Initial Bot

For this example we will use a classic flow bot.

  1. Select the Bot Designer menu item
  2. Click Create Bot
  3. From the blueprint options, choose Classic Flow Bot
  4. Name your bot, then set the custom message to Hi, How are you doing?
  5. Build it, we will come back to this bot later

Build your SDK Connectors

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.

  1. Create the three connectors using AWS SDK templates.(Select the secret you created in the first section of this tutorial)

  2. Using the AWS Comprehend Detect Language, create a blueprint named ComprehendDetectLanguage Request Mapping:{ "Text": {"type": "requestBody","requestBodyPath": "$.Text","inputPath": "$.Text"}}

  3. Using the AWS Comprehend Detect Sentiment Blueprint, create a blueprint named ComprehendDetectSentiment

  4. Using the AWS Translate Blueprint, create a blueprint named Translate

  5. Great! You should now see the connectors in your API connectors List.

Connect Everything Together

  1. Prep your Flow

  2. Open back up your sentiment bot. (Bot Designer -> Bot name)

  3. On the bot detail page click Designer

  4. Delete all the nodes except for the first Dialogue node Starting

  5. Add in Sentiment Detection

  6. Drop in an input node right after the Dialogue

  7. Add a BaaS Dialogue

    graph LR; Dialogue[Dialogue] --> Input(Input) Input --> BaaS(Baas)

  8. Label that node Detect Language

  9. Set it’s Alias to ComprehendDetectLanguage

  10. Configure the BaaS to pick up the message for the user, and assign it to the Text Property expected by the BaaS detect-language

  11. This means that msg.payload.user.message ends up pointing mapping to the Text property in your BaaS

  12. 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;``

  13. Add in Sentiment Detection.

  14. Repeat the steps above to add in another BaaS then point it to ComprehendDetectSentiment

  15. Configure this node to be named Detect Sentiment

  16. 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

  17. Your node should now look like detect-language

  18. 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. switch-sentiment 4. Configure the node to check all rules, then hit done.

  19. Provide a Response and Translate it using AWS Translate

  20. Drop in three BaaS nodes, one for each Sentiment

    graph LR; Switch[Switch] --> Positive(Positive) Switch[Switch] --> Negative(Negative) Switch[Switch] --> Neutral(Neutral)

  21. 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.

    • E.g. I am glad you are feeling so great for POSITIVE
  22. 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. end flow

Play and Test

  1. On the Bot Designer Canvas, click Deploy to save your changes
  2. Hit the back button to go to the bot detail page.
  3. At the bottom of the bot detail page, there is a Test button
  4. Once you are on the test page, you can interact with your bot.