Click here to Skip to main content
15,861,168 members
Articles / Web Development / ASP.NET / ASP.NET4.0
Article

Creating A Facebook Bot Using Microsoft Bot Framework

Rate me:
Please Sign up or sign in to vote.
5.00/5 (15 votes)
2 Jul 2016CPOL4 min read 85.9K   2.1K   32   29
You can easily create a Bot and deploy it on Facebook.

image

You can easily create a Bot and deploy it on Facebook.

image

To do this, we will use the Microsoft Bot Framework that contains the following components:

  • Bot Connector – A service that connects your bot to communication channels such as Facebook, Skype, and email.
  • Bot Builder – A C# and Node.js library that provides a powerful framework for constructing bots that can handle freeform and guided interactions.
  • Bot Directory – A directory of Bots that you can connect to.

image

The Bot Connector helps you connect your Bot to communication channels. You can write a Bot and expose the Microsoft Bot Framework-compatible API on the internet.

The Bot Connector will forward messages to users, and will send the user messages back to your Bot.

image

To demonstrate this, we will start with the Bot created in the article: Creating a Hello World! Bot Using The Microsoft Bot Framework.

Set-Up Facebook

image

The first step is to log into your Facebook.com account (or create one).

image

Select Create Page.

image

Select a template, fill in the required information, and click Get Started.

image

To get the Facebook Page ID (that you will need later), click on the About tab…

image

… and you will find the Facebook Page ID.

Create A Facebook Developer Account

image

Go to: https://developers.facebook.com/docs/apps/register and click the button to create a Facebook developer account.

image

Click the slider to Yes to accept the Policy and click Register.

Create A Facebook App

image

Next, go to: https://developers.facebook.com/ (log in again if needed).

image

Select Add a New App.

image

Select basic setup.

image

Fill in the information and click Create App ID.

image

Make a note of the App ID, you will need it in a later step.

Click the Show button to display the App Secret. Make a note of it, you will need it in a later step.

image

Now that the App is created, you need to configure it to use the Facebook Messenger.

Click Add Product.

image

Select Messenger.

image

Select Get Started.

image

Facebook Messenger has been added.

You now need to configure it to talk to the Microsoft Bot Connector.

Configure The Callback Url and Verify Token

image

Go to: https://dev.botframework.com/ and Sign In.

image

Select My bots.

image

Select a published bot you created using the directions in the article: Creating a Hello World! Bot Using The Microsoft Bot Framework.

image

Click the Add button next to the Facebook Messenger channel.

image

Click the expander next to Set webhook callback url and verify token.

image

Use the Select buttons to individually select and copy the Callback Url and Verify Token.

image

Return to: https://developers.facebook.com, select your application, and in the settings for Messenger, click the Setup Webhooks button.

image

Enter the Callback Url and Verify Token your copied, check the Subscription fields indicated in the image above, and click the Verify and Save button.

image

After the Webhooks have been set, click the dropdown next to Select a Page.

image

Select the Facebook page you created earlier.

image

Next, click the Subscribe button.

Get Page Token

image

In the Token Generation section, select the page that you previously created.

image

A Page Access Token will be created.

Copy the Page Access Token.

Configure The Microsoft Bot Connector

image

Return to: https://dev.botframework.com/ and select your Bot again, then click the Edit button next to the Facebook Messenger channel.

Click the expander next to Enter your credentials.

Enter the information you gathered in the earlier steps and click the Resubmit button.

image

Check the box next to Enable this bot on Facebook Messenger.

Click the I’m done configuring Facebook Messenger button.

Talking To Your Bot

image

You can now talk to your Bot by navigating to:

https://www.messenger.com/t/{Your Page ID}/

This will take you to a Facebook Messenger, and after logging in with your Facebook ID, it will allow you to converse with your Bot.

image

Until your App is approved and made public, only you can chat with your Bot.

To create test accounts to test it, go to: https://developers.facebook.com/, select the App, and then select Roles and then Test Users.

Fill in the form that displays to create test users.

image

To allow real Facebook users to test it, select Roles and then Add Testers.

Fill in the form that displays to allow access for the users.

image

