Start building your own chatbot now

Chatfuel offers a great and easy to understand environment to build simple bots, really fast. Since the bot’s actions are triggered with button clicks, it’s incredibly easy to mockup a conversation flow and deploy it to Messenger.

Yet, experienced bot builders will tell you: the actual user inputs your bot will get are impossible to predict exhaustively, sometimes absurd and quite often senseless. A pure input-output logic will sadly not get you very far.

This is precisely the reason why NLP is essential for a chatbot to be able to react to unpredictable inputs. Hence, if you ever have shipped an application to Facebook, you know that this will happen very, very often:

How to add NLP to chatfuel's bots

A chatbot failing at smalltalk

Some people just won’t click the buttons and type weird questions instead. To give you some perspective, one of the most triggered intent on one of customer support chatbot for a bank is @ask-joke. You would think people have better things to ask to their digital bank clerks – the reality is that most users want both: the facts and the fluff.

Knowing this reality is one thing, handling it is another. If you fail to handle fluff in a satisfying manner, you’ll leave your users deeply frustrated. The good news is that integrating Natural Language Processing with SAP Conversational AI to your existing Chatfuel bots only takes minutes, and I’ll guide you along the path.

What are we building today?

By the end of this tutorial, your existing bot will be able to understand more than 15 different topics of small talk and answer it cleverly, and you’ll have everything you need to add much more – all that in less than 10 minutes. So, without further notice, here’s what on the menu today:

  1. Setting up a SAP Conversational AI account
  2. Training your bot to understand and respond to small talk
  3. Interfacing SAP Conversational AI and Chatfuel
  4. Setting up a fallback on Chatfuel

Find a comfortable spot, and get the stopwatch ready. Now it is the time to plug a SAP Conversational AI brain into your Chatfuel’s bot!

Setting up your SAP Conversational AI account

  1. To begin, create an account on SAP Conversational AI! It’s free 🙂
    start-building-bot-code
  2. It’s now time to log in and create your first bot. Click on the button “Start with a template”, or just on + NEW BOT in the header section

    Add NLP to Chatfuel bots

    Create your first bot on SAP Conversational AI

  3. Choose “Create a complete chatbot”
  4. You can choose one or many pre-defined skills to generate a boilerplate for you bot. Pick Greetings and Small Talks
  5. Choose your bot name
  6. You can add an optionnal description
  7. Set English as the default language
  8. You can keep your bot public as there is no private info, but you can change this setting later

checkmark Step 1/4 – Set up a SAP Conversational AI account

25%

Train your bot

The train tab is the default view on your bot, and it displays its intentsAn intent is a “box” of sentences that all carry the same meaning, even though they can be very different to one another. When a user sends some text to your bot, our algorithm compares it to the phrases in your intents. Then it checks if it’s close enough to one of them and decides what the intention of the message is.

Because we started with a boilerplate you will already see a lot of intents:

Give Chatfuel a brain

Overview of your basic intents

This means that your bot already knows how to recognize 16 different meanings in hand typed messagesIf you want to learn how to create new intents or improve the existing ones, I invite you to give a look at this tutorial.

Build

The Build tab is where you find Bot Builder. It helps you construct the conversation flow of your bot using SkillsEach Skill represents one thing that your bot know to do, and they can interact with each other.

You can already see the two default skills, greetings and small-talk picked during the creation of the bot.

What’s a skill?

A skill has four parts:

  • Readme: Where you explain the purpose of your skill
  • Triggers: Where you define why this skill should be activated after a user message
  • Requirements: Describes what information this skill has to collect, and what questions need to be asked to fulfill the requirements.
  • Actions: What to do once the requirements are fulfilled

The two premade skills are very simple: they are triggered if the user message contains a certain intent and answer with a message depending on the intent. The skill greetings takes care of the intents greetings and goodbye, and the skill small-talk handles the 14 others.

If you want to edit the default skills, go to their Actions tab and click the pen to change the content of the message.

The setup part on SAP Conversational AI is now done, you can test your bot by clicking the blue Test bubble on the right of the screen or conversing with it by clicking on the Chat with yout bot bubble on the bottom-right corner;

checkmark Step 2/4 – Train your bot

50%

Interfacing SAP Conversational AI and Chatfuel with a Node.JS Proxy

Unfortunately, SAP Conversational AI and Chatfuel don’t speak the exact same language. The JSON output from SAP Conversational AI’s Bot Builder is not formatted exactly as Chatfuel API expects.

Let’s code a simple NodeJS proxy to solve this issue. We’ll set up a simple HTTP server that receives events from Chatfuel, calls SAP Conversational AI’s Bot Builder API with the message sent by Chatfuel and returns the messages configured in your Skills.

Start by installing dependencies:

npm install express axios

Then copy the following script to a .js file.

const express = require('express');
const url = require('url');
const axios = require('axios');

const PORT = 5000;
const SAPCAI_REQUEST_TOKEN = 'XXX';

