Effective Step By Step Guide To Build A Facebook Messenger Chatbot

 

There are over a billion people on Facebook which clearly makes it the largest chat platform in the world. Messenger bots are the newest entrant in the digital marketing world and the digital marketers are using this platform for generating the required number of leads for their business.

Build A Facebook Messenger Chatbot

 

In this Facebook chatbot tutorial, we will learn how to Build A Messenger Chatbot using Python. For web request handling we will use Flask and use Heroku, we will deploy our app.

Also Read  : Ad Spy Tool Adplexity Review And Coupon Code

Step By Step Guide To Build A Facebook Messenger Chatbot

STEP 1: We will start by creating a Webhook Endpoint

To verify the bot with our Facebook profile, we will require creating a webhook endpoint which could at least return 200 response codes.

In order to create a working endpoint, you will need to git clone Github repository:

git clone [email protected]:hartleybrody/fb-messenger-bot.git

Install the Python dependencies:

mkvirtualenv test-bot

pip install –r requirements.txt

The Flask web app you created can be deployed to any server but for simplicity we choose Heroku.

It is necessary for you to have Heroku CLI Toolbelt installed.

Run heroku create the application setup.

Cross check that Heroku you created run things locally on your machine by starting your local server with the command:

heroku local

You will see ‘Hello World’ written in your browser when you visit http://localhost:5000/

Press ctrl+C to kill the local server.

To deploy the working endpoint to Heroku –

git push heroku master

Type heroku open to open it in your browser.

You have now created a working webhook endpoint. The URL will help you in setting up your chatbot.

NOTE: Copy the full https://*.herokuapp.com URL as you will need it.

STEP 2: Creating a Facebook Page

It sounds a little uncanny that you don’t have a Facebook page. But if you really don’t have one, then create a Facebook page. Your bot will be integrated with your Facebook page as it will use your identity including the image and the name.

People will require a communication medium to interact with your chatbot and a Facebook is one.

STEP 3: Creating an App on Facebook

Create a Facebook app using Facebook Developer’s Quickstart Page.

Go to Create App ID, here you can create a Facebook App for your chatbot. You will require providing category, name, and email for your app.

When all the information is provided, click on Create App ID on the bottom-right corner.

Product setup page will open. Scroll down and you will see the Messenger section. Right next to it is a ‘Get Started’ button, click on it.

STEP 4: Setting Up the Messaging App

This is the stage where your Heroku endpoint will be wired with your chatbot. So, fill out the things properly and carefully.

Generate a Page Access Token –

On your Facebook page, you will see an ‘auth flow’ tab. When you click on it, a Token Generation pop-up window will appear. Here you can generate a Page Access Token for your application.

While copying the Page Access Token to your clipboard, you have to make sure that the system you provide it is environment variable.

On the command line, run;

heroku config : add PAGE_ACCESS_TOKEN=your_page_token_here

The token you created will help you in authenticating your requests every time you send a message.

Setting Up the Webhook –

While you are setting up the webhook, you will require filling out certain details:

  1. Callback URL: The Heroku URL we copied in STEP 1.

 

  1. Verification Token: When you are generating the Page Access Token, it will send you a Verification Token to your bot. place the token in your Heroku environment.

heroku config : add

VERIFY_TOKEN=your_verification_token_here

  1. Subscription Fields: It states all about the messaging events you care for. Simply put it to ‘messages’ for starters. You can change it later.

After configuring the webhook, you will have to subscribe to a particular page in which you want to receive all your notifications.

Once you have subscribed to a specific page, you have to keep the VERIFY_TOKEN and PAGE_ACCESS_TOKEN config values in the Heroku environment.

STEP 5: Start Chatting with your Chatbot

Open your Facebook page, click on the ‘Message’ bar on the top-right corner. A chat box will open.

Send your page messages, the bot will reply!

You can check the logs of your application using the command line, run:

heroku logs -t

Whenever a new message is sent to your chatbot, POST data is generated.

For example; the JSON POST body would look like this for the message “hello world” sent to the chatbot.

{

“object”:”page”,

“entry”:[

{

“messaging”:[

{

“message”:{

“text”:“hello world”

“seq”:20,

“mid”:”mid.1466015596912:7348aba4de4cfddf91″

},

“timestamp”:1466015596919,

“sender”:{

“id”:”885721401551027″

},

“recipient”:{

“id”:”260317677677806″

}

}

],

“time”:1466015596947,

“id”:”260317677677806″

}

]

}

STEP 6: Customizing the Bot’s Behavior

This is the most necessary part. The two main functionality of your chatbot will be to send and receive messages.

Sending Messages –

You will require two things for sending text messages.

  • Facebook ID of the recipient
  • The text message

For example; we have created a simple send_messages() function.

This function will send information via automatically hitting the Facebook API.

Receiving Messages –

The JSON POST data is loaded from Facebook which is later sent to the webhook whenever a messaging even is prompted.

It happens when we are on the messaging receiving end.

As discussed in STEP 4, we have assigned Facebook to a specific type of message that we want our webhook to notify.

STEP 7: Submit the Application to get reviewed

Before you make your chatbot available to the world, you have to go through a thorough review process in order to determine the flaws of it.

At this point, you and other admins can message with your chatbot. The code of the bot runs on the local server of your machine and can change anytime without giving prior notification to Facebook.

However, if you tend to change the code of the bot, then Facebook will revoke your access token API.

For stopping this from happening, go back to STEP 4. In the ‘App Review for Messenger’ section click on ‘Request Permissions.’

You can request any permission you want. Once you have requested the required permission, you will be redirected to ‘Review Status’ page. Here you will have to fill out an array of details and information in order to make sure that no one will abuse your platform in the future.

Quick Links:

Conclusion – Build A Facebook Messenger Chatbot

This Facebook chatbot tutorial will help you in creating a basic messenger chatbot using python. Do follow our steps carefully in order to get effective results. We hope this post suits your purpose well. Feel free to share this post on trending social media platforms like Facebook, LinkedIn, Twitter and Google+.

Recommended Hosting

CloudWays
Features
Support
Pricing
Super fast

Pros

  • Special Offer Pay As You Go
  • Reliable WordPress Hosting
  • Top Notch Customer Support
  • Fast & Convenient

Cons

  • Basic Technical Knowledge Required
About Jitendra Vaswani

Jitendra Vaswani is the founder of SchemaNinja WordPress Plugin, prior to SchemaNinja he is the founder of big internet marketing blog BloggersIdeas.com. He is successful online marketer & award winning digital marketing consultant. He has been featured on HuffingtonPost, BusinessWorld, YourStory, Payoneer, Lifehacker & other leading publication as a successful blogger & digital marketer. Jitendra Vaswani is also a frequent speaker & having 5+ yrs experience of in Digital Marketing field. Check out his portfolio( jitendra.co). Find him on Google+, Twitter, & Facebook.

2 Comments

  1. Rajhansh Rai

    Hello..Awesome content, nice post, very factual and informative.
    thank you for sharing …

    Reply  
  2. Prasun Shakya

    Thank you Jitendra. Your article always helpful for someone who is new into blogging.

    Reply  

Leave a Reply

Your email address will not be published. Required fields are marked *