This documentation guides you through setting up and using a Slack-Facebook integration. The integration automatically sends a message to a designated Slack channel (facebook-updates
) whenever a new message is received in your Facebook inbox.
Before using this integration, ensure you have the following prerequisites:
-
Slack Access Token: Obtain a Slack access token issued to your Slack application. Each application installation generates a workspace-specific access token.
- Create a new Slack application following the steps in our Basic app setup documentation.
- Set the necessary permissions your app will request as per our scopes documentation.
- Install the app to your workspace as outlined in the installation guide.
- Obtain your access token by going to api.slack.com, selecting your app, navigating to the OAuth & Permissions section, and copying the 'Bot User OAuth Token'.
- Create a Slack channel named
facebook-updates
and add your app to this channel.
-
Facebook Token: Use any hash string as your Facebook token.
-
Channel ID: Obtain the channel ID of the
facebook-updates
channel on Slack.
Follow these steps to install and set up the integration:
-
Clone the Repository:
git clone https://github.com/DhawalAgrawal/fb-updates-slack
-
Install Dependencies:
cd fb-updates-slack yarn install
-
Configure Environment Variables:
- Create a
.env
file from.env.example
. - Set the following values in the
.env
file:SLACK_TOKEN=your_slack_auth_token FACEBOOK_TOKEN=your_facebook_hash_string CHANNEL_ID=your_facebook_updates_channel_id PORT=3000
- Create a
-
Download and Install ngrok:
- Visit ngrok's download page and download the ngrok executable for your operating system.
- Install ngrok by following the installation instructions provided for your OS.
-
Sign Up/Login to ngrok:
- Go to ngrok's website and sign up for a free ngrok account or log in if you already have an account.
-
Authenticate ngrok on Your Machine:
- Open a terminal or command prompt window.
- Navigate to the directory where ngrok is installed or place the ngrok executable in a directory accessible from your command line.
- Authenticate ngrok on your machine using the command:
Replace
./ngrok authtoken <your_ngrok_auth_token>
<your_ngrok_auth_token>
with the authentication token obtained from your ngrok account dashboard.
-
Start ngrok to Generate a Public URL:
- In your terminal or command prompt, start ngrok to generate a public URL for your local server where your Facebook Messenger webhook will be hosted. Use the command:
Replace
./ngrok http <port_number>
<port_number>
with the port number where your local server (hosting the Facebook Messenger webhook) is running, typically port 3000 or a custom port if specified.
- In your terminal or command prompt, start ngrok to generate a public URL for your local server where your Facebook Messenger webhook will be hosted. Use the command:
-
Copy the ngrok Forwarding URL:
- After starting ngrok, you will see a console with information about the tunnel, including a Forwarding URL (e.g.,
https://randomstring.ngrok.io
). Copy this URL as it will be used as your webhook URL in the Facebook Developer Dashboard.
- After starting ngrok, you will see a console with information about the tunnel, including a Forwarding URL (e.g.,
-
Access Facebook Developer Dashboard:
- Go to Facebook for Developers and log in with your Facebook account.
-
Create or Select Your App:
- If you haven't already created a Facebook app for your integration, create a new app in the Facebook Developer Dashboard.
- If you already have an app, select it from the dashboard.
-
Set Up Webhooks:
- In your app dashboard, navigate to the "Webhooks" section.
- Click on "Add Callback URL" or "Set Up Webhooks."
- Paste the ngrok Forwarding URL copied earlier into the "Callback URL" field.
- Set the verify token (set 'your_facebook_hash_string' ) in the "Verify Token" field. This token will be used to verify requests from Facebook.
- Select 'messages' in the types of events you want to subscribe to.
- Save your webhook settings.
-
Subscribe Your App to the Facebook Page:
- In the same "Webhooks" section, you can also click on "Subscribe" next to the page event 'messages' to subscribe it.
- Check the scope has 'chat:write' iff not select it.
-
Verify Webhook Connection:
- After configuring the webhook in the Facebook Developer Dashboard, Facebook will send a verification request to your ngrok URL.
- Ensure your local server is running and can handle verification requests by responding with the correct challenge code provided by Facebook.
-
Test with Facebook Messenger:
- Use another Facebook account or a test account to send messages to the Facebook page associated with your app.
Check your ngrok console for incoming webhook requests and verify that your integration is receiving and processing messages correctly.
Once everything is set up:
-
Start the application:
yarn start
-
Go to your Facebook page and send a message from another account.
-
You will start receiving messages from Facebook in the
facebook-updates
channel on Slack.
You are now ready to use the Slack-Facebook integration.
Note: Ensure all sensitive information such as tokens and IDs are kept secure and not shared publicly.