Connect a bot to Facebook
You can configure your bot to communicate with people through Facebook Messenger or Facebook Workplace. This article describes how to create a Facebook app using the Meta for Developers site, connect your bot to your Facebook app in Azure, and test your bot on Facebook.
This article shows how to add the Facebook channel to your bot via Azure portal. For information on how to use a custom channel adapter, see Additional information.
Prerequisites
- An Azure subscription. If you don't already have one, create a free account before you begin.
- A bot published to Azure that you want to connect to Facebook.
- A Facebook for Developers account. If you don't have an account, you can create one at developers.facebook.com.
- A Facebook page from which users will access your bot. If you don't have one yet, Create a new Page.
- To use Facebook Workplace with your bot, you must create a Workplace account and a custom integration to connect the bot to.
Create a Facebook app
Users will access your bot from a Facebook Page. To connect the bot, you'll enable Facebook Messenger on the Facebook Page and then connect the bot to the Page.
Create your app
- Sign in to your Meta for Developers account.
- Go to Create a new Facebook App.
- On the Select an app type page, select Business and then Next.
- On the Provide basic information page, enter a name for your app and select Create app.
- If prompted, enter your password and select Submit to create your app.
- After your app is created, the site goes to a page for your app.
- Expand Settings and select Basic.
- Copy and save the App ID and App Secret.
- Now under Settings, select Advanced.
- In the resulting pane, scroll down to the Security settings, and enable Allow API Access to app settings.
- Select Save Changes.
Enable Messenger
- Select Dashboard.
- In the resulting pane, scroll down to the Messenger tile and select Set Up.
- The site adds Messenger settings to your app and displays the settings page.
Add pages and generate tokens
Under Messenger, select Settings.
Scroll down to Access Tokens and select Add or Remove Pages.
- When prompted for the identity to associate with Messenger, either continue with your current account or sign in to another.
- When prompted for the Pages you want to use with your app, select the pages and then select Next.
- If prompted to submit the request for Login Review, review the information and select Done.
- On success, the site displays a success page. Select OK to continue.
The Page you added now appears in the Pages list.
Copy and save the Page ID for later.
Select Generate token for the Page.
- The site displays security information and gives you a chance to copy the token.
- Read and acknowledge the warning.
- Copy the token and select Done.
You now have your app information and a token for the next step. Leave the Facebook setting steps pending. You'll update them later.
Configure your bot in Azure
To let your bot send messages and other events to Facebook Messenger, enable webhooks integration.
- In a new browser window, go to the Azure portal.
- Open the Azure Bot resource blade for your bot.
- Open Channels and select Facebook.
- In Configure Facebook Channel, enter the Facebook information you copied in the previous steps.
- Enter your Facebook App ID and Facebook App Secret.
- Enter your Page ID and Access Token.
- Copy the generated callback URL and verify token values.
- Select Add.
Tip
- If you don't have a copy of your information from the previous steps, you can retrieve it for the Meta for Developers site.
- If you need to, create a new web token for the page. For instructions, see Add pages and generate tokens.
Enable webhooks
Go back to the Facebook settings to finish up the configuration process.
- On the Meta for Developers site, go back to the Messenger settings page for your app.
- In the resulting pane, scroll down to the Webhooks section and select Add Callback URL.
- On the Edit Callback URL page:
- Enter the callback URL and verify token values that you copied from the Azure portal.
- Select Verify and save.
- The Page you added now appears in the Pages list under Webhooks.
- Select Add subscriptions for the Page.
- On the Edit page subscriptions page, select the following subscription fields:
- messages
- messaging_postbacks
- messaging_options
- message_deliveries
- Select Save.
- On the Edit page subscriptions page, select the following subscription fields:
- The site displays the added subscription fields next to the Page for your bot.
Make your app public
Until an app is published, it's in Development Mode. Plugin and API functionality will only work for admins, developers, and testers.
Only the creator (the Facebook Dev account that created the page and bot) can get a bot response. Normal Facebook users can't see the page or the bot. Give dev or test roles to target users, so they can also chat with bot.
Users to be added to tester roles must first register on the Meta for Developers site. The tester role is not available to Facebook users that don't have a Meta for Developers account. For more information about app roles and test users, see the Meta for Developers developer documentation.
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's compliant with Facebook's Platform Policies.
Make the app public and publish the Page
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.
Set the API version
If you receive a notification from Facebook about deprecation of a certain version of the Graph API:
- Go to Meta for Developers.
- Go to the app you created for your bot.
- Under Settings, select Advanced.
- Select Upgrade API version, then switch Upgrade All Calls to version 4.0.
Test the connection by following the steps described in the Test your bot in Facebook section.
Test your bot in Facebook
You can now test whether your bot is connected to Facebook correctly by sending a message via the Facebook Page you associated with your new Facebook app.
- Go Facebook and switch to the profile for your page.
- Select more actions (…), then Add Action Button.
- In the Customize your action button dialog, select Try it and follow the instructions.
- On the Action Button page under Get people to contact you, select Send Message.
- Select Next, complete the dialog and save your changes.
- Switch back to your personal profile.
- Go to your page, and select Message to test the connection to your bot.
You can also test this feature using the sample bot for the Facebook adapter by populating the appsettings.json file with the same values described in the previous steps.
Additional information
See the Bot Framework C# Facebook-events sample for a sample bot that supports Facebook Messenger communication.
For Facebook Messenger documentation, see:
For Facebook Workplace documentation, see: