1. Home
  2. Docs
  3. Tutorials
  4. How to use Chatfuel’s default answer to improve the response of your chatbot

How to use Chatfuel’s default answer to improve the response of your chatbot

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 1. return a meaningful response to the user’s requests (even with free text requests), 2. collect the users’ request to make sure you can improve your bot 3. automatically classify unclassified requests (sounds like an oxymoron? this will be more clear later)

Conversation.one is a single hub to build, collect, analyze, manage and leverage voice and chat bots data

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:

  1. If you don’t already have a Conversation.one account, sign up. If you have an account, log in.
  2. Click on Create New App in the navigation. If it’s your first application, you will be redirected to the Create New App wizard automatically.
  3. 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.
  4. 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.
  5. Now, we have to create an initial interaction model. For this demo select “Are you a bot”. More questions can be configured later.
  6. Your new application is ready. Click Customize.

Configure Chatfuel Default Answer

To link Chatfuel to Conversation.one, we will need to change Chatfuels default answer. The settings below will invoke a call to Conversation.one everytime that 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
    1. Set type to “GET”
    2. 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.
    3. Set “USER ATTRIBUTES” to “queryString” and “messenger user id”
  3. You can add “Typing” block

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:

  1. Click “Functions”
  2. 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);
                        });
                });
        });
    }

    Notes on the code above:

    • Replace {AppID} with your application id. You can find it in the editor’s URL.
    • We use memcached as a session manager
    • Line 40: Conversation.one response is formatted as SSML, which is not supported by Chatfuel. To make it compatible, we strip the SSML tags.
  3. 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 TheBrain 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 TheBrain. 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 the tutorials to learn how to provide complex dynamic contextual answers to your users.

Need help?

Feel free to contact us, we are happy to help and answer any question you may have.

Was this article helpful to you? Yes No 1

How can we help?