To make the Bot public, select App Review then click the slider to change it from No to Yes.

image

To request the needed permissions, select Settings under Messenger then click the Request Permissions button.

image

Select pages_messaging and then click the Add 1 Item button.

See this page for more information on the App review process:

https://developers.facebook.com/docs/messenger-platform/app-review

Links

aihelpwebsite.com

 

 

 

 
 
 
 

Microsoft Bot Framework

Facebook for developers

Facebook Messenger (Stack Overflow)

Microsoft Bot Framework messages with buttons in Facebook Messenger

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) http://ADefWebserver.com
United States United States
Michael Washington is a Microsoft MVP. He is a ASP.NET and
C# programmer.
He is the founder of
AiHelpWebsite.com,
LightSwitchHelpWebsite.com, and
HoloLensHelpWebsite.com.

He has a son, Zachary and resides in Los Angeles with his wife Valerie.

He is the Author of:

Comments and Discussions

 
QuestionCallback URL Does Not Work Pin
Member 1358342618-Dec-17 16:31
Member 1358342618-Dec-17 16:31 
QuestionMessenger platform webhooks Pin
Saurabh.abhyankar8-Mar-17 15:15
Saurabh.abhyankar8-Mar-17 15:15 
AnswerRe: Messenger platform webhooks Pin
defwebserver13-Mar-17 20:11
defwebserver13-Mar-17 20:11 
GeneralRe: Messenger platform webhooks Pin
Saurabh.abhyankar13-Mar-17 20:13
Saurabh.abhyankar13-Mar-17 20:13 
GeneralRe: Messenger platform webhooks Pin
defwebserver13-Mar-17 20:26
defwebserver13-Mar-17 20:26 
GeneralRe: Messenger platform webhooks Pin
Saurabh.abhyankar13-Mar-17 21:03
Saurabh.abhyankar13-Mar-17 21:03 
GeneralRe: Messenger platform webhooks Pin
defwebserver14-Mar-17 2:41
defwebserver14-Mar-17 2:41 
Questionconnecting to a remote bot Pin
chint.992-Feb-17 2:50
chint.992-Feb-17 2:50 
AnswerRe: connecting to a remote bot Pin
defwebserver13-Mar-17 20:12
defwebserver13-Mar-17 20:12 
Questionpages_messaging permission? Pin
Brian Colavito30-Nov-16 11:11
Brian Colavito30-Nov-16 11:11 
AnswerRe: pages_messaging permission? Pin
Brian Colavito1-Dec-16 3:44
Brian Colavito1-Dec-16 3:44 
QuestionFacebook webhooks Pin
stuartdunkeld9-Nov-16 1:36
stuartdunkeld9-Nov-16 1:36 
AnswerRe: Facebook webhooks Pin
defwebserver9-Nov-16 4:19
defwebserver9-Nov-16 4:19 
QuestionReplies from bot Pin
atulonweb@gmail.com29-Sep-16 4:19
atulonweb@gmail.com29-Sep-16 4:19 
AnswerRe: Replies from bot Pin
defwebserver29-Sep-16 5:31
defwebserver29-Sep-16 5:31 
GeneralRe: Replies from bot Pin
atulonweb@gmail.com6-Oct-16 22:39
atulonweb@gmail.com6-Oct-16 22:39 
GeneralRe: Replies from bot Pin
defwebserver7-Oct-16 2:40
defwebserver7-Oct-16 2:40 
GeneralRe: Replies from bot Pin
atulonweb@gmail.com11-Oct-16 20:39
atulonweb@gmail.com11-Oct-16 20:39 
GeneralRe: Replies from bot Pin
defwebserver12-Oct-16 2:57
defwebserver12-Oct-16 2:57 
GeneralRe: Replies from bot Pin
atulonweb@gmail.com12-Oct-16 6:05
atulonweb@gmail.com12-Oct-16 6:05 
GeneralRe: Replies from bot Pin
defwebserver12-Oct-16 6:25
defwebserver12-Oct-16 6:25 
QuestionI'll try Pin
XavierRossignol3-Jul-16 4:31
XavierRossignol3-Jul-16 4:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.