This is a simple book recommendation bot
built using Amazon Lex
, integrated with a Lambda function
, to return personalized book suggestions to the users.
This bot has been deployed on two services, kommunicate
& Twilio
- With kommunicate, the chatbot was embedded into an HTML website & the delivery of the chatbot's responses to users was managed, through the web interface.
- Whereas on Twilio, the delivery of the bot's responses to users via the chosen communication channel, here,
WhatsApp
, was managed.
-
Sign in to
AWS Management Console
-
Create a Lex Bot
- Click on "Create bot."
- Choose "Custom bot" and provide the following details:
- Bot name: BookRecommendationBot
- Language: English (or your preferred language)
- Output voice: None (since it's a text bot)
- Session timeout: 5 minutes (default)
- COPPA: No
- IAM role: Create a new role with basic permissions
- Sentiment analysis: No
- Add
Intents
:
- Click on "Create intent."
- Intent name: WelcomeIntent
- Sample utterances: Add various ways users might ask for a recommendation, e.g., "Can you recommend a book?", "book recommendations", "Suggest a book", , “Hello”
- Define
Slots
:
- Slot name: Genre
- Slot type: AMAZON.BookGenre (Since, this SlotType isn’t provided by AWS, create a new type)
- Prompt: "What genre of books do you like?"
- Save the Intent and Build the Bot
-
Navigate to
Lambda Console
-
Create a Function:
- Click "Create function."
- Choose "Author from scratch."
- Provide the following details:
- Function name: BookRecommendationFunction
- Runtime:
Python 3.8
(or your preferred runtime - Permissions: Choose an existing role or create a new one with basic Lambda permissions.
- Add Code:
- Replace the default code with
lambda_function.py
-
Deploy the Function
-
Add Lambda Trigger in Lex:
- Go back to your Lex bot.
- In the Intent, scroll to Fulfillment and enable it.
- Click "Save intent" and "Build."
- Deployment:
- Under Deployment, go to Aliases.
- Choose the Alias associated with the Bot & select Bot's language.
- Choose the Source & latest Lambda function and Save the settings.
-
Navigate to
IAM Dashboard
-
Visit Roles > Your-Bot
-
Under Permission Policies
- Create an inline policy, namely, LexInvokeLambdaPolicy & add the code from
iam_permissions.json
- Save changes
- Obtain AWS Credentials:
- Sign in to AWS console as Root/IAM user.
- Open IAM panel > "Access management -> Users".
- Create a user (e.g., “Lex-bot”).
- Attach policies:
AmazonLexReadOnly
andAmazonLexRunBotsOnly
. - Create Access Key: Select "Third-Party Service" and generate keys.
-
Gather Lex Bot Details:
- Bot Name: Name of your Lex bot.
- Bot Alias: Alias created when publishing the bot.
- AWS Region: Region where Lex service is running.
-
Integrate Lex Bot with Kommunicate:
- Log in to
kommunicate
> Bot section. - Click "Integrate Bot" in Amazon Lex card.
- Fill in Access Key ID, Secret Access Key, Bot Name, Bot Alias, AWS Region.
- Click "Next".
- Configure Bot in Kommunicate:
- Name your bot (visible to users).
- Enable/Disable "autoHandoff".
- Select "Let this bot handle all new conversations".
- Set Welcome Message in Lex:
- Add
kmConversationStarted
as sample utterance to WelcomeIntent.
- Add
Once integrated, the HTML website embedded with Lex-bot would look like:
- Set Up Twilio Account
- Sign up at
Twilio
- Obtain your Account SID and Auth Token.
- Verify your phone number in the Twilio console under "Verified Caller IDs".
- Create Bot Version and Alias in Lex:
- Go to your Lex bot and create a new version.
- Create an alias for the bot and link it with the new version.
- Configure Twilio Messaging Service:
- Access the
Twilio console
and navigate to "Messaging." - Set up a messaging service and take note of the assigned phone number.
- Verify your WhatsApp number following Twilio's sandbox setup.
- Integrate Lex with Twilio
- Navigate to "Channels" in the Lex console and add a Twilio channel.
- Enter your
Twilio Account SID
andAuth Token
- Copy the callback URL provided by Lex.
- In Twilio, set the webhook URL (callback URL from Lex) in the messaging service.
- Test the Integration
- Send a message from your verified
WhatsApp
number to the Twilio number. - Confirm reception of responses from the Lex bot to validate the integration.
After deployement, the conversation over the chosen communication channel, could look like: