Skip to content

A starter kit for a Messenger chatbot using Serverless framework and AWS lambda

Notifications You must be signed in to change notification settings

YuLingCheng/messenger-chatbot-starter-kit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Messenger Chatbot Starter Kit

This starter kit uses Serverless and AWS lambda.

Follow installation process to get started with your Messenger chatbot.

For more instructions on how to set up your Facebook app, have a look at this tutorial.

Content of the starter kit

Once installed and set up, this starter kit will provide you with a chatbot that will send a default message with a helper that the user can click to get some help.

  • serverless.yml contains minimum config to create a Lambda function with GET and POST endpoints to connect with your Facebook Messenger app
  • handler.js contains the Lambda function
  • bot.js contains the default services to send messages
  • secretary.js contains the default services to generate Messenger formatted messages (image messages, messages with action button, messages with quick replies... Add more using Facebook documentation)

Get started

Requirements

  • Have Serverless installed and set with your AWS credentials.
  • Have a Facebook page that you own
  • Have a Facebook app with the Messenger module

Module dependencies

This basic chatbot Lambda function uses axios to post to the Facebook page.

Installation

sls install -u https://github.com/YuLingCheng/messenger-chatbot-starter-kit -n my-chatbot && cd my-chatbot
npm install

In serverless.yml, pick a FB_APP_TOKEN and fill in your FB_PAGE_ACCESS_TOKEN under functions.chatbot.environment. If you don't know how to the FB_PAGE_ACCESS_TOKEN check this article

  • You can change provider.region if you want to pick another data center.
  • You can change provider.stage if you want to change environment stage.

Deploy

To deploy:

# First time
sls deploy

# Deploy only the function (much faster)
sls deploy -f chatbot

You can remove your project from AWS my running

sls remove -v

Testing your chatbot

Test your function locally with the following command:

sls invoke local -f chatbot -p <path_to_file>.yml # you can use .json files as well

Test your deployed function with the following command:

sls invoke -f chatbot -p <path_to_file>.yml

There are some test files available in testFiles, you can test:

You'll need to get your senderId and set it in these files.

To get your senderId,

  1. add console.log(senderId); in the handler.js
  2. deploy the lambda
  3. set up the webhook on facebook using the endpoint created
  4. subscribe your app to the page
  5. send a message to your page
  6. get your senderId from the logs using sls logs -f chatbot

Bonus

cUrl commands to set up your Messenger Bot initial messages

Set initial message

curl -X POST -H "Content-Type: application/json" -d '{
  "setting_type":"greeting",
  "greeting":{
    "text":"Hello {{user_first_name}}"
  }
}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token=YOUR_FB_PAGE_ACCESS_TOKEN"

Add initial call to action

curl -X POST -H "Content-Type: application/json" -d '{
  "setting_type":"call_to_actions",
  "thread_state":"new_thread",
  "call_to_actions":[
    {
      "payload":"INIT_HELP"
    }
  ]
}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token=YOUR_FB_PAGE_ACCESS_TOKEN"   

About

A starter kit for a Messenger chatbot using Serverless framework and AWS lambda

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published