Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Building a chatbot – step by step

3,242 views

Published on

This presentation covers how to build a chatbot - step by step. It covers Facebook, integration using AWS Lex and AWS Lambda

Published in: Software
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Building a chatbot – step by step

  1. 1. Building a chatbot – step by step Srushith R srushith@codeops.tech
  2. 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. 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. 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. 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. 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
  7. 7. Python code
  8. 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. 9. 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
  10. 10. 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.
  11. 11. 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
  12. 12. Create an IAM role Select IAM in the AWS services and select roles
  13. 13. Create an IAM role
  14. 14. Create an IAM role select AWS Lambda
  15. 15. Add policies to the role add AWSLambdaExecute and AWSLambda FullAccess policies to the role
  16. 16. Create an IAM role
  17. 17. IAM role
  18. 18. Changed code (for logging)
  19. 19. Create a Lambda function
  20. 20. Select a runtime and blueprint
  21. 21. Create Lambda function inline code editing tab. If the code is simple (without any dependencies), you can add it here
  22. 22. Configure triggers can choose any of the triggers, but we don’t require any in this example
  23. 23. Folder with the dependencies code with dependencies must be uploaded as a zip file with all the necessary dependencies
  24. 24. Folder with the dependencies and the source code
  25. 25. Zip them
  26. 26. The resulting zip file
  27. 27. Upload the zip file
  28. 28. Add environment variables
  29. 29. Specify the lambda handler and choose a role for logging handler format: <Source code file name>.lambda_handler role with logging permissions
  30. 30. Set the timeout maximum runtime of the lambda function
  31. 31. Review the details
  32. 32. Review the details and create
  33. 33. Test the Lambda function
  34. 34. Configure the input test event
  35. 35. Save and test!
  36. 36. Lambda function output
  37. 37. Log output
  38. 38. Amazon Lex
  39. 39. Creating a bot using Lex
  40. 40. Creating bot using Lex
  41. 41. Add the bot details maximum inactive time
  42. 42. Create intent create an intent
  43. 43. Create intent
  44. 44. Create intent
  45. 45. Create intent
  46. 46. Utterances add preferred utterances – purchase a <docker> book
  47. 47. Add Slots add slots
  48. 48. Choose a Slot type you can choose from the existing templates or create your own slot type
  49. 49. Add Prompts prompts are questions to the users that help in filling the slots
  50. 50. the moment slot ‘title’ is entered, Lex recognises it in the utterances select if the particular slot is a compulsory one
  51. 51. Integrate Lex and Lambda select the desired lambda function from the drop down
  52. 52. Add permission
  53. 53. Save and build
  54. 54. Build
  55. 55. Its up and ready!
  56. 56. Lets test!
  57. 57. Oops!
  58. 58. 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}
  59. 59. 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.
  60. 60. 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
  61. 61. Change the code, again!
  62. 62. Test at the lambda end Zip the folder with the dependencies again with the updated code and test with the input:
  63. 63. Fingers crossed! The output at the lambda console:
  64. 64. Lets test from the bot
  65. 65. Why me?
  66. 66. Current scenario
  67. 67. But why? This is the reason: Lex Lambda
  68. 68. Lex expects a particular format Amazon Lex expects a response from a Lambda function in the following format:
  69. 69. 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.
  70. 70. 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
  71. 71. dialogAction - close
  72. 72. dialogAction - ConfirmIntent
  73. 73. dialogAction - delegate
  74. 74. dialogAction - ElicitIntent
  75. 75. dialogAction - ElicitSlot
  76. 76. Code change, one last time
  77. 77. Phew!
  78. 78. Integration with facebook
  79. 79. 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.
  80. 80. Create a facebook page
  81. 81. Facebook page
  82. 82. Facebook page - AmazonBooks
  83. 83. Create a facebook app facebook developer portal
  84. 84. Create a new app
  85. 85. Security check
  86. 86. Facebook app - AmazonBot
  87. 87. Choose Messenger
  88. 88. Facebook Messenger platform
  89. 89. Generate page access token Select the facebook page from the drop down
  90. 90. Allow permissions
  91. 91. Approve!
  92. 92. Page access token Note down the page access token
  93. 93. Facebook app secret Click on the dashboard to get the app secret
  94. 94. Connecting Lex and facebook
  95. 95. Channels in Lex In AWS Lex, select the channels tab and fill in the details for facebook integration
  96. 96. 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
  97. 97. Add the details verify token is used for handshake between Lex and facebook add the pre noted page access token and app secret
  98. 98. Callback URL A callback URL is generated once the bot is activated. This URL is used as the webhook URL at the facebook end
  99. 99. Webhook setup
  100. 100. Webhook setup callback URL and the same verify token from the Lex end Select messages, messagin_postbacks and messging_options
  101. 101. Subscribe the facebook page select the facebook (AmazonBooks) page from the drop down
  102. 102. Subscribe the facebook page
  103. 103. Search for the facebook page in the chat Search for the page in the chat or at messenger.com
  104. 104. Finally!
  105. 105. Hooray!
  106. 106. • Check out www.CodeOps.tech/meetup for upcoming meetups

×