In this blog we are going to show how easy it is to create a Messenger chatbot that understands unstructured user input. We'll use Google’s API.AI for natural language processing (NLP) support. The bot will analyze the messages sent by users and respond appropriately. If a message is a greeting, the bot will respond with a greeting. If a message is not recognized, the bot with respond with a static message.
These 2 cases represent the actions implemented by the bot. Each one of these actions is represented in API.AI with an intent. In Intwixt, each action is implemented as activities containing the logic necessary to send the appropriate message over Messenger.
A typical development process for implementing NLP bots with API.AI and Intwixt involves switching back and forth between the tools provided by each. This can be easily done by working in two different browser sessions, running them side by side. API.AI is tightly integrated with the Intwixt tooling. Therefore, as the user makes changes in API.AI, they become visible in Intwixt. Some changes, like training the bot with new utterances for existing intents do not impact the bot implementation. The user should be able to make them without even restarting the bot.
Let’s get started! (Please note, I've added a video at the end of this blog, showing all steps in full detail).
Step 1 - Set up API.AI Agent & Get its API Keys (API.AI)
First, if you haven’t done it already, sign up for an API.AI account (https://api.ai/). Once you log in, create a new agent. Let’s call it “HelloWorld”. For more information on how to create the agent and other basic resources, please see the API.AI’s guide: https://docs.api.ai/docs/get-started. This is what you will see once the agent is created:
The first thing to notice is that the agent already has an intent created for greetings (e.g. "Default Welcome Intent"). This means that it is already pre-configured to react to greeting messages. Please note that the intent is not configured with user utterances. You will have to add them yourself. To keep things simple for now, just add a couple of them: “hi” and “hello”.
The second thing to notice is the “Default Fallback Intent”. The agent will communicate this intent whenever it does not understand the message (i.e. it cannot extract another intent from the message).
Tip: the test tool that is provided by API.AI, which is visible above in the top right corner, is very handy. We highly recommend using it, as you make changes in API.AI.
Next you need to get and copy the API keys used by your API.AI agent. Your bot, which is implemented in Intwixt, will have to be configured with these keys in order to contact the API.AI agent. The instructions on how to get your agent’s API keys are straightforward and can be found here: https://docs.api.ai/docs/authentication.
At this point you have everything you need to implement your bot in Intwixt. Let’s start working on it.
Step 2 - Set up the API.AI Credential (Intwixt)
Once you log in (https://my.intwixt.com), you need to set up your account with an API.AI Credential. That is done on the Credentials page (https://my.intwixt.com/#/credentials). You can modify the default credential or, if your account allows it, create a new credential. You will have to use the keys copied as part of Step #1.
Step 3 - Create the bot (Intwixt)
Tip: before you implement the new bot, we recommend setting up your Intwixt account and import/run the HelloWorld (Messenger) bot. That way you know that your Messenger connection is working fine. Here is a blog that describes how to do that: http://www.intwixt.com/blog/importabot.
Once your account is set up with a Messenger credential you are ready to build your new bot. Go to the bots page (https://my.intwixt.com/#/flows) and create a new chatbot by clicking the button, Create, and then choosing the menu option, Create Generic Flow. Let’s call this flow, "Hello World". Add and configure a Messenger Start Conversation trigger, which ensures that your bot is able to listen for messages sent by Messenger users. Next, connect a Get User Info activity. You will use this activity to get more information about the calling user. Map the Sender ID parameter from the output of the trigger activity to the User ID parameter of the Get User Info activity’s input message.
Next, create a second botlet that will extract the intent from the user’s message and respond with an appropriate message of its own. Let’s call this botlet, "Intent Resolver". Add an HTTP Receiver trigger and configure it with the following schema, which contains the Messenger ID of the user, the first name of the user and the text message sent by the user:
messenger_id, first_name, user_text
At this point, the botlet’s Receive activity will look like this:
Let’s connect the main logic to this newly created botlet. To do that, add to the main business logic an Invoke activity and configure it to invoke the botlet. Map the Sender ID from the trigger’s output message to the Invoker’s Input / Messenger ID parameter. Map the First Name parameter from the Get User Info activity’s output message to the Invoker’s Input / First Name parameter. Lastly, map the Message parameter from the trigger’s output data to the Invoker’s Input / User Text parameter. At this point you have a bot that is enabled to receive messages and dispatch them to an empty implementation. It should look like this:
We'll now add the bot’s business logic that deals with NLP. The logic will be implemented as part of the Intent Resolver botlet. Let’s add to it an Extract Intent from Text activity (from the GoogleAPI.AI Connector). As its name indicates, this activity is in charge with extracting the intent from the user’s message. The configuration of the activity is very simple. It references the Google API.AI credential created earlier and it uses, as part of an input parameter, the text message sent by the user.
The output of this activity will have the intent of the message plus more additional metadata extracted from the message. Since this example is very simple, we will only use the intent information. If you expand the Intent output parameter (in the Output tab), you should be able to see all intents that are known to the API.AI HelloWorld agent (e.g. Default Welcome Intent and Default Fallback Intent).
Next, let’s implement the logic that responds to the user based on the intent extracted from the message. We’ll start with the logic for the Default Welcome Intent. Since this intent is configured in API.AI with a few output messages, the output of the Extract Intent from Text activity will have, at runtime, a suggested response message. Instead of typing in a static message, we can send this suggested message to the user, which is taken from the Fulfillment Speech output parameter. To make it more personal, we’ll prepend the message with the first name of the user. We’ll use the Messenger connector’s Send a Text activity to send it out.
Since we want this activity to only execute when the extracted intent is Default Welcome Intent, we need to configure a transition condition to reflect that. The API.AI connector, like any other Intwixt connector, enhances the development experience by bringing in additional metadata into the bot designer. For example here, the user can build transition conditions based on the intents defined in API.AI (see bellow).
Next we’re going to implement the logic for handling the Default Fallback Intent. This time we will respond with a static message (e.g. “I am sorry, I do not understand this”). As is the case with the other intent, we need to ensure that this activity executes only when the extracted intent matches the desired intent, which is in this case Default Fallback Intent. Since this is the only intent left and it is the “fallback” intent, you can simply use the Otherwise transition clause.
At this point the HelloWorld NLP bot is fully implemented and ready to be tested. First, let’s activate the bot.
Next, let’s send a few messages from Messenger and see how the bot reacts to them. You should see something like this:
This looks good. The bot responds to known greetings with a greeting and to unknown messages with the static “I am sorry, I do not understand” message. Now, if the user sends a greeting that is not understood by the API.AI agent, the bot will not respond with a greeting.
This is an expected behavior. We need to train the bot to understand this new greeting, which can easily be done in API.AI. Just add the greeting to the Default Welcome Intent.
Without making any change to the bot implementation and without even restarting, just send the same greeting again in Messenger.
That’s it! We now have a working NLP bot using Intwixt and API.AI. Here is the video that shows all steps outlined above:
Please note that this example is available for you as a template within Intwixt. If you have any questions, please do not hesitate to reach out. We would love to learn about your use-case and see what else we need to do to help you build amazing bots. Also, if you want to stay up to date with the platform announcements and get more insight on how to build amazing bots, please join our community at: https://www.facebook.com/groups/intwixtdevcom/.