Giter VIP home page Giter VIP logo

amazon-pinpoint-lex-bot's Introduction

Amazon Pinpoint-Lex Bot

Use Amazon Web Services (AWS) Pinpoint, Lambda, and Lex to let your customers interact with (Lex) chat-bots via 2-way SMS.

Sample SMS conversation screenshot

With this approach you will be able to create an SMS-based chatbot using Amazon Lex and Amazon Pinpoint. Such a chatbot can help your customers schedule appointments, lookup latest account information, or most common solutions to their troubles. Note that we will be using a generic Lex bot, which means you can reuse the same bot for your other services as well, be it Web or, for example, Amazon Connect/IVR.

Major benefits:

  1. Customer can use standard SMS
  2. No need for any other means of communication or installation of proprietary apps
  3. No need for a dedicated/separate chatbot
  4. Standard Lex settings and capabilities are supported
  5. Standard Lex security can be easily enabled for further security
  6. Customer phone number context is standard, remains linked to the phone number, allowing for pause-resume/reuse off another application (eg. website, app, etc.)
  7. Lex conversations can be easily stored and reused to enrich customer view

License Summary

This sample code and flow is made available under Open Source - MIT No Attribution License (MIT-0). See the LICENSE file.

Architecture

Upon an SMS request from the user, AWS Pinpoint publishes to AWS SNS queue. A custom AWS Lambda function detects a new event and redirects information, specifically the originating phone number and message, to Lex. Lex uses the phone number to keep context of the conversation, sends response back, and Lambda sends that response over to the customer. Once first loop completes, the customer can continue this back-and-forth, just like they would with the chat-bot in Alexa or on a website.

Architecture Diagram

Prerequisites

This flow requires that you have an AWS Account with AWS Pinpoint project and access to Lex.
No extra configuration is required - you can start right out of the gate.

Repository content

Main files:

.
├── README.MD                   <-- This instructions file
├── template.yaml               <-- SAM template. Make sure to edit manually first!
├── ExecutionRole.json          <-- Sample IAM policy (also part of the SAM template). Make sure to edit manually first!
├── src                         <-- Actual source code
│   └── app.js                  <-- Lambda function code
│   └── Sample-AccountBot.json  <-- Sample Lex bot you can reuse for testing your flow for the fist time.
│   └── package.json            <-- NodeJS dependencies and scripts
│   └── tests                   <-- Unit tests
│       └── unit
│           └── eventData.json  <-- Sample payload that mimics the SMS received from customer (from Pinpoint via SNS). Make sure to edit manually first!

Setup

Below is a high-level how-to, but you should feel free to follow the more expanded version of the walk-through with screenshots @ https://aws.amazon.com/blogs/messaging-and-targeting/create-an-sms-chatbox-with-amazon-pinpoint-and-lex/

Step 1: Pinpoint

  1. Create the new Pinpoint project and request a new long-code (the phone number through which all of the interactions will take place). See steps 1.1 and 1.2 of this tutorial. Alternatively, you can simply reuse an existing project and phone number (aka long- or short-code).
  2. Enable two-way SMS by clicking on the long-code of choice under the "SMS and voice" Settings and enabling the 2-way with the SNS topic defined. See step 1.3 of the same tutorial. Take note of this SNS topic (new or existing).
    If this number will be used for outgoing notifications, make sure you handle unsubscribe (aka STOP) requests before going to production.
  3. Optionally add a temporary email subscription to the SNS topic to passively and proactively monitor it during debugging.
  4. Send SMS through and confirm SNS event occurs.

Step 2: Lex

  1. Create the new Lex bot or identify the one you want to reuse.
    There are no special considerations in our implementation, even one intent should be sufficient. Feel free to use one of the Demo Lex bots as a starting point, if you don't have one already.
    As with any bot, try to keep messages on the shorter side and avoid special characters.

