How to imrpove the response of your Chatfuel’s Facebook 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) is a single hub to build, collect, analyze, manage and leverage voice and chat bots data

Create a conversational app

A 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 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’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, we will need to change Chatfuels default answer. The settings below will invoke a call to 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

  • Set type to “GET”
  • Set “URL” to “{AppID}/Chatfuel/”
    Note: Replace {AppID} with your application id. You can find it in the editor’s URL.
  • Set “USER ATTRIBUTES” to “queryString” and “messenger user id”

3. You can add “Typing” block if you want.

Create a Chatfuel JSON API Function

To connect Chatfuel with’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("", {
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])
return cache.set(userid, response.sessionID, 3600)
.then(function(result) {
return Promise.resolve(true);
  1. 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: response is formatted as SSML, which is not supported by Chatfuel. To make it compatible, we strip the SSML tags.

2. Click Save to apply your changes

Try it out

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

In Chatfuel, click “TEST THIS CHATBOT”, and type the question “are you a bot?”. 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.

Originally published at on January 30, 2018.

Source: Deep Learning on Medium