Choose your Hard!

I ended my last blog with saying that I will let you know what I decided about training last Sunday in this week’s update . . . well, I ended up having a super chilled Sunday and did not train . . …

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Building an Intelligent Chatbot for Slack

A chatbot provides a way for your users to give complex commands and get complex tasks done using simple language.

For example, assume you want to buy a pair of shoes from an ecommerce site that has it’s own chatbot. You can tell the bot that you are looking to buy some shoes, and it would have a conversation with you to gather more details, like “what brand/colour?”, “what’s your size?” and “what kind of shoes? Sneakers or boots?”. Instead of browsing through the website, you will have a conversation with the bot, mirroring the type of experience you would have when you go to the retail store.

Another example of a use case would be Starbucks’ chatbot:

It’s easy to order coffee while conversing with the bot, and then clicking on a bunch of buttons and searching for the orders manually. For these reasons, chatbots are considered to be one of the best ways to engage with the user. Now that we know what a chatbot is, let’s get into the basics. Below is my definition of a chatbot:

In layman terms, a chatbot is a program that interacts with a user on a chat platform. It can be as dumb (fun) as you want it to be and as smart (resourceful) as you require it to be!

There are various ways of creating a chatbot based on what kind of functionality we want it to have. In this tutorial, we are going to build a basic chatbot. Below is a high level design of it:

We will use Slack’s API to send the messages to DialogFlow (DF). DialogFlow’s NLP engine would understand the intent and semantics behind the user’s messages, and craft an appropriate reply for our chatbot to send to the user. The Slack interface and DialogFlow together make the chatbot. To put this in an analogy, the Slack interface would be the “face” of our bot, and DialogFlow would be the “brain”.

For this study, Slack is our platform of choice, as it is used widely across the world for organization level communications. It also has very good support for adding bots through its APIs. Before we can do anything with Slack however, we need to get a few things ready:

A workspace (group) is Slack’s way of organizing teams.you are part of a workspace. A workspace’s URL address looks like this:

You can create your own workspace at this url:

Once you enter your email, Slack will send you a 6 digit code on your email id. Follow their given steps to select your name, password, team size. Then, Slack asks you to provide a name for the group:

You can give it any name you like. I’ll name mine something . Once you select the URL address and create your workspace, Slack will recommend sending email invitations. You can just skip this step by clicking on “Skip for Now” and voila! You just created your own workspace on Slack. Try sending some messages!

Now that you have a workspace to experiment with, you need an application where you can attach your bot. Create the app on the below link:

Click on “Create App” and give a name to the app and select your workspace:

This will redirect you to your app dashboard. From there, you can select the “Bots” option:

Click “Add a Bot User” –> Give a name to your bot. In my case, I have named it “skynet”. Now that we have created a bot for our app, we need to add it to our workspace so we can chat with it! Go back to the above app dashboard and scroll down to find the “Install App to Workspace” option.

Once you do that, Slack will ask you to “authorize” the application. Go ahead and accept the authorization. Now that we have authorized the bot, let’s create a simple rule based chatbot using python.

Before we are able to connect any external program to our Slack bot, we need to ensure that Slack has the right authorization to access the bot. For that, Slack provides an “auth token” that we need to provide when trying to connect with it. Go back to the “app dashboard” and select the “OAuth & Permissions” option:

This will open the permissions settings of the app. Select the “Bot User OAuth Access Token” and save it (I have hidden them for security reasons). This token is instrumental in connecting to our bot.

Now that you have everything set up, let’s write some python code. To make it easy to connect to the Slack API, I have written a small python program. Let’s setup the code environment.

Note: The entire project is in python 2.7.

1.Install slackclient library.

2. Clone my slack repository.

3. Set your slack auth token to the value of “Bot User OAuth Access Token” and the name of your bot in the environment variables.

For example, my BOTNAME is “skynet” so I’ll set it like this:

4. Go to the Slack-AI-ChatBot directory.

5. Start the python bot.

You should now get the below notification:

Now that everything is set up, you can just go to your Slack workspace and start chatting with your bot. Note that it would reply only when you talk to it by using its mention @chatbotname (just like we normally do in Slack).

These are some of the hard coded rules I have preset in the bot. You can change them or add your own rules. If you open the file mainbot.py, you will find the below function:

Now that we have got a taste of building a chatbot for Slack using custom rules, let’s see how can we use the power of NLP and ML to make our bot even more intelligent!

Note: Every time you make changes in the code, you will have to restart the program for the changes to take effect.

Before we proceed, recall the anatomy of our chatbot we discussed right at the beginning of the article. For your reference, below is that diagram:

The first part, which included setting up Slack, has been completed. Now, it’s time to explore the DialogFlow api.

2. You will get access to your console:

DialogFlow works by creating agents. Agents are best described as NLU (Natural Language Understanding) modules. These can be included in your app, product, or service. They transform natural user requests into actionable data.

This transformation occurs when a user input matches one of the intents inside your agent. Intents are the predefined or developer-defined components of agents that process a user’s request.

For example, you tell the chatbot that you are feeling cold, or you want the fan to be turned off. The agent needs to know what the intent is behind these statements. More specifically, “What does the user wants me to do?”. Once the intent is known, a corresponding action/reply can be generated.

Let’s start by creating our own agent. We’ll take the example of a chatbot for a pizza company.

