How to create Facebook Messenger Bot

This tutorial will guide you through the process of creating a simple messenger bot, using the Facebook Messenger API, node.js, and ngrok.

Create a Facebook Page.

Create a Facebook App, Click Add a New App. There are a few platform for you to select, for this tutorial I will choose the basic setup, type the display name, contact email and choose a category for your app.

From Product Setup dashboard, select Messenger then click the “Get Started” button. Select the page you just created, and this will generate a Page Access Token.

Download Node.js source code or a pre-built installer for your platform. Then, open up terminal, navigate to the directory you want to host your project in, and run: npm install messenger-bot.

Create a new file called app.js and copy-paste in this code, originally taken and modified from messenger-bot’s examples directory.

'use strict'
const http = require('http')
const Bot = require('messenger-bot')

let bot = new Bot({
  token: 'PAGE_TOKEN',
  verify: 'VERIFY_TOKEN'

bot.on('error', (err) => {

bot.on('message', (payload, reply) => {
  let text = payload.message.text

  bot.getProfile(, (err, profile) => {
    if (err) throw err

    reply({ text }, (err) => {
      if (err) throw err

      console.log(`Echoed back to ${profile.first_name} ${profile.last_name}: ${text}`)


Go back to your Facebook app page, copy the Page Access Token, and replace PAGE_TOKEN in your app.js file with your actual token.

Open terminal, run the server, type node app.js, if there are no error messages, it means the server is working.

Since this is a demo using your local server, we can use ngrok to creates a secure public URL to a local webserver on your machine.

Open new terminal and run ./ngrok http 3000. You will see a screen displaying details about the tunnel. Copy the https url forwarding to your clipboard. Please be reminder, if you turn off the ngrok, your bot won’t work either.

Go back to your Product Setup dashboard, to receive messages sent by messenger,the app should enable webhooks integration. Click Setup Webhooks.Paste the https url forwarding to Callback URL , and for Verify Token, you can type VERIFY_TOKEN.

For a demo purpose I just tick message_deliveries, messaging_optins, messages and messaging_postbacks. Click Verify and Save.

Next step, we need to subscribe your app to the page. Open up your terminal and run

curl -ik -X POST “<token>&#8221;

Replacing <token> with your Page Access Token.

If no error, then you can start message your bot. Go to your bot’s Facebook page, click Message and type anyhing. It should echo back whatever you send it.

Yes..your own messenger bot! Try editing app.js and customize your bot.

Thanks for reading.

You can see the video from here.