Engineering

Mattermost Recipe: How to create a chatbot with Dialogflow and Google Cloud Functions

(Editor’s note: This guest Mattermost Recipe was contributed by Brian Hopkins, a member of the Mattermost community.)

Here’s the next installment of a new series of posts we’re doing on the Mattermost blog: Mattermost Recipes.

The goal of these posts is to provide you with solutions to specific problems, as well as a discussion about the details of the solution and some tips about how to customize it to suit your needs perfectly.

If there’s a Recipe you want us to cook up in the future, drop us a line on our forum.

Problem

You want to build a chatbot using advanced AI for the open source chat framework Mattermost.

Solution

Use Google Cloud Functions to build out a Natural Understanding chatbot via the Dialogflow system owned now by Google and integrate it with Mattermost.

First off, you will need to sign up for Google Cloud. After you are signed up, you will also need to sign up for the Blaze Plan since we will be making external API calls. It’s pay as you go and you should also get $300 in credits when you sign up for Google Cloud.

More information on Google Cloud can also be found here. You will also need to sign up for a free Dialogflow account to be able to build a chatbot there.

Lastly, you’ll need a Mattermost user that has permissions to create integrations on your Mattermost server.

Step 1: Configuring Google Cloud Webhook via Cloud Functions

Using the source code found here, copy the index.js file and package.json file to the respective sections in the Google Cloud Function you create in the below steps.

1. Upon logging into Google Cloud Console, go to the Compute Section > then Cloud Functions. You should see a screen similar to mine below. It will also ask you to create a project name; you can see mine is called Mycroft for a chatbot I have built for that community.

chatbot

2. Click on Create Function and create your cloud function that you will be calling from Mattermost via an outgoing webhook integration you will be setting up next.

3. Upon clicking on Create Function, you will be presented with a screen like you can see in the below image. You will want to keep the defaults and you can name your function whatever you like. If you have extra memory requirements, you can change that as well if you prefer. Make sure to open the inline editor and paste in the index.js and package.json files from the source code listed above and make sure the function to execute section is set to mattermostHook.

chatbot

4. After clicking Create, you now have a screen with a lot of information on it. Click on the Trigger tab to get your webhook URL. Copy this down because you’ll need it for the next section.

Step 2: Setting up Mattermost webhooks

Follow the instructions via the Mattermost documentation to set up an outgoing webhook and an incoming webhook.

In your outgoing webhook setup, you should basically have something similar to the below screenshot configured. Notice the bottom section where you need to put our Google Cloud Trigger Function URL there. This is what tells Mattermost to send the event to your bot.

We also have the trigger word set up so that you have to do @botname with your question for it to respond. Otherwise, it will just respond to everything in the channel (which is fine, if that is what you want).

chatbot

In our incoming webhook, we only really need to set up the channel we want our posts to go to. It won’t matter much anyway because you can override the channel you post to in the API.

chatbot

Great! Now you have a webhook listening in the Random channel and another one that can post to it.

The downside, currently, is that with you would have to set an outgoing webhook up for each channel you want the bot listening in on, adding the Google Cloud Trigger URL in each. (I think another way around this would be to set up a websocket bot, but that is for a different blog post.)

Step 3: Setting up Google Dialogflow for our chatbot

Now you’ve arrived at the final step, which is to set up a chatbot at Dialogflow.

1. Sign up for a new account, which is free. Create a new Agent via the Console. You will want to select a name and timezone. Make sure you don’t select APIv2 for this particular how-to.

2. You now need to create your first intent which you can do via the Intents section on the left side. For more information from Dialogflow on creating an Agent with intents, please go here. You can see from the screenshot below I have clicked on create new intent and have given it some basic questions that users might ask to trigger this intent.

chatbot

In this screenshot, I am giving the response that I want my bot to send when this intent is triggered.

chatbot

After you’re done with this step, save your progress.

On the right side of the screen, you should be able to then test via the console to see if your bot works:

chatbot

3. Finally, you can go to the Gear/Settings icon for our agent (in dialogflow.com) and grab the client access token. You will then put that into the index.js file in your Google Cloud Function on line 3 and save it. You can now test your bot and it should reply.

Please note that it can take a few minutes for the Google Cloud Function to save and then update before working. You can always look at the function’s live logs to see what’s going on.

chatbot

Discussion

In this particular recipe, we explore using Dialogflow for Natural Language Processing/Understanding (NLP/NLU).

There are lots of other options you could use here, too. For example, you can use Rasa and host your own server, giving you a complete end-to-end hosted solution using Mattermost and Rasa.

The NLP/NLU piece of this recipe is what allows the bot to understand different ways users ask questions and enable it give the same response back depending on what intent is classified.

For more information on things of this nature, please feel free to check out my blog.

Source Code

The source code for this blog project can be found here.

I don’t go into really detail on what the code does, but basically it creates a webhook that takes the event from Mattermost and grabs out the user and channel it was requested from (in case we wanted to later do @mention the user).

Then the text is classified by sending it to the Dialogflow API. Once we get that response, a Mattermost incoming webhook posts it back to the channel on line 42.

(Editor’s noteThis post was written by Jason Blais, Lead Product Manager at Mattermost, Inc. If you have any feedback or questions about Mattermost Recipe: How to create a chatbot with Dialogflow and Google Cloud Functions, please let us know.)

mm

Jason Blais is a Lead Product Manager at Mattermost, Inc. Prior to joining Mattermost, Jason served as a product manager and analytics manager for SpinPunch, a Y Combinator-backed online gaming startup. Jason has also provided statistical consultation at Stanford University. He is a University of Waterloo alumnus.