A bot that helps you find highly rated food recommendations nearby, using AWS Lex and the Google Maps Places API.
Time estimate: 30 minutes (20 minutes if you are familiar with AWS Console or creating Slack apps)
Before you begin, you need:
- A Transposit account with a forked version of this app
- A Slack workspace where you can create and install apps
- A hunger for good food nearby
Why Amazon Lex?#
Amazon Lex uses natural language understanding (NLU) to help you build your own conversational interfaces. The days of just using text string matches to build a bot are gone. Lex allows you to quickly build more advanced, natural language, conversational bots (also known as chatbots). This example could be built without Lex but it would require more code to clarify the intent of the text. So, let's get started!
Set up your Amazon Lex bot#
- If you don't already have an AWS account, follow the instructions here. You don't need to get setup with the AWS CLI.
- Once you are signed into AWS, navigate to the Amazon Lex console at https://console.aws.amazon.com/lex/.
- If this is your first Lex bot, choose the "Get Started" button; otherwise, on the Bots page, choose the "Create" button.
- On the Create your bot page, set the following:
- Bot name: WhereToEat
- Output voice: None
- Session timeout: 10 min
- COPPA: No
- Click the "Create Intent" button, choose "Create intent," and give it the name "FindRestaurant"
- Under Sample utterances, Copy and paste the following phrases, you can add others, but these are good to start with:
- food nearby
- I want to eat food
- I want food
- Help me find a restaurant
- Help me find a place to eat
- What is a good place to eat
- What food is nearby
- Where should we eat
- Under Slot types in the left navigation, create a slot type:
- Name: PriceLevel
- Description: The restaurant price level on Google Maps
- Slot Resolution: Restrict to Slot values and Synonyms
- Under Value:
- 1, Synonyms: inexpensive, cheap
- 2, Synonyms: moderate
- 3, Synonyms: expensive
- 4, Synonyms: very expensive
- Under Slots, add these three slots:
- Name: Location, Slot type: AMAZON.PostalAddress, Prompt: What address are you located at?
- Name: Price, Slot type: PriceLevel, Prompt: How much do you want to spend? (Inexpensive, Moderate, Expensive, Very Expensive)
- Name: Distance, Slot type: AMAZON.NUMBER, Prompt: How far do you want to walk? (in minutes)
- Make sure to save the intent at the bottom of the page and build it at top
- You can now test out the basic functionality in the console's test chatbot feature
- After confirming the setup, you can publish. You will create an alias (i.e. TEST) that will later be used in Transposit.
Get Access and Secret Keys#
Here we are creating a user. Following the principle of least privilege, we’re going to only give them access to the permissions they need to run a Lex bot, and nothing else.
- Under Services in AWS, navigate to Identity and Access Management (IAM)
- Click on Groups, and Create New Group
- Choose a group name related to Lex (e.g. LexProduction)
- Attach the AmazonLexRunBotsOnly policy and create the group
- Click on Users, and Add user.
- Choose a user name that will be descriptive for it being able to programmatically access your Lex bot (e.g. lex-runtime)
- Make sure to only check the Programmatic access under AWS access type
- Add user to the group you just created by checking the box for the corresponding group
- You don't need any tags, so click through to create the user
- You will need to copy and paste the Access Key ID and Secret Access Key in the next part, so make sure to temporarily paste them into a secure text for future use.
Set up AWS Lex in Transposit#
- Now that you have your Access Key ID and Secret Access Key, navigate to your forked Transposit app.
- Under Auth & settings > Keys, click the Connect button and fill it in with your Access Key ID and Secret Access Key.
- Under Deploy > Production Keys, click Add key for aws_lex_runtime and fill it in again with the same Access Key ID and Secret Access Key.
- Under Code > Data connections -> aws_lex_runtime, add what AWS region you are using, you can see this in the URL for the AWS console (e.g. us-west-2).
- Lastly, in the post_text operation, change the bot alias to the one you published with (e.g. 'TEST') and change the botName to 'WhereToEat'.
- You should be able to click Run and get results, similar to this:
"message": "Sorry, can you please repeat that?",
Setup Google Maps#
- Go to https://cloud.google.com/maps-platform/#get-started to get an API key for the Google Maps Places API
- You will be prompted to login to the Google account you want to associate the key with
- Select Places checkbox in the pop up
- Enter new project name (e.g. WhereToEat)
- You may need to set up billing, Google Cloud Platform will likely give you a year long free trial at this time if you have haven't set this up before
- You will see a pop up with your API key, copy this key
- In Transposit, under Auth & settings > Keys, add your key to the google_maps connector.
- Under Deploy > Production Keys, click Add key for google_maps
- You should be able to now click Run on both the search_nearby and geocode operation and get results, similar to this:
"long_name": "Barton Springs Road",
"short_name": "Barton Springs Rd",
- Create a new app here in Slack
- Copy the URL for your webhook in Transposit under Deploy > Endpoints, under the deploy as webhook checkbox, similar to this:
- In your Slack App configure page, select Event Subscriptions, and paste in the URL you copied from Transposit (Note: It should automatically verify that the function is valid)
- On the same page, under Subscribe to Bot Events, add the message.im event and message.channels event (only if you want it to listen to channels too)
- In your Slack App configure page, select Bot Users, and add a bot user with the WhereToEat display name, wheretoeat username, and always show the bot as online
- Follow the instructions here to get OAuth setup and under Scopes in Slack, make sure to set the chat:write:bot and bot scopes
- If you have not installed or reinstalled the app, in the Slack app configuration, go to the Install App section and install it into your workspace
- Now you can DM your bot, telling it you want a place to eat and follow its prompts!
Note: Right now, you will need to message the bot something similar to the sample utterances for the bot to understand your prompt.
- Add another criteria (type of restaurant, minimum rating, etc) using slot values with AWS Lex
- Include validation for responses or more sample utterances in AWS Lex to make the bot “smarter”