Home > Uncategorized > Creating a #Facebook #Chatbot using #AWS #Lex and #Lambda

Creating a #Facebook #Chatbot using #AWS #Lex and #Lambda


Chatbots are a novel way of interacting with your customers if you don’t have the staff to deal with them personally.

Creating one using Amazon LEX is really easy, and you can do it with no code whatsoever. – Although if you want it to do anything of use, then you’ll need to write a Lambda function.

First, log into AWS, open up LEX, then create a new Bot.

You’ll need to set up an Intent, and add an utterance that will activate this intent. You add slots – which are dynamic data provided by the user that provides you with the information you need to fulfil the user’s request.

You can test it out on the right hand side of the page, but the real kicker is getting the bot to work with Facebook. – So, you head on over to developers.facebook.com and create a new App. Select Products > Add product > Messenger

In Token Generation, select your Facebook Page, and press generate. You’ll need this token later.  – Also grab the app secret key, you’ll need this in a moment.

Head back over to LEX, select Channels, then fill in the form, – Your Verify Token can be anything at all, just remember it for the next step.  Press activate, and you’ll get an Endpoint URL.

Jump back to Facebook, and open up the webhooks tab.  Create a new webhook, and enter the Verify Token, and the Endpoint URL. give permissions for the following: messages, messaging_postbacks, messaging_optins , and then select a page, and press subscribe to events.

Now, if you open Facebook messenger on your phone and start a chat with your business page, your bot should start replying to messages

Of course, to make it do anything useful, you have to write some code, To do this, you create a lambda function in AWS, and link it to the Fulfilment step in LEX. In this example, I am taking the data provided by the user, and building up a URL from it, that the user can click – so that a web-page can finish serving the customer.

I used this code; based on the orderFlowers NodeJs example.

function elicitSlot(sessionAttributes, intentName, slots, slotToElicit, message) {
return {
dialogAction: {
type: ‘ElicitSlot’,

function close(sessionAttributes, fulfillmentState, message, responseCard) {
return {
dialogAction: {
type: ‘Close’,

function delegate(sessionAttributes, slots) {
return {
dialogAction: {
type: ‘Delegate’,

function FindFlight(intentRequest, callback) {
const source = intentRequest.invocationSource;
const depart = intentRequest.currentIntent.slots.departure;
const destination = intentRequest.currentIntent.slots.destination;
const departureDate = intentRequest.currentIntent.slots.departureDate;
const returnDate = intentRequest.currentIntent.slots.returnDate;
const Adults = intentRequest.currentIntent.slots.Adults;
const Children = intentRequest.currentIntent.slots.Children;
const infants = intentRequest.currentIntent.slots.infants;

if (source === ‘DialogCodeHook’) {
const outputSessionAttributes = intentRequest.sessionAttributes;
callback(delegate(outputSessionAttributes, intentRequest.currentIntent.slots));
callback(close(intentRequest.sessionAttributes, ‘Fulfilled’,
contentType: ‘PlainText’,
content: `Click the following link to see our flight offers`
“version”: 3,
“contentType”: “application/vnd.amazonaws.card.generic”,
“genericAttachments”: [
“title”:`${depart} to ${destination}`,
“subTitle”:`${departureDate} – ${returnDate}`,



* Called when the user specifies an intent for this skill.
function dispatch(intentRequest, callback) {
console.log(`dispatch userId=${intentRequest.userId}, intentName=${intentRequest.currentIntent.name}`);

const intentName = intentRequest.currentIntent.name;

// Dispatch to your skill’s intent handlers
if (intentName === ‘YOUR-INTENT’) {
return FindFlight(intentRequest, callback);
throw new Error(`Intent with name ${intentName} not supported`);


exports.handler = (event, context, callback) => {
// TODO implement
dispatch(event, (response) => callback(null, response));


Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: