Build a Retail Chatbot for Promotion in just 20 Minutes

Build a Retail Chatbot for Promotion in just 20 Minutes

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% cashbacks 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.

Tech Stack

The tech stack that will be used for this bot is:

  1. Facebook Messenger – Platform to deploy our bot
  2. Heroku – For back-end hosting
  3. API.ai – For natural language processing and understanding
  4. Python 2.7 – To perform back-end tasks
  5. Flask – For the web development framework
  6. Git – To deploy code on Heroku
  7. 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, login 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 recognises it because it has learned from similar examples.

Let’s now take a concrete example and develop the Default Welcome Intent. In 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 which 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 login to Heroku and create a new app:

In 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:

cd path_to_files_downloaded
heroku login
git init
heroku git:remote -a replace_this_with_bot_name
git add .
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 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 setup 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 customise the bot to your preference. In promobot.py, there are 3 methods which are key and which you should modify:

  1. get_response: this function receives the response from API.ai, understands the Intent (see 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.
  2. 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.
  3. 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!

Author: Raj Sanghvi

Raj Sanghvi is a technologist, innovator and founder/CEO of BitCot, a full service award-winning application development company. With BitCot, Sanghvi has helped over 100 clients develop mobile apps & SaaS platforms, making them affordable and successful. Visit Raj Sanghvi on LinkedIn and follow him on Twitter, @BitCot.View Full Bio

Free project quote

Fill out the enquiry form and we'll get
back to you as soon as possible.
Contact Us: 858-683-3692
Dave S

Dave S

Co-Founder- StompSessions

Quote

I have Known BitCot for 4 years and have been impressed with the diversity and quality of BitCot’s work. With that solid foundation it was really easy to select BitCot as our development partner. Quote

Show Buttons
Hide Buttons