Secciones

How to develop a Facebook bot?

Inicio » Artículos » How to develop a Facebook bot?
La categoría del artículo es
Escrito el 17 August 2016, 06:39


2016 has been the year of bots and you can find them everywhere. Even Facebook launched its own bot by the beginning of the year. From my experience working with it, Facebook API is not the best documented API you can find on the internet. That’s the reason I have decided to create my own article to help other developers.

The documentation I am trying to improve can be found at https://developers.facebook.com/docs/messenger-platform/quickstart


2016 has been the year of bots and you can find them everywhere. Even Facebook launched its own bot by the beginning of the year. From my experience working with it, Facebook API is not the best documented API you can find on the internet. That’s the reason I have decided to create my own article to help other developers.

The documentation I am trying to improve can be found at https://developers.facebook.com/docs/messenger-platform/quickstart

To follow up this article you need to:

- Install node and npm

- Install git

- Install ngrok to easily create a public url that connects directly to a local port in our machine without having to set up a remote machine with its own certificate

- Create a Facebook app or use one you have already created

- Create a Facebook page or use one you have already created

We will be basically connecting a Facebook App with a Facebook Page.

Once you already have all the dependencies installed, lets have some fun.

1.- Start ngrok mapping the port 5000 with the command

ngrok http 5000

The command will output some important you information you need to write down to set up the facebook bot:

You need the forwarding https url.

2.- Clone the messenger-platform-samples project

This project (https://github.com/fbsamples/messenger-platform-samples) contains a nodejs project that will start up a server on port 5000. This application will be the one listening all the messages and replying them properly.

You don’t need to do anything yet with this application.

3.- Add messenger product to your app

Go to your apps dashboard, click on the app you want to use and add the product Messenger (if you don’t have it yet).

Click on Get Started button

At Messenger’s settings, click on Setup Webhooks

Add as the Callback URL the url you wrote down in the first step as forwarding https url and as verify token choose any word (e.g. my\_token)

I would also recommend to add the Webhooks product as it allows you to change the Callback URL.

Why would you need to change the url? Easy. If you are developing your Facebook bot and you use ngrok to generate a public url, every time you restart ngrok, it will generate a new url, unless you have a paid plan.

4.- Generate the Page Access Token for the selected page you want to connect your bot

You will also need this page access token to set up the bot

5.- Subscribe the App to the Page

Select the page you want to subscribe to your bot

6.- Set up your application

Open the file node/config/default.json and fill all the required values:

  • appSecret: you can see the app secret from https://developers.facebook.com/apps//dashboard/
  • pageAccessToken: use the token generated in step 3
  • validationToken: use the word set up as verify token in step 2
  • serverURL: use the https forwarding url you wrote down in step 1 provided by ngrok

7.- Start up the node app

If you run locally the command npm start the message Node app is running on port 5000 will be shown meaning that you can start to chat with your page bot.

The application will only echo your messages for now and now the only limit will be your imagination.


Looking forward your comments...

your_ip_is_blacklisted_by sbl.spamhaus.org.