On the top left of the window, you’ll see an option to “Create Agent”. Click on it and select a name for your agent. I have named mine as “pizzaBot”.

Now that we have the agent ready, we need to define some entities it needs to recognize and some intents it needs to understand.

Entities are a group of objects that you need the agent to recognize. For example, if we are creating a pizzaBot, some of our entities would be objects that are usually required with a pizza:

Let’s create our entities.

Now that our entities are ready, we need to define some intents that will help our agent understand what to do with these entities.

In simple words, an intent is a mapping between what the user “says”, and what operation your bot takes on that instruction. If you click on the intent tab, you’ll see that there are two intents already present.

Let’s play around with the default intents. Type “pizza please!” in the demo field on the right and see how the agent reacts:

Since we haven’t trained our agent yet, it used the default fall back intent and responded with one of the many responses it has stored for that scenario. Let’s create an intent to cook a pizza and call it “makePizza”. Click on create intent, select a name, and save it. You will see something like this:

The way DialogFlow works is by taking some sample user sentences to start out with. Then, it trains it’s engine to generate an algorithm that best matches these sentences to the correct intents. Let’s add some basic training phrases for our agent:

The idea is to train our agent in as diverse and varied examples as possible to make it more precise. Once you have set up the training phrases, you need to define what action the agent should take if it encounters this intent. You can do that by clicking on the “Add Parameters and Action” option. Add the following actions and parameters:

What do these column names mean?

Click on “Define prompts” next to toppings and write “What toppings would you like on your pizza?”

Similarly, add one for cheese. Wow. That was a lot of work. Let’s see what we achieved. Save the intent and get back to the demo area of the agent. Try asking the agent for a pizza. Since we have trained the agent on a few sample phrases, it should be able to immediately recognize any phrase related to ordering a pizza. It should also ask you for the toppings and for the type of cheese you want? Checkout the following video to see what I’m talking about.

Notice how natural the conversation feels. Once you trigger an intent, the agent tries to extract the parameters you have marked as important from the user by having a conversation. Note that you only need to give a few examples for the training phrase; the NLP engine of DialogFlow takes care of the rest.

Also, you can add custom responses for the user in case the intent is successfully acted upon and completed. Click on the “Add Responses” button below the “Action and Parameters”, add the following response, and save it:

Once your pizza is ready, you’ll be greeted by the below message:

What happened behind the scenes? When we used $cheese and $toppings in the response field, they were automatically replaced by the values the parameters extracted from the user’s conversation. Convenient, isn’t it?

Now that everything about the agent is set up , it’s time to train it. Click on the settings icon next to your agent name.

In the settings window, select “ML Settings”.

Here are few important things to note:

Click on the “Train” button and once the agent is trained, save it. Now our NLP agent is good to go. Let’s go to the next step and integrate this with our Slack app to make the chatbot complete!

DialogFlow (DF) is an excellent choice when it comes to integration with most popular applications. It supports integration with Slack right out of the box. Let’s get our DF agent to work with our Slack bot!

On the left side of the window, click on the “Integrations” button and you will see a bunch of options. Select the Slack icon and click settings beneath that. The below box will pop up:

On the top right hand side, toggle on the switch to “turn on” the integration with Slack.

Scroll down and you will find fields to enter your Slack app’s credentials:

For that, head back to your Slack app’s console. If you don’t have it opened already, just go to the following URL and select your app:

Scroll down on the page and you’ll find your app’s credentials (I have hidden them for security reasons).

Copy them to the respective fields in the DialogFlow page.

Note: To copy the “Client Secret”, you have to first click on the “Show” option button next to it. After you have copied the credentials, click “Start” on the bottom right of the Slack box in DialogFlow.

Now that you’ve set up Slack and Dialogflow, you’ll need to enable OAuth (helps in authentication), Event Requests URLs (provided by Dialogflow), and Event Subscriptions (what kind of events does our bot listen to?).

After a few moments you should see a green “Verified” text above the Request URL field.

Event subscription is the most crtitical thing for the chatbot. It basically tells Slack what kind of events our bot will listen to and get triggered. Events can range from someone joining a channel to a new personal message.

Once you are done with this step, you can go back to your workspace and start conversing with your bot!

Go to your workspace and either personal DM the bot, or mention his name like @botname and it will start responding.

Did you notice?

That apart from answering the normal pizza questions, the bot is also good at replying to generic messages like “hi”, “hello”, and “thank you”? This is possible because of a DialogFlow feature called “SmallTalk”.

If you click on the “Small Talk” button on the left pane in DialogFlow, you’ll come across a set of options:

Small Talk let’s you add how your bot should react when it receives generic comments like “Hi, Hello” or general questions like “Who are you?” and “What can you do?”. Go ahead and set it up with details specific to your project.

Add a comment

Related posts:

SHRIEKS THROUGH THE FOREST

The leaves of the cicada waved gently in the wind. The sun was bright and cheery, the mountain still. The squirrels were napping. They had enough to last them the coming winter. Their impulses urged…

Christianity Has a Heaven Problem

The consequence of a church with its eyes primarily set on the next world is that it fails to answer the problems of this one with anything substantial other than eventually your suffering will end.

Leadership Starts from the Ground Up

Leadership is a concept that calls to mind a wide variety of definitions in the minds of many individuals and is also a highly misunderstood concept in many cases. It is quite natural to assume that…