Isn’t it frustrating that you have to search for a product for hours? What if you could just chat and get your desired product, without having to tear your hair trying to figure out where to look? And what if additionally, you could win 75% cashback by just the click of a button? Way too much too ask right? What if I told you that you could build a chatbot to do this in just 20 minutes,
without a single line of code!
So let’s build a retail Messenger chatbot which chats with customers and offers promo codes and coupons for all products. For our demo, we will build the bot for an imaginary retailer named: AbcElectronix, which sells TVs, Washing Machines, Home Appliances, and other electronic goods.
To begin, here is the flow diagram of our bot.
The tech stack that will be used for this bot is:
Facebook Messenger – Platform to deploy our bot Heroku – For back-end hosting API.ai – For natural language processing and understanding Python 2.7 – To perform back-end tasks Flask – For the web development framework Git – To deploy code on Heroku Virtualenv – To build a clean environment with minimum dependencies
So without further ado, let’s begin.
Getting Started with API.ai:
Begin by watching this 3 min video to get an idea of how API.ai works.
Then, log in to your API.ai account and create a new agent called
PromoBot. Select Intents, and create the following intents:
An Intent is a list of examples of user inputs. Let’s say, to begin a chat, a user might say: “Hi”, “Hi there”, “Wassup”, “Hello” or “Anybody there?”. So these examples are fed in the Intent, and API.ai uses Machine Learning to learn for them. Thus, when we launch the app if the user says “Hey”, API.ai recognizes it because it has learned from similar examples.
Let’s now take a concrete example and develop the
Default Welcome Intent. User says, enter the following examples:
Then, scroll down and under
Text Response, type: “You can win exciting up to 75% cashback offers on any product :)”.
Save, then test it by typing “Hi” in
Try it now…. As you can see our bot detected the intent, then gave the correct response!
Similarly, you can add Text Responses to each Intent mentioned earlier and test them. So until now, using API.ai, we have created a powerful bot that understands the user input and gives an appropriate text response.
Deploying on Heroku:
Download the code from here and extract the zipped file. Now let’s deploy the code on Heroku. First, you will need to install the Heroku Toolbelt. Then log in to Heroku and create a new app:
Name, input the desired name for your bot and then click Create App.
Now return to your terminal and deploy your app on Heroku:
heroku git:remote -a replace_this_with_bot_name
git commit -m “Initial Commit”
git push heroku master
heroku config:set WEB_CONCURRENCY = 3 Important: When you push your changes to Heroku, you will be provided with a URL. Store it somewhere for you will need it in the next step. Launching on Facebook Messenger:
First, start by creating a Facebook Page here. If you have one, ignore, and proceed. Next go to the Facebook Developers Page, under
My Apps on the top right corner select Add a New App. When a dialog box opens, under Display Name ID enter any name you want to be displayed on Facebook. Fill out the rest of the fields and create your App Id. Under Add Products on the left, click on Messenger and get started.
Now let’s set up the webhook. A webhook is basically a URL. Paste the URL you had from Heroku, and under
Verify Token, enter: “my_voice_is_my_password_verify_me”. Aso, tick on messages and messaging_postbacks. Finally, verify and save. Final Steps:
Now, let’s get the
Page Access Token. From the drop-down, select your page, and copy the code generated.
In the files downloaded, open:
promobot.py, and paste the code in for the variable: PAT.
Open API.ai, and click on the gear design on the top left. From
API keys, copy the Client access token and paste it in the code in the quotes of the variable CLIENT_ACCESS_TOKEN.
Finally, add, commit, and push the changes to Heroku:
git add .
git commit -m “Update”
git push heroku master
Now go to your Facebook page, and enjoy chatting!
Quick Note for Developers:
You can customize the bot to your preference. In
promobot.py, there are 3 methods which are key and which you should modify: get_response: this function receives the response from API.ai, understands the Intent (see the section on API.ai), and finally sends the final message to be sent to Facebook. For each Intent, a code has been provided to facilitate communication between functions. Codes are available in promobot.py itself. send_message: this function receives the final message from get_response, decodes to understand which was the Intent, and uses the right template to post the message on Facebook. Templates are types of messages which can be posted on Messenger, such as texts, images, buttons, lists, etc. some_random_template: All functions ending with “template” are specific JSON templates got from the official Facebook Messenger docs.
By modifying just these 3 sections, you can tailor and expand your chatbot to your need!
Good or bad, we’d love to hear your thoughts. Find us on Twitter (
@Bitcot) or write into – firstname.lastname@example.org
Read More Article: