Facebook Messenger is a platform that reaches over a billion people world wide. Now that Facebook has opened up access to sending and receiving messages many new opportunities open up.
You can jack up your chat bot’s smarts with two tech buzzwords: natural language processing (NLP) and natural language understanding (NLU). Those two things can make your chat bot not awkwardly ask you again and again what someone just said.
You can read the 5 min quickstart the Wit.ai team prepared but it's really only meant for beginners and not for intermediate hackers.
So instead here is how to add Wit.ai to do real things in 30 minutes.
NLP helps you break down a sentence into things called entities. It’s like primary school for your bot. For example, NLP can read the sentence “Sally sold seashells by the seashore” and figure out there’s an entity Sally, that there’s another entity sold, that there are entities seashells and seashore.
Sometimes just having words in a sentence is enough. You can extract category or keywords and from there you can hack your chat bot to do something. But for more smarts you need to take your bot to the equivalent of university. NLU helps you figure out what that sentence meant. Sally did something with something. And because of that the chat bot can then react and respond based on what Sally did do and with what. In other words, with NLU your chat bot can figure out human concepts like intent or sentiment.
If you have not built a chat bot before then read my other tutorial first: http://github.com/jw84/messenger-bot-tutorial
Be sure you know how to deploy a Heroku server. You can then git clone this repository, running npm install, and get started.
-
Sign up for a Wit.ai account here: https://wit.ai
-
Go clone my weather app: https://wit.ai/jw84/weather
-
Find the app token in your weather app so we can test everything is working. In your app go to the Settings page then find the “Server Access Token” in API Details.
Let’s test first that the token works by running this command in your Terminal:
curl -H 'Authorization: Bearer <YOUR TOKEN HERE>' 'https://api.wit.ai/message?v=20141022&q=what%20is%20the%20weather%20in%20New%20York'
You should get back a response like:
{
"msg_id" : "14735f29-daa9-4553-a259-24cfa8c4ba42",
"_text" : "what is the weather in New York",
"outcomes" : [ {
"_text" : "what is the weather in New York",
"confidence" : null,
"intent" : "default_intent",
"entities" : {
"location" : [ {
"type" : "value",
"value" : "New York",
"suggested" : true
} ]
}
} ]
}
Great success! ✨
-
Git clone my bot here
-
I have organized my chat bot into four major components: the server, the Facebook Messenger connector, the bot app, and the NLP service. It’s a good idea to make chat bots modular. You can progressively add more more components to make the bot understand language or locations but you should also be able to remove components and until only a web server is left.
-
Deploy the bot to Heroku by running these command in Terminal inside the cloned git repo directory. A page should pop up meaning your app is ready. Be sure to remember the URL of your Heroku app.
heroku apps:create git push heroku master heroku open
Setup Facebook to talk to your chat bot
-
Go to https://developers.facebook.com and create a new app, be sure to add Facebook Messenger as a product to the app. You can also use an existing an app.
-
Go to the settings page of the app you’ve made then go to the Messenger settings page. Here you will generate a token of the Facebook Page to link to with Messenger. Remember to save this token key somewhere.
- Set up Webhooks to your FB app. Remember to use the link to your own Heroku app.
Click Verify and Save. You should see the Complete sign!
-
Now we’ll set up the server again. Set the keys required as environment variables to make the bot work by running these commands in Terminal:
heroku config:set WIT_TOKEN='your_token_here' heroku config:set FB_PAGE_TOKEN='your_token_here'
When that’s done you should be able to at least say hi to your chat bot and have it echo back hi! 🤖
Wit.ai does the hard work for you by creating a simple to use interface to manage what are called “stories” — these are ways to extract meaning from a keyword or sentence.
For now you might have to write a lot of stories for your chat bot to understand language but soon Wit.ai has a vision that we can share hundreds of stories with each other. And with more stories the more skills your chat bot will have.
- Create a new story in the Wit dashboard. The first type of story we’ll write is extracting location from conversations. This way we can build a weather bot! Go here https://wit.ai/jw84/weather/stories to see the recipe I’ve created.
- Find the Read function in the bot.js file. Here the bot can recognize messages. The first message can be hello, the chat bot can send back an introduction. Otherwise, we can pass the message on to Wit.ai for processing.
- Let’s test and see if the weather shows up!
-
Create yet another Wit app in the dashboard. This new app will have three stories to extract the category and sentiment entities out of a conversation so your chat bot can reply with some cute pics! Go here https://wit.ai/jw84/cutepics/stories to see the recipe
-
Create a new story to look for the user response then trigger the bot to execute the action and respond.
When you double click a word it will be highlighted as blue, after assigning the word as an entity the highlight turns purple to indicate Wit.ai has learned.
Have the bot execute a merge function to extract the entity and save it to the context object. Then execute the fetch-pics action to return the actual pic itself. Thereafter you can trigger two replies by Wit.ai, one saying what the category is and the other the image link itself.
- Add and create two more stories based on my template. Each will help train Wit.ai on what to look for and how to respond to a sentiment and to an acknowledgement.
-
Now we can test. Be sure to update your Heroku server with the new Wit app’s token by running this command:
heroku config:set WIT_TOKEN='your_new_token_here'
Let’s go back and chat it up. Ask to see some corgis. Then ask to see some racoons!
Congratulations you’ve made a chat bot that’s smart enough to know what you’re talking about, what you mean, and reply back accordingly!
NLP and NLU are not magical! They are merely defining rules for your bot. Your chat bot will break sometimes and maybe even often when being told new and interesting conversations it’s never heard before.
It is now up to you to help keep training and testing your bot until it’s the very best. You have two tools in Wit.ai to do so. Go to the console and let’s learn about the Inbox and the Understanding page.
Wit.ai has an inbox that shows you all the messages it has received. From here you can pick and choose which one to validate and contribute to the training data of your chat bot.
For example, if you have an entity of location you can assign more values to help train your chat bot to know that “San Francisco” and “Timbuktu” are both locations. If you have categories for “corgis” then you can also validate that “dogs” can mean the same thing.
The more people that talk to your chat bot and the more complex your bot is the more you will spend time in this page. And the work you do here will help make your bot even smarter.
The other page you will be spending time at will be in the Understanding page. Here you can try out sentences yourself to test whether your chat bot can understand and to troubleshoot when a sentence doesn’t trigger the right response.
Learn how to get your bot approved for public use here.
Remember, your chat bot has to be approved by Facebook so that anyone can talk to it. Otherwise, you have to go to the Roles page in your Facebook app and add testers.
Go here to learn how to add a chat button your page.
You can use https://m.me/<PAGE_USERNAME> to have someone start a chat.
Read about all things chat bots with the ChatBots Magazine here
You can also design Messenger bots in Sketch with the Bots UI Kit!
I build and design bots all day. Email me for help!
Thanks to https://github.com/hunkim/Wit-Facebook for the inspiration