Use the AWS SDK to Translate Bots

In this tutorial we will combine a flow-based bot, and a few 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 you 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 on 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 secrete you created in the first section of this tutorial)

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

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

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

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

Connect Everything Together

  1. Prep your Flow
    1. Open back up your sentiment bot. (Bot Designer -> Bot name)
    2. On the bot detail page click Designer
    3. Delete all the nodes except for the first Dialogue node Starting
  2. Add in Sentiment Detection

    1. Drop in an input node right after the Dialogue
    2. Add a BaaS Dialogue
      graph LR; Dialogue[Dialogue] --> Input(Input) Input --> BaaS(Baas)
    3. Label that node Detect Language
    4. Set it’s Alias to ComprehendDetectLanguage
    5. Configure the BaaS to pick up the message for the user, and assign it to the Text Property expected by the BaaS detect-language
    6. This means that msg.payload.user.message ends up pointing mapping to to the Text property in your baas
    7. Save the language value
    8. Drop a function node onto the canvas and connect the Language Detect BaaS node to it
    9. Name it Save Language
    10. Add in code to map the results of the Baas to msg data. msg.languageCode = msg.baas.ComprehendDetectLanguage.response.Languages[0].LanguageCode return msg;`
  3. Add in Sentiment Detection.

    1. Repeat the steps above to add in another BaaS, and point it to ComprehendDetectSentiment
    2. Configure this node to be named Detect Sentiment
    3. AWS Comprehend Detect Sentiment can have a language specified along with the text to be processed.
    4. To use the previously detected sentiment, we map msg.languageCode to LanguageCode
    5. Map msg.payload.user.message to Text to sent the user message to comprehend
    6. Your node should now look like detect-language
    7. Change the response based on the sentiment.
    8. Now add a switch node to the end of your chain.
    9. We want the switch node to evaluate a response from the BaaS. In this case: baas.ComprehendDetectSentiment.response.Sentiment
    10. We will get back either POSITIVE, NEGATIVE, or NEUTRAL responses. switch-sentiment
    11. Configure the node to check all rules, then hit done.
  4. Provide a Response and Translate it using AWS Translate

    1. Drop in three BaaS nodes, one for each Sentiment
      graph LR; Switch[Switch] --> Positive(Positive) Switch[Switch] --> Negative(Negative) Switch[Switch] --> Neutral(Neutral)
    2. Configure each BaaS node:
    3. All of them will have the Alias set to the Translate Baas
    4. A string property en mapped to SourceLanguageCode
    5. A property msg property msg.languageCode mapped to TargetLanguageCode
    6. 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
    7. Translate the response to user
    8. Drop in a Dialogue Node
    9. Name it Response
    10. In the message field, change the type to msg and map the response from the translation BaaS. baas.Translate.response.TranslatedText
    11. 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.