Send mock messages to Messenger using the Facebook Messenger Platform
For every message we need a recipient and an access token to be used for each call. Luckily there is the option set these as defaults so you do not have to enter them over and over again.
messenger-cli.js defaults <recipient_id> <access_token>
Messages can be send using the send
command within the CLI. If you have set your messaging defaults in the previous step you don't even need to provide a recipient ID and access token.
messenger.cli.js send <type>
You can have a look at which message types are available using the --help
option.
This is dynamically inferred based on which files are present in the messages
directory.
If you want to receive Messenger messages on your local machine you have to set up a Messenger webhook on your Facebook application.
Facebook tries to verify if the endpoint can actually receive messages so we need a local server, which listens to these type of requests and responds to any authentication messages by Facebook.
I recommend using ngrok to pipe messages to your local machine. ngrok can be installed by your favorite package manager (e.g brew on Mac) or downloaded directly through the website here.
- Start a local messaging server
messenger-cli.js server start --verify_token=<random_string>
Make sure to to use this verify token in the Messenger webhook section on your app on Facebook.
- Start ngrok to receive a publicly available address:
ngrok http 8080 -host-header=rewrite -region eu -bind-tls=true
- Enter your public address and verify token under the Messenger webhook section on Facebook.
Facebook now connects with your local machine awaiting an auth response. If everything went fine the dialog closes and the webhook connection is established.
Now you should connect a Facebook page to your app in order to receive it's messages.