Run Node.js Facebook Messenger Chat Bot on AWS Lambda
5 stars based on
With bots, app owners can better engage with their users by providing personalized and interactive communication that can scale for the masses. Since the launch, businesses and app owners have shown great interest in the chat bots. Just three months after the announcement, there was an estimated 11, bots built on the platform.
Users of these bots can enjoy a myriad of services such as:. The current interest in and appeal of bot status fb is obvious and as the technology in artificial intelligence improves, the bots will get better at interacting with users. Being skilled in AI will certainly help, especially in building sophisticated bots, but is not required. You can certainly build a bot without knowing machine learning.
There are two types of bots you can build. One is based on a set of rules and the other uses machine learning. The former is limited in the interactions it can offer. It can only respond to specific commands. With bots that use machine learning, you get better interaction with the user. Bot status fb user can interact with the bot in a more natural way as they would in a human to human interaction, as opposed to just using commands. The bot also gets smarter as it learns from the conversations it has with people.
Machine learning knowledge will not be necessary, though. Lucky for us, there are services such as wit. You can download the code for the completed demo app here. Also, as of version 2. To get started, first make sure Node is installed on your computer. You can check this by typing node -v in the Terminal. If installed, it will output the version number. Use heroku --version to verify that the CLI is installed. Create the project directory and initialize a package. This lets Heroku know bot status fb command to execute to start the app.
During project setup, I defined app. Modify it as shown:. The first GET handler will be for our own testing — to know if the app has been successfully deployed. The second GET handler is the endpoint that Facebook will use to verify the app.
You can paste your own token into the code. For the Facebook platform to connect with our backend application, bot status fb first need to put bot status fb online. On running bot status fb heroku open command, the link to the running app will be opened in your bot status fb browser. If everything went well, you will get a page with the text Deployed! Open your Heroku Dashboard and select the app that you just deployed.
Commit your changes and push them to Heroku. You can create a new Page or use an existing one. Select a Page Type from the bot status fb options. Ater clicking on Get Startedthe Page will be created and you will be asked for more details regarding your app description, website, profile picture, target bot status fb, e.
You can skip these setup steps for now. To create bot status fb Facebook App, head over to the Add a New App page and click on the basic setup link below the other platform choices. You will then be taken to the Messenger Settings page shown below. To receive messages and other events sent by Messenger users, the app should enable webhooks integration.
Webhooks formerly Real Time Updates let you subscribe to changes you want to track and receive updates in real time without having to call the API. These specify which events the app will be subscribed to. On successfully enabling webhooks, you should see Complete in the Webhooks section and a list of the events subscribed to.
In the Token Generation section, select your Page from the bot status fb menu. After authentication, a Page Access Token will be generated for you. Note that the bot status fb token will not be saved in the current page bot status fb is shown in on Facebook. Each time you go to that web bot status fb, the Page Access Token field will be blank and when you select your Facebook Page from the Bot status fb dropdown menu, a new token will be generated.
However, any previous tokens created will continue to function. So make sure you copy the token before closing the web page. In order for your webhook to receive events for a specific page, you must subscribe your app to the page. In the Webhooks section, select the page to subscribe to. When a user starts a new conversation with your bot, the first thing they will see is the Welcome Screen. You can customize the screen by setting Greeting Text that will be used instead of the Page description.
By default, to start a conversation it is the user that sends the first message to your bot. You can, bot status fb, enable a Get Started button that will enable your bot to send the initial message.
The button will send an event to your server that you can then respond to. Select Messaging from the left panel and then turn on Show a Messenger Greeting on the right panel. Set the message to your liking. If the request is successful, a Get Started button will appear on the Welcome Screen of a new conversation.
A user clicking on the button will trigger the postback bot status fb callback. Your bot can then respond to this postback. Postbacks can be triggered by different types of components — a Postback button, Get Started button, Persistent menu or Structured Message. You can set any string as the payload. To receive postback messages, your app must be subscribed to postbacks on your webhook.
Remember the Welcome Screen and Get Started button only appear for new conversations. When coding and testing your bot, you can start a new conversation by deleting the current one. Whenever someone interacts with your bot or sends your page a message, an update will be sent via your Bot status fb integration.
To get the messages, you have to listen for POST calls at your webhook. All callbacks will be made to this webhook. In the POST handler above, we iterate over the messaging entries sent to the app. Sometimes messages are batched and sent together, so an entry may contain multiple objects. The following shows the different callback messages that can be sent to the app.
If the event is a Postback, we call the processPostback function where we check the payload value. Remember, we had set Greeting as the payload of the Get Started button, so here we first check if the messaging event was sent as a result of clicking that button. From the API, you can get their first name, last name, profile picture, locale, timezone, and gender.
It is important to bot status fb a response as fast as possible. Facebook will wait for a before sending you the next message. In high volume bots, a delay in returning a can cause significant delays in Facebook delivering messages to your webhook. If your webhook returns an error i. If the webhook continues to fail for 8 hours, then Facebook will send you an alert letting you know that the webhook is being disabled, then your app will be unsubscribed.
To test the bot, you can start a conversation with your page either from facebook. Your users might select the wrong page. To prevent this you can set a unique username for the page. You can also go to https: Below, you can see the greeting text we set bot status fb the Welcome Screen as well as the Get Started button.
They can also enter another movie name and get its details instead. On Heroku, while you can use the free sandbox mlab plan, you will need to have a credit card saved on Heroku for verification. If you do not want to provide your card details, then you can register an account on the mLab website, create a free-tier sandbox database there and link to bot status fb from your code more on this shortly.
Search for mlab and select the result. On the dialog window that pops up, select the Sandbox - Free plan from the dropdown menu and click on Provision. You should see a confirmation of the chosen add-on.
If you prefer setting up the MongoDB database on the mLab website, then register for an account there and head over to the Create new deployment page. Set a name for your database and complete the process by clicking the Bot status fb new MongoDB deployment button. On the page that follows, select the database you just created from the displayed table. A page will bot status fb opened that will show instructions on how to access the database.
Select the Users tab, and click the button Add database user. Fill out a username and password, and click Create. This creates a new set of credentials you can use to allow your app access to the DB.