First, we'll create a way to securely store your Facebook Page's access token.
Get started by running EDITOR=nano rails credentials:edit
inside of your Whales Terminal at http://localhost:7331.
This is a terminal-based text editor that you can navigate using your arrow keys. Go to the bottom of the file and type in the following:
facebook_token:
Provide a space after facebook_token:
and paste in your Page Access token from the last step (in the Token Generation section of your bot's Messenger settings, as below:)
You can click this field to copy it.
After you paste this in, you may see that there's a newline between facebook_token:
and your token. You should use your arrow keys and your Delete key to remove this newline; it should be formatted to look like the below:
Notice how there's no newline between facebook_token:
and the beginning of the token (EAAKX...
)
Type Ctrl-O and hit Enter in the prompt to save your file, and type Ctrl-X to exit the nano
editor.
Your terminal should say New credentials encrypted and saved.
This means that we can now securely access your Facebook access token with: Rails.application.credentials.facebook_token
.
To allow your bot reply, you'll have to construct a request to Facebook API to send a message.
First, check out app/controllers/main_controller.rb
to see the code you need to fill out for this step.
You need a few key ingredients:
In the protected send_message
function:
conversation_id
and some text for what gets sent. You can check out Facebook's documentation below for more information on what fields you can pass.In your receive
function of the main controller:
send_message
with some information from what you are receiving from Facebook's request to our server.Information about Facebook's Message sending API can be found here: https://developers.facebook.com/docs/messenger-platform/send-messages#send_api_basics
conversation_id
conversation_id
in the protected send_message
function refers to an ID that is given to you as a part of Facebook's request to your server.