The AndyBot server handles communication and coordination between Facebook and Andybot's APIs. It's function is to
- Recieve webhooks from the Andrew Facebook app and record chatbot activity in a Postgres SQL Server
- Send, message send requests to the Facebook Send API
- Present content from
activities.json
configuration file - Make appropriate AndyBot API calls depending on user scans and responses
Background Reading
- Botpress documentation: https://botpress.io/docs/
- FB Messenger Platform Docs: https://developers.facebook.com/docs/messenger-platform
Prerequisites:
- Make sure git is installed
- Make sure you can connect to the database from the current machine
- Ensure that you have NodeJS 8.6.0 or above installed
- Ensure that your current machine is reachable over HTTPS from the internet
Environment Configuration and Important Environment Variables
These are a list of some of the important environment variables.
FB_APP_ID
FB_APP_SECRET
FB_PAGE_ACCESS_TOKEN
FB_VERIFY_TOKEN
BOTPRESS_PASSWORD
PG_HOST
PG_PORT
PG_USER
PG_PASSWORD
PG_BOTPRESS_DB
We use the dotenv module to automatically load the environment variables in the .env file in the root directory. Please make sure you have pasted the correct environment configuration from the environments directory into that file before trying to run the app. These .env files must be kept secure.
Important Configuration Settings
In addition to the environment variables, there are other important configuration parameters in src/config.js
. These include
-
hostname: 'bots.pagekite.me',
This is the URL to which your FB app's webhooks will be configured to point to. Please note that you must not include the protoocol (i.e. http / https) in the hostname. Messenger will always ONLY use HTTPS so make sure your machine can he reached over HTTPS at this hostname. -
staticUrl: 'https://static.andybot.org/static/'
This is the URL for all CDN'd assets like images used by the bot throughout the experience -
apiEndpoint: 'http://localhost:3001'
This is the URL endpoint for the AndyBot API the bot should connect to. Please read more aboutandybot-express
to set this up.
Developer Setup
-
git clone git@github.com:CMP-Studio/andybot-botpress.git
-
npm install -g yarn
-
cd andybot-botpress; yarn install;
-
You should have access to
.env
files forandybot-botpress
. Make sure you paste the contents ofandybot-dev.env
into a file called.env
in the root folder -
cd andybot-botpress; yarn install;
-
node app.js
-
In a new terminal window, make sure you set up your secure web tunnel using pagekite.
-
Go to
localhost:3000
and log in with the usernamestudio
andBOTPRESS_PASSWORD
appropriately -
Go to the Messenger tab.
-
Under "Connection" click "Disconnect / Reconnect" button. Once your configuration is saved successfully you should see a green indicator.
-
Make sure
andybot-express
API is accessible from your bot machine.
Botpress at it's core is an express application. When an incoming webhook is delivered, Botprss triggers a sequence of middleware the executes and responds to this request.
You can add custom handlers using bp.hear
which will trigger your custom handlers.
Directory Structure and Important Files
What is Happening at Startup
When app.js
runs, it starts a botpress server. This is a
little different from the typicial, default botpress setup since
-
Unlike a simple botpress server, in our case
messenger.config.yml
andcontent.yml
are dynamically generated at runtime from their cooresponding template file inside thesrc
folder. This allows us to reuse code, easily reconfigure evironment specific parameters without having to maintain multiple environment specific.yml
files. This happens withinsrc/setup.js
using thejson2yaml
package.- Please keep in mind that there are 2 types of templating syntaxes within
x.template.yml
files that provide for different use cases <%=startupTimeVariableName%>
- this syntax is used to substitute strings within the emitted.yml
file. This templating will only happen once when you start up the app{sendTimeVariable}
- this syntax is used to substitute variables when sending a UMM message block. This templating happens every time a UMM block is sent in the code, for example when you callevent.reply("#some_message_block")
in your bot code.
- Please keep in mind that there are 2 types of templating syntaxes within
-
We do not use
botpress start
to run our bot. Instead, the bot is started manually through the code inapp.js
, which will take care of building our YML templates and setting up Botpress.
Communication with Facebook
FB will send webhook requests when user's message the bot or sent messages have been delivered or read. The bot will also recieve webhooks when users click postbacks, quickreplies or scan entry codes.
Communication with API
When we find the user has taken certain actions, the botpress server will call to notify the andybot-express
API and update the user's state.
- Go to
developers.facebook.com
- Browse to your app, go to the roles tab
- Add user by name or FBID to the testers or developers group
- Go to the business manager and choose the page you are looking for
- Add the name or FBID of the person to the "Editors" group under the page
- Make sure the person accepts their developer invite by going to
developers.facebook.com
How to tell if you're able to test the bot? If you see a blue button that says Get Started on the bot's page, then you have been enabled as a tester. If you see a button that says Message you are not yet a tester.