In this article, we take a look at how to add AWS Lambda functions to our Amazon Connect Contact Flows. This will be the first in a series of articles where we build our very own “Dial A Joke” Service. That’s right, we’re going back to the ’90s using 2019 technology. Throughout this project, we will be taking a look at:

  • Adding Lambda Functions to Contact Flows for dynamic response generation
  • defining and leveraging call variables
  • Using Lambda responses to drive Contact Flow decision making and conditional logic flows
  • Implementing and triggering outbound calls from Amazon Connect
  • a basic introduction to SSML (Speech Synthesis Markup Language

If you haven’t already taken a look at Amazon Connect, you can take a look at some of our previous articles.

The Design

What we are building is going to be a straightforward service that has three main components. Firstly it will answer the incoming call and start logging the flow into CloudWatch Logs. Then, it will invoke a Lambda function which will return both a joke and a punchline. Finally, it will play both the returned values with a slight pause between them. This pause will give the caller a chance to process the joke before hearing the punchline.

Some might also notice there is another path with a Prompt that will play in the event of an error.

Adding the Lambda Function

So before we go ahead and set up the Contact Flow, we need to create a new Lambda function. The quickest and easiest way to do this is via the Amazon Connect Management Console.

Firstly, head to https://console.aws.amazon.com/connect and select your previously created instance from the list. If you don’t already have an Amazon Connect instance, refer to the articles above for step by step instructions.

Once you’ve got your overview screen up, go ahead a click on “Contact Flow” from the Left Hand Side.

On the Main Panel, you should see a heading “AWS Lambda.” Go ahead and click on the “Create a new Lambda function.”

Go ahead and create a new Python 3.7 Lambda Function from scratch. For our example, I’m going to use “AmazonConnectJokeGenerator” as the name.

To make life easy, simply copy the code below into function’s code editor. The below is python code, so make sure that you keep the spaces in place.

import json
import random

def lambda_handler(event, context):
  resultMap = [
    {"Joke":"Why can’t you trust an atom?","PunchLine":"Because they make up literally everything."},
    {"Joke":"How do fish get high?","PunchLine":"Seaweed."},
    {"Joke":"Did you hear about the kidnapping at school?","PunchLine":"Everything’s fine. He woke up."},
    {"Joke":"What does a grape say after it’s stepped on?","PunchLine":"Nothing. It just lets out a little wine."},
    {"Joke":"Why don’t teddy bears ever order dessert.","PunchLine":"Because they’re always stuffed."},
    {"Joke":"What do you call a bear with no teeth?","PunchLine":"A gummy bear."},
    {"Joke":"What happens when a frog’s car breaks down?","PunchLine":"It gets toad away."},
    {"Joke":"I never wanted to believe that my Dad was stealing from his job as a road worker.","PunchLine":"But when I got home, all the signs were there."},
    {"Joke":"What’s the difference between roast beef and pea soup?","PunchLine":"Anyone can roast beef but nobody can pee soup!"},
    {"Joke":"Did you hear about the guy who broke both his left arm and left leg?","PunchLine":"He’s all right now."},
    {"Joke":"Why does Humpty Dumpty love autumn?","PunchLine":"Because he had a great fall."},
    {"Joke":"People wonder why I call my toilet “the Jim” instead of “the John.”","PunchLine":"I do it so I can say “I go to the Jim first thing every morning.”"},
    {"Joke":"I was wondering why the ball kept getting bigger and bigger…","PunchLine":"And then it hit me."},
    {"Joke":"What do computers snack on?","PunchLine":"Microchips."},
    {"Joke":"How come oysters never donate to charity?","PunchLine":"Because they’re shellfish."},
    {"Joke":"What’s the tallest building in the world?","PunchLine":"The library, cause it has the most stories."},
    {"Joke":"How do trees get online?","PunchLine":"They log in."},
    {"Joke":"Why did the scarecrow keep getting promoted?","PunchLine":"Because he was outstanding in his field."},
    {"Joke":"Money doesn’t grow on trees, right?","PunchLine":"So why does every bank have so many branches?"},
    {"Joke":"hy did the pig leave the party early?","PunchLine":"Because everyone thought he was a boar."},
    {"Joke":"Why shouldn’t you write with a broken pencil?","PunchLine":"There’s no point."},
    {"Joke":"Why are barns so noisy?","PunchLine":"Because all the cows have horns."},
    {"Joke":"What do you call someone wearing a belt with a watch attached to it?","PunchLine":"A waist of time."},
    {"Joke":"What’s the difference between a teacher and a train?","PunchLine":"One says, Spit out your gum and the other says, Choo choo choo."},
    {"Joke":"What did the janitor yell after he jumped out of the closet?","PunchLine":"Supplies!"},
    {"Joke":"How can you get four suits for a dollar?","PunchLine":"Buy a deck of cards."},
    {"Joke":"Why didn’t the melons get married?","PunchLine":"Because they cantaloupe."},
    {"Joke":"What do you say to a drunk who walks into a bar with jumper cables around his neck?","PunchLine":"You can stay. Just don’t try to start anything."},
    {"Joke":"A man got hit in the head with a can of Coke.","PunchLine":"Thank goodness it was a soft drink."},
    {"Joke":"What’s the difference between a cat and a complex sentence?","PunchLine":"A cat has claws at the end of its paws. A complex sentence has a pause at the end of its clause."}
  ]
  return random.choice(resultMap)

It’s a pretty script and selects a random string map from the “resultMap” list. Each of these maps consists of two strings, “Joke” and “PunchLine.” By passing these objects as the Lambda response, they will be available to the Contact Flow as “External” variables. For a more in-depth explanation of Contact Flow variables, you can refer to the AWS Connect Developer Guide here. Once you’ve copied the code, save the Lambda function and make sure it’s running correctly.

Browsing back to your Amazon Connect Console, you should now see you “AmazonConnectJokeGenerator” Lambda function in the list. Simply select it and click “Add Lambda Function.” This process will grant the correct permissions for Amazon Connect to call the lambda function.

Building the Contact Flow – Fetching the Joke

Now that we have our Lambda written and the correct permissions applied, it’s time to build out the contact flow. Go ahead and browse to the “Overview” of your instance and click the “Login as administrator” button

As we’ve done in our previous articles, go ahead and open the “Contact Flow” screen from the “Routing” Menu. Click the “Create contact flow” button and give your new flow the name “DialAJoke.”

First, we want to go ahead and start logging to the Contact Flow Logs. This will help us troubleshoot issues later on if they occur. Go ahead and drag the “Set Logging Behaviour” step from the “Set” menu, onto the Contact Flow. Check the steps properties and ensure that “enable” is selected. Make sure you also create a connector between the “Entry Point” and “Set Logging Behaviour” steps.

Next, Drag the “Invoke AWS Lambda function” step from the “Integrate” menu, onto the contact flow. Create a connection from the previous step. Next, open the properties window and select the “AmazonConnectJokeGenerator” lambda from the dropdown list.

Building the Contact Flow – Playing the Joke

The next step is to play the joke for the caller. This can be done by referencing the External Variables passed back by the Lambda function. From our source code, we have 2 variables we can reference, “Joke” and “PunchLine.” Drag a “Play Prompt” step from the “Interact” menu onto the Contact Flow and connect it to the “Success” output from the previous step. Under its properties, we want to select “Text to speech (Ad hoc).” From there, select the “Enter dynamically” radio button and select “External” from the Type Dropdown. Finally, enter “Joke” as the attribute and hit save.

Nothing is worse than that punchline of a joke being told before you’ve had a chance to think about it. So we are going to want to add a pause between telling the joke and telling the answer. However, Amazon Connect doesn’t have a “Pause” and “Delay” step that we can use. To work around this, we can add another “Play Prompt” step, and use some SSML to achieve the same result. SSML (Speech Synthesis Markup Language) is a way we can control how words/sentences are spoken by the Amazon Polly engine. Add the “Play Prompt” step, open the properties, and again select “Text to speech (Ad hoc).” In the “Enter Text” textbox, go ahead and add the following SSML. <speak><break time=”2s”/></speak>. This will not say anything but add a 2-second pause. Before exiting the properties window, make sure you change the “Interpret as” dropdown to SSML.

Finally, Go ahead and create the last “Play Prompt” step and repeat the process used to play the Joke. Only this time, make sure you define the “PunchLine” as the attribute to be used.

Building the Contact Flow – Handling Errors and Ending the Call

Before we can go ahead and publish our new Contact Flow, we need to close all the open pathways. Amazon Connect can’t publish a Contact Flow if any of the possible outcomes result in an unhandled call. To achieve this, simply create a new “Play Prompt” step and make a connection from the “Error” outcome of the “Invoke AWS Lambda function” step. Define a simple error message under “Text to speech (Ad hoc)” and save it.

Last but not least, we need to remember to hang up the call once we’ve finished. Go ahead and drag the “Disconnect / hang up” step onto the grid and connect the last two “Play prompts” to it.

At this stage, you should be able to select “Save and Publish” from the dropdown menu.

Conclusion

Following the instruction outlined in the accepting our first call article, you can direct an inbound number to your new “Dial A Joke Service” and your up and running. People can now ring your Direct Inbound Number, and Amazon Connect will tell them a joke. In the next article, we’ll add some additional features to our new service and look at some other logging options.

Category:
Amazon Web Services, Cloud Infrastructure
Tags:
,