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.
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:
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:
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:
- Callback URL: The Heroku URL we copied in STEP 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
- 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.
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.
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+.
if you really enjoyed this post please share this post to your friends so they learn something new and grow in digital marketing. Your valuable comments are oxygen for me.
SHARING IS ♥️