2. What is this bot all about?
• This is a bot for buying books – ‘Amazon
BookBot’
• You can interact with this bot to find out
relevant technical books on the given topic
available in Amazon
• For each book, you can find out book details
(author, price, reviews, availability, etc.)
• This bot can be integrated with
FaceBook/Slack
3. Technologies
• Amazon Web Services
– AWS Lex – for Natural Language Processing (NLP)
– AWS Lambda – for the back-end serverless
function(s)
• Python 2.7 – for the serverless function code
– python-amazon-simple-product-api 2.2.11 - A
Python wrapper for the Amazon.com Product
Advertising API (for getting book information from
Amazon)
4. Installation (for hands-on)
• AWS account
– Preferably AWS CLI installed
• Python 2.7 installed
• Python module installed: python-amazon-
simple-product-api 2.2.11
Note: The Python module and the source code
must be available in the same folder (for
uploading into AWS Lambda)
5. Roadmap
1. Write a python code to get book details given
a keyword from Amazon
2. Create a Lambda function
3. Create a chat bot using AWS Lex
4. Integrate with facebook
6. Getting started
• The core functionality of fetching the details
of book(s) given the keyword can be
implemented as a simple Python code first
– This will reduce your effort in coding, debugging
and testing your Python code
– The same functionality can be integrated as an
AWS Lambda function later
– Here is the sample code with its invocation sample
as an example
8. Output
$ python books.py
1 The DevOps Handbook: How to Create World-Class Agility, Reliability, and Security in
Technology Organizations
2 The Phoenix Project: A Novel about IT, DevOps, and Helping Your Business Win
3 What is DevOps?
4 DevOps Handbook: A Guide To Implementing DevOps In Your Workplace
5 The DevOps 2.0 Toolkit: Automating the Continuous Deployment Pipeline with
Containerized Microservices
6 Building a DevOps Culture
7 The Phoenix Project: A Novel About IT, DevOps, and Helping Your Business Win
8 The DevOps 2.1 Toolkit: Docker Swarm: Building, testing, deploying, and monitoring
services inside Docker Swarm clusters (The DevOps Toolkit Series) (Volume 2)
9 5 Unsung Tools of DevOps
10 Effective DevOps: Building a Culture of Collaboration, Affinity, and Tooling at Scale
9.
10. Tweak the code
• Regardless of the language you choose, there
is a common pattern to writing code for a
Lambda function that includes the following
core concepts:
– Handler: Handler is the function AWS Lambda
calls to start execution of your Lambda function
– The context object: Via context object your code
can interact with AWS Lambda
11. Lets log!
• Add logging statements to the Python code to
see what exactly is happening in the
background. There are two ways to log:
– Use of print statements: all print statements are
by default logged into CloudWatch
– Use ‘logging’ module in python: logging.*
functions write additional information to each log
entry, such as time stamp and log level.
12. IAM role for logging
• IAM (Identity & Access Management) role is
similar to an user, in that it is an AWS identity
with permission policies that determine what
the identity can and cannot do in AWS
• It is a service that logs AWS events made by or
on behalf of your AWS account
• Create an IAM role to be used for logging.
– You should permit this role to log the data
13. Create an IAM role
Select IAM in the AWS services and select roles
60. Lets interrogate – check logs on
CloudWatch
But this is what we got:
Expected input:
{
“title” : “java”
}
{u'currentIntent': {u'slots': {u'title': u'java'}, u'name': u'get_book_titles',
u'confirmationStatus': u'None'}, u'bot': {u'alias': None, u'version': u'$LATEST', u'name':
u'BookBot'}, u'userId': u'dmfh3duubfag9vpt9a6va3sj153hfj70', u'inputTranscript':
u'suggest a java book', u'invocationSource': u'FulfillmentCodeHook',
u'outputDialogMode': u'Text', u'messageVersion': u'1.0', u'sessionAttributes': None}
61. How Lex communicates?
• Lex doesn’t just send the slot data, it sends much
more!
– currentIntent: has slots, name (the intent name) and
confirmationStatus
– bot:
• name – The name of the bot that processed the request
• alias – The alias of the bot version that processed the request.
• version – The version of the bot that processed the request.
– userId : This value is provided by the client application.
Amazon Lex passes it to the Lambda function.
– inputTranscript: The text used to process the request.
62. How Lex communicates?
• invocationSource:
– DialogCodeHook: Amazon Lex sets this value to
direct the Lambda function to initialize the
function and to validate the user's data input
– FulfillmentCodeHook: Amazon Lex sets this value
to direct the Lambda function to fulfill an intent.
• sessionAttributes: Application-specific session
attributes that the client sent in the request.
This plays an important role for conversational
bots, where a state has to be maintained
70. Lex expects a particular format
Amazon Lex expects a response from a Lambda function in the
following format:
71. Lambda response format for Lex
• sessionAttributes: This field is optional
• dialogAction : The dialogAction field directs Amazon Lex to
the next course of action, and describes what to expect from
the user after Amazon Lex returns a response to the client.
The type field indicates the next course of action
– Close — Informs Amazon Lex not to expect a response
from the user
– ConfirmIntent — Informs Amazon Lex that the user is
expected to give a yes or no answer to confirm or deny the
current intent.
– Delegate — Directs Amazon Lex to choose the next course
of action based on the bot configuration.
72. Lambda response format for Lex
– ElicitIntent — Informs Amazon Lex that the user is
expected to respond with an utterance that
includes an intent
– ElictSlot — Informs Amazon Lex that the user is
expected to provide a slot value in the response
81. Integration with facebook
On the Facebook developer portal, create a
Facebook application and a Facebook page.
–App Secret for the Facebook App.
–Page Access Token for the Facebook page.
97. Channels in Lex
In AWS Lex, select the channels tab and fill in the details
for facebook integration
98. Aliases
An alias is a pointer to a specific version of an Amazon Lex bot.
Use an alias to allow client applications to use a specific version
of the bot without requiring the application to track which
version that is.
Go to the settings tab of AWS Lex and add a name and version
(Latest by default) and click on the add button
99. Add the details
verify token is used
for handshake
between Lex and
facebook
add the pre noted
page access token
and app secret
100. Callback URL
A callback URL is generated once the bot is activated. This URL is
used as the webhook URL at the facebook end