// Setting up our server
express()
  // This route will be triggered when Chatfuel sends a message
  .get('/', (req, res) => {
    const query = url.parse(req.url, true).query;
    const userId = query['chatfuel user id'];
    const userMessage = query['user_message'];

    // Call SAP Conversational AI API with the user message
    return axios
      .post(
        'https://api.cai.tools.sap/build/v1/dialog',
        {
          message: { content: userMessage, type: 'text' },
          conversation_id: userId,
        },
        { headers: { Authorization: `Token ${SAPCAI_REQUEST_TOKEN}` } }
      )
      .then(body => {
        // Format messages to Chatfuel format
        const formattedMessages = body.data.results.messages.map(chatfuelFormat);
      
        // Sends the answer back to Chatfuel 
        res.json({
          messages: formattedMessages,
        });
      });
  })
  .listen(PORT, () => console.log(`App started on port ${5000}`));

// We need to manually "translate" SAP Conversational AI message object to Chatfuel's format
// For now I only implemented text and images but you can add others if you need to
// Check out the documentation I linked above to the see the json structure of the other formats
function chatfuelFormat(message) {
  // Source : { type: 'text', content: 'XXX' }
  // Destination { text: 'XXX' }
  if (message.type === 'text') {
    return { text: message.content };
  }

  // Source: { type: 'picture', content: 'URL' }
  // Destination: { attachment: { type: 'image', payload: { url: 'URL' } } }
  if (message.type === 'picture') {
    return {
      attachment: {
        type: 'image',
        payload: { url: message.content },
      },
    };
  }

  console.error('Unsupported message format: ', message.type);
  return { text: 'An error occured' };
}

In this code, don’t forget to add you bot’s request token (you can find it  on your bot’s page > Settings > Tokens).

Now you should host this code on a public server so that Chatfuel send requests to your API.You can try Ngrok if you want to expose publicly a port of your local computer.

checkmark Step 3/4 – Interfacing SAP Conversational AI and Chatfuel

75%

Configuring Chatfuel Fallback

Finally, we need to configure Chatfuel to send a request to the proxy when the bot receives a free-typed message.

On your Chatfuel panel, choose the Default answer

and reproduce this setup:

How to make Chatfuel bots smarter?

Setting up the fallback on Chatfuel

 

This will save the user message to a variable named user_message, and send a request using JSON API block to the proxy we created with two parameters: the message and a user id, to keep track of conversation states on SAP Conversational AI.

Your bot now understands small talk! Go test it on messenger:

checkmark Step 4/4 – Setting up the fallback

100%

Going further

Even if you have a functional Smalltalk module as is, you may want to personalize the texts of your bot, create new intents or improve the detection of your bot.

Everything you need to know about Intents and Skills is explained in this tutorial.

Take your time to improve the quality of your bot’s Smalltalk, it’s a very important part of its identity, and you will notice a much stronger engagement. The personality of a chatbot is a determining factor of its user retention rate.

Thank you very much for following along! We would be very happy to have your feedback on how small talks impacted your existing bots. And remember you’re very welcome to contact us if you need help, trough the comment section below or via Slack.

Want to build your own conversational bot? Get started with SAP Conversational AI !

Follow us on
  • Ga Friedman

    Hi
    Great and intelligent article, but I must dosagree about one thing. May I remind you that a bot SHOULD CONVERS WITH PEOPLE. This is the most (almost the only) strong advantage of bots. The conversation act.
    Now, you would never let any one who is not a programmer to program your software. You would never let someone who is not a mechanic, to fix you car. And I can go on with lots of examples.

    So why Do you think that the “script” part should be treated like anyone can do it? Like it’s a gimic? I have a guess why… 🙂 Don’t feel insulted, but it’s probably you, yourself, has no clue about it. You probably have never created, formatted, shaped a real written conversation, with all needed aspects inside.
    Just a disclosure – I’m a well experienced script writer (quite famous in my country). And I know how complicated it is. Bots will start being worth something only when people will understand that you cannot create a conversation without experts on that matter, and you don’t want to do it in 10 minutes. It’s the heart and the brain of your product. It is better to invest some thinking first 🙂
    Thank you very much
    Gal Friedman

  • Roldano De Persio

    Hi Nathan, unfortunately I am not a programmer at the moment 😉
    I am lost in the part
    npm install express axios.
    Could you please elaborate this part?
    I have seen other tutorial in which Dialogflow was coupled with Chatfuel.
    In one of this tutorial they used Runkit.
    Is it possible to have a similar example with Recast + Chatfuel?
    Merci 🙂

  • Amos

    Hi,

    very interesting article, thanks

    I tried to implement that with a glitch server.

    I have the following message in the my console when triggering the query :

    (node:8327) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Converting circular structure to JSON
    1:57 PM
    (node:8327) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

    Any idea on how to fix it?

    THanks !

    • Amos

      Here is a public glitch projet with a working code glitch.com/edit/#!/charming-staircase

      Cheers

  • Maxime

    Hi Amos,
    When I try to execute your project, I still have the same errors :

    (node:19698) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Converting circular structure to JSON
    (node:19698) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

    How did you fix it ?

    Thank you 😀

  • Maxime

    Ok, I have resolved the problem 🙂

    There is a typo error line 31, you just have to change :

    const userId = query[‘messenger_user_id’];
    BY
    const userId = query[‘messenger user id’];

    Cheers 😉

This site uses Akismet to reduce spam. Learn how your comment data is processed.