Connect a bot to Facebook

Your bot can be connected to both Facebook Messenger and Facebook Workplace, so that it can communicate with users on both platforms. The following tutorial shows how to connect a bot to these two channels step by step.

Note

The Facebook UI may appear slightly different depending on which version you are using.

Connect a bot to Facebook Messenger

To learn more about developing for Facebook Messenger, see the Messenger platform documentation. You may wish to review Facebook's pre-launch guidelines, quick start, and setup guide.

To configure a bot to communicate using Facebook Messenger, enable Facebook Messenger on a Facebook page and then connect the bot to the app.

Copy the Page ID

The bot is accessed through a Facebook Page. Create a new Facebook Page or go to an existing Page.

  • Open the Facebook Page's About page and then copy and save the Page ID.

Create a Facebook app

Create a new Facebook App on the Page and generate an App ID and App Secret for it.

Create an App ID

  • Copy and save the App ID and the App Secret.

Save App ID and secret

Set the "Allow API Access to App Settings" slider to "Yes".

App settings

Enable messenger

Enable Facebook Messenger in the new Facebook App.

Enable messenger

Generate a Page Access Token

In the Token Generation panel of the Messenger section, select the target Page. A Page Access Token will be generated.

  • Copy and save the Page Access Token.

Generate token

Enable webhooks

Click Set up Webhooks to forward messaging events from Facebook Messenger to the bot.

Enable webhook

Provide webhook callback URL and verify token

In the Azure portal, open the bot, click the Channels tab, and then click Facebook Messenger.

  • Copy the Callback URL and Verify Token values from the portal.

Copy values

  1. Return to Facebook Messenger and paste the Callback URL and Verify Token values.

  2. Under Subscription Fields, select message_deliveries, messages, messaging_optins, and messaging_postbacks.

  3. Click Verify and Save.

Configure webhook

  1. Subscribe the webhook to the Facebook page.

Subscribe webhook

Provide Facebook credentials

In Azure portal, paste the Facebook App ID, Facebook App Secret, Page ID,and Page Access Token values copied from Facebook Messenger previously. You can use the same bot on multiple facebook pages by adding additional page ids and access tokens.

Enter credentials

Submit for review

Facebook requires a Privacy Policy URL and Terms of Service URL on its basic app settings page. The Code of Conduct page contains third party resource links to help create a privacy policy. The Terms of Use page contains sample terms to help create an appropriate Terms of Service document.

After the bot is finished, Facebook has its own review process for apps that are published to Messenger. The bot will be tested to ensure it is compliant with Facebook's Platform Policies.

Make the App public and publish the Page

Note

Until an app is published, it is in Development Mode. Plugin and API functionality will only work for admins, developers, and testers.

After the review is successful, in the App Dashboard under App Review, set the app to Public. Ensure that the Facebook Page associated with this bot is published. Status appears in Pages settings.

Connect a bot to Facebook Workplace

See the Workplace Help Center to learn more about Facebook Workplace and Workplace Developer Documentation for guidelines about developing for Facebook Workplace.

To configure a bot to communicate using Facebook Workplace, create a custom integration and connect the bot to it.

Create a Facebook Workplace Premium account

Following the instructions here to create a Facebook Workplace Premium account and set yourself as the system administrator. Please keep in mind that only system administrator of a Workplace can create custom integrations.

Create a custom integration

When you create a custom integration, an app with defined permissions and a page of type 'Bot' only visible within your Workplace community are created.

Create a custom integration for your Workplace following the steps below:

  • In the Admin Panel, open the Integrations tab.
  • Click on the Create your own custom App button.

Workplace Integration

  • Choose a display name and a profile picture for the app. Such information will be shared with the page of type 'Bot'.
  • Set the Allow API Access to App Settings to "Yes".
  • Copy and safely store the App ID, App Secret and App Token that's shown to you.

Workplace Keys

Now you have finished creating a custom integration. You can find the page of type 'Bot' in your Workplace community, as shown below.

Workplace page

Provide Facebook credentials

In Azure portal, paste the Facebook App ID, Facebook App Secret and Page Access Token values copied from the Facebook Workplace previously. Instead of a traditional pageID, use the numbers following the integrations name on its About page. Similar to connecting a bot to Facebook Messenger, the webhooks can be connected with the credentials shown in Azure.

Submit for review

Please refer to the Connect a bot to Facebook Messenger section and Workplace Developer Documentation for details.

Make the App public and publish the Page

Please refer to the Connect a bot to Facebook Messenger section for details.

Sample code

For further reference the Facebook-events sample bot can be used to explore the bot communication with Facebook Messenger.