In November 2022 OpenAI launched their AI-driven chatbot called ChatGPT. It swept the world fast and started to be implemented via API into anything that it can be used with.

From websites, search engines, chat platforms even personal assistants, everyone wants to be able to elevate current solutions with some AI power. On that note, the Mattermost chat platform is also a perfect candidate for this implementation.

💡
INFO: all credit for this integration goes to yGuy over at GitHub for making the repo that can be built into a Docker image in order to run the container needed to connect with a Mattermost instance.

Considering that I use Mattermost as my Synology support platform I have decided to give it a go.

One of the first things needed in order to make this happen is to have a Mattermost instance up and running. I'm self-hosting it using Docker, and more details on that can be found on the following link.

Mattermost - (free) open-source messaging platform
How to run Mattermost collaboration platform on your Synology NAS using Docker - a short tutorial
How to run Mattermost via Docker

Prerequisites for ChatGPT and Mattermost integration

In order to get this running there are several steps that need to be executed. I will follow through and list them all. First, prerequisites.

  • the Mattermost token for the bot user @chatgpt
  • the OpenAI API key
  • a Docker container for continuously running the service

These are the three elements needed so let's see how to configure each of them, starting with creating the Mattermost chatgpt bot.

Create a Mattermost bot account

Once there is a functional Mattermost instance up and running it is time to make the bot. Like with any other bot, the process is the same.

In the upper left menu of Mattermost we choose Integrations > Bot Accounts  > Add Bot account.

Fill in the needed information, add a bot icon (not mandatory), and make sure to configure the username of the bot as chatgpt. If you want to use a different name then that will need to be changed inside the src/botservice.js file inside the repo before pulling it and making it into a Docker image!

💡
Once the bot has been created be sure to grab the bot ID value as we will need it later as a value for one of the Docker variables.

Create an OpenAI API key

  1. Next, go to Open AI platform, register/log in, and in the API Keys section, click Create new Secret Key.
  2. Save the OpenAI API Key that is generated, and store it safely as it will also be used as one of the Docker variables.
Create a new key
⚠️
WARNING: Be sure to copy the secret key value before you close the window. This parameter will be also used as a Docker variable.

Deploy the ChatGPT container

Finally, we need to run the Docker container. To do that we first need to a) build the image from the GitHub repository, or use the image that I have already built and uploaded to the official HUB.

If you want to use the already-built image then just use the following docker-compose either via the Docker command line or using Portainer and its stacks feature.

version: "3"
services:
  chatgp:
    image: blackvoidclub/chatgpt-mattermost-bot:latest
    container_name: chatgpt
    environment:
      - MATTERMOST_URL=https://mattermost.server
      - MATTERMOST_TOKEN=xxxxxxxxxxxxxxxx
      - OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    restart: always

MATTERMOST_URL variable needs to have a valid Mattermost instance URL. MATTERMOST_TOKEN will have the ChatGPT bot ID that was generated upon creation. Finally, OPENAI_API_KEY needs to have the value of the secret key generated on the OpenAI website.

There are several optional variables as well, so add them if you want.

# Set this if using a custom username for the bot, default = @chatgpt
MATTERMOST_BOTNAME: "@chatgpt"

# Console logging output level, default = INFO
DEBUG_LEVEL: TRACE

# Node environment, default = production
NODE_ENV: production

If on the other hand, you want to build your own image before running it using the same compose as above, just a different name of the image, do the following on your Docker-capable machine.

  1. Clone the repo git clone https://github.com/yGuy/chatgpt-mattermost-bot
  2. Go into repo directory cd chatgpt-mattermost-bot
  3. Create docker image docker build . -t yguy/chatgpt-mattermost-bot
⚠️
In step three you can change the name of the future image to anything you want, just be sure to use the correct name in the docker-compose as part of the image: parameter!

That's it. Everything should be up and running and if all the parameters are set correctly we should be able to have chatGPT running inside Mattermost.

How to use ChatGPT bot in Mattermost?

Private conversation with ChatGPT via Mattermost 

Interact with the ChatGPT bot in Mattermost, as with any other member of the chat platform. Be sure to tag its name in order to initiate a response.

In case you want it to work in any channel, first, add it as a member of your Mattermost team. Then, add it to the specific channels that you want.

Add the ChatGPT bot as part of your team if you want to add it to specific channels

There we have it. Mattermost instance using a ChatGPT AI bot to enrich the experience. The GPT is a separate docker container, and any updates to it will require a rebuild of the image as the source code changes.