Step 3: Permissions/IAM

  1. Add new Lambda execution role with limited permissions to Pinpoint (to respond to the customer), Lex (to actually communicate with the bot), and CloudWatch Logs (for basic logging and debugging).
    Feel free to either reuse ExecutionRole.json provided, copy YAML portion from template.yaml if you prefer, or use Managed policies to build out the role.
    Remember that AWS Policies are always designed to only allow what is absolutely necessary. As you expand your application, you may need to add new (or remove) resources and functions you intend to call. You may also want to add X-Ray permissions down the line for full tracing support.

Step 4: Lambda

  1. Add the new Lambda function (either upload the included Lambda directly or copy-paste).
  2. Set Environment variables - these are dynamic configuration parameters that you will be able to switch/change in production.
  • PinpointApplicationId The ID of the Pinpoint project that you created earlier.
  • BotName The name of the Lex bot that you created earlier.
  • BotNumber The Pinpoint number Lex Bot should use (if you have more than one). Alternatively, consider checking which number customer sent the request to first.
  • BotAlias Latest (or whichever alias you gave the bot)
  • Region The AWS Region that you created the Amazon Pinpoint project and Lex bot in.
  1. Associate Lambda with SNS.
  2. Save.
  3. Configure a new Lambda Test event - use included eventData.json for the sample payload, but remember to change the phone number.

Step 5: Run and Confirm

  1. Run test and confirm your event produces the SMS back to the number you specified or check console for errors.
  2. Send a manual SMS to your Pinpoint number and confirm your event goes through SNS and you receive the bot response via SMS.
  3. Feel free to engage the bot and try out various scenarios.

amazon-pinpoint-lex-bot's People

Contributors

chriscoombs avatar dependabot[bot] avatar ilasoft avatar jpeddicord avatar sringopi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

amazon-pinpoint-lex-bot's Issues

Possible Bugs with Lambda code and IAM policy

Not a coder by trade so forgive me if the issues are basic misunderstandings.

First an assumption I made. Line 23 of template.yaml has "YOURPINPOINTPROJECTID" and line 58 has "YOURPROJECTORAPPID". I assume these are the same thing, the AWS Pinpoint project ID. If not then what is what? The read me file says that "PinpointApplicationId The ID of the Pinpoint project that you created earlier." so one of these should be the Pinpoint project ID.

Next line 58 ends with "...endpoints/" however when entered Lambda seems to think everything after the "/" is a comment and grays it out. I tried putting everything starting with "arn:aws:" to the end of the line in double quotes but am not sure that is correct. It fixes the comment issue (all the code below that appears normal and not grayed out) but the code itself does not work.

After adding the test code when testing the function I get many errors including "errorType": "Runtime.UserCodeSyntaxError" and the line below. Hopefully this helps debug the issue.

undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Unexpected identifier","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Unexpected identifier"," at _loadUserApp (/var/runtime/UserFunction.js:98:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object. (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}

Also having issues is the IAM policy. When I copy in the policy and edit the region, account number, and Pinpoint ID I receive warnings every service.

  • Pinpoint: specify apps resource ARN for the Sendmessage action, one or more actions may not support this resource.
  • Cloudwatch: Specify log-group resource ARN for the CreateLogGroup and 1 more action
  • Cloudwatch: Specify log-stream resource ARN for the PutLogEvents action.
  • Cloudwatch: One or more actions may not support this resource
  • Lex V2: IAM does not recognize one or more actions. The action name might include a typo or might be part of a previewed or custom service. [issue with the PostContent permission, this does not exist?]
  • Lex V2: One or more actions may not support this resource.
  • Lex V2: IAM does not recognize one or more actions. The action name might include a typo or might be part of a previewed or custom service.
  • Lex: One or more actions may not support this resource.

Are these warnings an issue, do you receive the same thing when this is tested on your end?

I realize this is a lot and hopefully most of the errors have one or two root causes. Any help you could provide would be welcome. Feel free to direct me to other resources. Note I found this by reading the post https://aws.amazon.com/blogs/messaging-and-targeting/create-an-sms-chatbox-with-amazon-pinpoint-and-lex/ and have followed the directions there, if there is another place I should be looking please let me know.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.