Create a mattermost chatbot with Dialogflow and Google Cloud
You want to build a chatbot using advanced AI for the open source chat framework Mattermost.
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.
- Upon logging into Google Cloud Console, go to the Compute Section > then Cloud Functions. You should see a screen similar to mine. 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.
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.
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
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).
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.
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.
In this particular recipe, we explore using Dialogflow for Natural Language Processing/Understanding (NLP/NLU).
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 For Project
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.