Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Improve the Response of Your Chatfuel Facebook Chatbot

DZone's Guide to

How to Improve the Response of Your Chatfuel Facebook Chatbot

Learn how to properly handle Chatfuel’s default response so that you can return a meaningful response to the user’s requests, improve your bot, and classify requests.

· AI Zone ·
Free Resource

Start coding something amazing with the IBM library of open source AI code patterns.  Content provided by IBM.

Do you use Chatfuel to create your bot on Facebook? Did you know that Facebook reported that Messenger bots failed to fulfill 70% of users’ requests? This quick tutorial will show you how to properly handle Chatfuel’s default response to make sure you can:

  • Return a meaningful response to the user’s requests (even with free text requests)

  • Collect the users’ request to make sure you can improve your bot.

  • Automatically classify unclassified requests. (Sound like an oxymoron? This will be more clear later.)

Create a Conversational App

A Conversation.one application represents your conversational interface through Amazon Alexa, Google Home, or Facebook Messenger bot. To create an app:

  • If you don’t already have a Conversation.one account, sign up. If you have an account, log in.
  • Click on Create New App in the navigation. If it’s your first application, you will be redirected to this wizard automatically.
  • Select your industry and enter your company name. For the purpose of this tutorial, select Other for the industry, and enter Chatfuel Test as your company name. Click Next to continue.
  • Select the devices and services you want to support with your app. For this demo, select Brain, which is Conversation.one’s service that combines deep learning and crowdsourcing to make sure your bot will understand humans. Click Next to continue.
  • Now, we have to create an initial interaction model. For this demo, select Are you a bot? More questions can be configured later.
  • Your new application is ready. Click Customize.

Configure Chatfuel Default Answer

To link Chatfuel to Conversation.one, we will need to change Chatfuel's default answer. The settings below will invoke a call to Conversation.one every time your chatbot does not know how to handle the users’ request.

  1. Add User input block.

    • Set SAVE ANSWER TO ATTRIBUTE to queryString.

  2. Add JSON API block.

    • Set type to GET.

    • Set URL to https://engine.conversation.one/functions/{AppID}/Chatfuel/.

      • Note: Replace {AppID} with your Conversation.one application ID. You can find it in the editor’s URL.

    • Set USER ATTRIBUTES to queryString and messenger user ID.

  3. You can add a Typing block if you want.

Create a Chatfuel JSON API Function

To connect Chatfuel with Conversation.one’s Brain, we will need a backend function that implements the JSON API format. To create the function:

Click Functions and add the code:

const striptags = require('striptags');
const URL = require('url');
const memcached = require('memcached-promisify');
var cache = new memcached();

function Chatfuel(context, req, res) {
    var query = URL.parse(req.url, true).query;
    var userid = query["messenger user id"];

    return cache.get(userid).
    then(function(sessionID) {
        var userText = {
            "userInput": {
                "text": query.queryString
            },
            "type": "input.text"
        };
        if (typeof(sessionID) !== "undefined" && sessionID !== null) {
            userText.sessionID = sessionID;
        }

        return fetch("https://engine.conversation.one/chatbot/{AppID}", {
                method: 'post',
                body: JSON.stringify(userText),
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(function(res) {
                return res.json();
            }).then(function(response) {
                let json = {};
                json.messages = [];
                for (var i in response.output) {
                    if (response.output[i].type == "text") {
                        var out = response.output[i].content.replace(/<\/p>/ig, "\n");
                        var lines = out.split("\n");
                        for (var j in lines) {
                            var message = {
                                "text": striptags(lines[j])
                            }
                            json.messages.push(message);
                        }
                    }
                }
                res.send(json);
                return cache.set(userid, response.sessionID, 3600)
                    .then(function(result) {
                        return Promise.resolve(true);
                    });
            });
    });
}

Click Save to apply your changes.

Try It Out

Now that your Chatfuels default answer is connected to Conversation.one, try out what you have so far.

In Chatfuel, click TEST THIS CHATBOT, and type the question, Are you a bot? Conversation.one will return one of the premade answers. You can easily change those answers later in the dashboard.

Try a similar question to see how the Brain can make sure that your users are served with the best answer. Type Are you human? The system will identify the question as a similar question to "Are you a bot?" and will ask the user to confirm that he intended to ask that question. Confirm by typing yes.

Now, try an unknown question. Type How can I contact you? The system will not find any similar question and will ask the user to repeat the question in different words.

View Your Collected Chatbot Data

To view your collected data, click on the Brain. You will see a table with all the collected samples. For each sample, the system will provide a suggested intent, with a match probability. You can accept or reject the system’s suggestion using the toolbar on the right-hand side.

You should find the question Are you human? classified as AreYouABot and How can I contact you? unclassified. Click Yes to accept the classification for Are you human?

To provide an answer to How can I contact you? you will have to define a new question in the editor. For example, you can use the premade intent Contact.

What’s Next?

You can automate the process of accepting news sample using the Brain configuration under the Settings page. For example, you can decide to accept all high probability samples and run a regression test before deploying the new interaction model.

You can follow these tutorials to learn how to provide complex dynamic contextual answers to your users.

Start coding something amazing with the IBM library of open source AI code patterns.  Content provided by IBM.

Topics:
chatbot ,bot development ,ai ,tutorial ,chatfuel ,conversational app

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}