Skip to content

Building Bots for WhatsAPP

Amitha R edited this page Oct 13, 2018 · 5 revisions

Application Created - BotWorthy HR

BotWorth <-----> (Recruitment Questions) <-----> watsapp

Google Sheets <----> Webhooks on AWS Lambda <---> Dialog Flow <---> twilio <----> watsapp.

Step 1: Create Agent in DialogFlow

  • Sign in Dialogflow
  • Create an Agent - BotHRWorthyBot

Step 2: The Whatsapp Sandbox

  • Whatsapp for Business is an application that offers an API service.
  • It runs as a docker container for on-premise deployment. Or, it can be setup on AWS using a pre-built CloudFormation template.
  • Use MySQL for storing data.
  • Whatsapp requires to register a phone number and that will serve requests using twilio
  • Create a new project and Continue.
  • Programmable sms section -> Whatsapp - activate the sandbox.
  • Store the listed number and send the requested message - join the sandbox.

Step 3: Twilio and Dialogflow Integration

  • WhatsApp Business API + twilio ------------------ Dialogflow

  • DialogFlow - Integrations - Twilio(Text Messaging) - Require Authentication Code.

  • Twilio - Programmable SMS Dashboard section -> right-top corner - show API Credentials.

  • Copy the credentials and paste it into the DialogflowTwilio integration form.

  • Start the form, this internally springs a nodejs app with the integration process built-in.

  • In the Dialog-floe Twilio-form copy the request url and paste it in twilio - programmable-sms -whatsapp - sandbox section section - "WHEN A MESSAGE COMES IN" - Save.

Step 4: Default Intents

  • In Dialogflow - BotHRWorthy - Intent
    • Default Welcome Intent
    • Default Fallback Intent

Step 5: Service Account & Google Drive API

  • Google Sheets - Database.

Dialogflow <----> Webhook <----> Service Account <----> Google Sheets

  • Service Account Setup:
    • Dialogflow -> Setting -> Service Account (Dialog flow uses this to interact with Google Cloud Platform);
    • Click and open the link in a new tab - Google Cloud Platform.
    • IAM & admin section -> Service Account ---> Create Service Account
    • Enter the service Account -> select role - owner -> download the JSON file -> Done.
    • Search for Google Drive API - Enable it.

Step 6: Google Sheets & Permission

  • Google Drive - signin.
  • New - Google sheets - blank spreadsheets - create two tabs - Jobs, Applications.
  • Google Cloud Platform - left top corner - IAM & admin -> Service account - Email.
  • Google Sheet -> Share button - paste the email id --> edit -> send.

Step 7: AWS Setup

  • AWS Lambda - Function as a Service - deploy functions without worrying about scaling or the underlying server setup.

Outside ----Incoming Requests------> API Gateway ----------------------------> AWS Lambda

Outside <------Response------------- API Gateway <---------------------------- AWS Lambda

  • Serveless movement - decouple large application into small manageable components.

Serverless framework - configure and deploy the webhook.

  • Download AWS CLI - windows download installer.
  • Enable secure access to aws account.
    • The CLI use special access key id and a secret key id to talk to the AWS account. The serverless framework also uses the function to deploy on AWS lambda.
    • Services - IAM (Identity Access & Management)- Users Create a new users, attach the policies - AdministrativeAccess.
    • Store the Access Key and Password.
    • In windows explored - folder - cmd - aws configure - paste the Access Key, Password, etc.

Step 8: The Serverless Framework

  • In cmd - npm i -g serverless
  • sls --version

Step 9: Securely Storing Secrets

  • AWS System Manager's Parameter Store - Secrets are stored encrypted at rest.
  • SSM Dashboard - AWS -> Services -> System Manager --> Parameter Store.
  • Creation of Parameters:
    • In cmd -> aws ssm put-parameter --name "<>" --value "<>" --type SecureString
      • Read -> aws ssm get-parameter --name "<>"

Step 10: Setting the webhook

  • In the folder - cmd - sls create --template aws-nodejs --path bothrworthywebhook.
  • This creates a ready-made boiler plate that deploys has a AWS lambda.
  • This command create a folder and certain files in it.
    • handler.js
    • serverless.yml - config file
  • Lambda functions are built only for a certain period of time hence are advantageous.
  • make changes in serverless.yml file.
  • npm init, intall serverless-offline.
  • Type sls offline - inorder to bring the "webhook" locally - port 3000.
  • check the API in postman - POST - http://localhost:3000/bothrworthywebhook - body (someKey, someData) - send.

Step 11: IAM Policy for accessing Secrets

  • AWS Lambda function must be able to interact with SSM Store.
  • IAM policy - permit permissions, read parameters, etc.
  • Get parameters from AWS, region is also required.
  • Make changes to serverless.yml - ass roles, regioncodes.

Step 12: Google Sheets - A higher order function

  • Require the Accounts Private Key and Client e-mail.
  • Use service account to gain access to the sheets API.
  • Gain Access to the BotHRWorthy Sheets document.
  • Get to individual worksheets.
  • Read/Write/Search.
  • Create -Sheets/index.js -> store the parameters and write functions to extract it.
  • In dialogflow - create an Intent - "Get-Openings".

Step 13: Handling Get-Openings

  • Code to handle the get-opening api from dialog-flow.
  • Run ngrok - webhook
  • The webhook from ngrok in integrated into dialogflow -> fullfillment --> http:...../bothrworthywebhook.
  • Save changes and test setup.

Step 14: The Apply & Status Intent

  • Intent (Apply) ---| What is the job code that you wish to apply for? Are you above 18? Do you have cs degree? /work ex? relocate to san francisco? | ---- AWS Lambda function.
  • Create an intent "Apply" - Dialogflow and an entity termed "Yaynay".

Step 15: Deploying the webhook on AWS Lambda

  • The serverless framework -- creates a JSON Document - "CloudFormation Template" (Code & Assets) - resources required to deploy it on the AWS platform.
  • It then packages the code and deploys into an Amazon S3 bucket.
  • S3 - Simple Storage Service - stores files of all kind.
  • Thereafter it is deployed on AWS Lambda.

Serverless Framework ----> CloudFormation Template -(code&Assets)--------> Amazon S3 -------> AWS Lambda

  • cmd -> sls deploy.
  • Obtain - Endpoint URL to Lambda function.
  • Copy the url -> into Dialogflows fulfillment webhook -> Save.
  • Test the changes with Whatsapp.