Skip to content

Psiphon-Inc/fb-messenger-bot

Repository files navigation

fb-messenger-bot

This project is based on Facebook's Messenger Bot project and was initially setup following this guide https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup, information from that guide was used as a starting point to create a dockerized and customized version of the Facebook Messenger Bot.

Prequisites and Installation

In order to run this container you'll need docker installed and all dependencies be provided as described in the following sections.

Install Docker

Check the official Docker documentation for information how to install Docker on your operating system. And then install Docker and supporting tools.

Clone this repository

Clone this repository to your server.

Create .env File

Certain variables which are considered secret are not set in these files and are represented by variables. You will need to create a .env file in your project directory and define the following variables:

PRIVKEYPATH
CERTPEMPATH
FULLCHAINPEMPATH
PORT
CERTPATH
ACCESSTOKEN
WEBTOKEN

You may not know yet what to put for these variables, that will be defined in the next steps.

Facebook Page and Facebook App

You will need a Facebook Page and a Facebook App. Information on obtaining these can be found in Facebook's documentation here.

You will get your ACCESSTOKEN from Facebook. Your Verify Token or WEBTOKEN is needed in this step as well, the Verify Token is explained in step 4 of this article. It is a random string of your choosing.

The ACCESSTOKEN and WEBTOKEN variables can now be defined in your .env file.

Obtaining a domain name and certificates must be done along with setting up the Facebook App.

Domain Name and Certificates

Your Facebook Messenger server needs a domain name. Get a domain name from a domain registrar.

Update an A record for your domain so the domain you just obtained points to the public IP address of your Facebook Messenger Bot server. This is done on the domain registrar's website ususally.

It can take up to 48 hours for the DNS record to propogate, but is usually pretty quick. Once it has propogated, you need to obtain a certificate. You can get a certificate from Let's Encrypt for free. You may need to open port 80 on your firewall if it is not currently open.

Now you can request the certificate from Let's Encrypt by running:

sudo certbot certonly --standalone

It is possible that this command will be different depending on the OS you are running, Certbot provides instructions here.

Certificates should be created for your domain now. They will need to be renewed before 90 days when they expire. It is recommended to renew after 60 days.

When you are finished close port 80 if it is not needed.

You can now define the variables PRIVKEYPATH, CERTPEMPATH, FULLCHAINPEMPATH, and CERTPATH in your .env file.

Define port

Choose the port you would like to use for your application, you will need to give the port to Facebook in order for Facebook to connect to your server. You will also need to give the .env file the PORT variable.

Customization

You can customize the messages your bot will send by editing the template-messages.json file, change the messages to what you would like them to say, and rename the file messages.json. This file contains the responses, options and messages that the bot can send to users. index.js gets the messages from this file to send.

Build

Once you have setup everything you will need to build your docker image. The Dockerfile uses two args.

ARGS

The Dockerfile uses the following: ARGS

PORTNUMBER
CERTPATH

These must be passed to the Dockerfile as build args. Your chosen port will be PORTNUMBER and the path to your cert files will be CERTPATH.

To build:

docker-compose build --build-arg PORTNUMBER=${PORTNUMBER} --build-arg CERTPATH=${CERTPATH}

Docker-compose

Once you have built the image, you can run with:

docker-compose up 

or, if you would like to background the process:

docker-compose up -d

Optional:

It is possible to use the same messenger bot for two or more Facebook pages in different languages. Included in the repo are two optional files: twoPages_index.js & messages-templates-twopages.json. The templates use an English page and Spanish second page as examples.

If you wanted to use the same bot on two pages, you should substitute these two files for the index.js and messages-template.json files in the setup steps. You will also need to log into the messenger bot app dashboard on the Facebook developer website and navigate to the Messenger settings. From here you will have to add subscriptions for each additional page you want the app to work with.

About

Code for the Psiphon Facebook Messenger Bot

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published