How to use mattermost golang driver

Sep 29, 2023 by Arnaud Stoz | 523 views

golang

https://cylab.be/blog/288/how-to-use-mattermost-golang-driver

If you have already tried to develop a bot in golang for mattermost, you have probably already found the documentation page for the API.

The documentation page says :

The easiest way to interact with the Mattermost Web Service API is through a language specific driver.

However there is no documentation on how to use this "driver" for mattermost. In this post you will learn how to use golang driver to post a message to a specific channel.

Importing the driver

The first step to use the golang driver is to import it with the import statement

import (
       mattermost "github.com/mattermost/mattermost/server/public/model"
)

you might be missing the module. In that case you will have to run go get github.com/mattermost/mattermost/server/public/model. This will download the package.

This name could change with future release of the driver. Just check the correct name in the go.mod file closer to the model folder.

Notice the mattermost keyword at the beginning of the package name. This allows to rename the package name. In this case it is changed from model to mattermost.

Initializing the client

Once the package is imported, before being able to send message to a channel, you should create a new Client4.

In this quick tutorial we assume you have created a bot to allow you to post to mattermost. If it's not already done, you can find documentation here. Once you have created your Client you need to give the token you received when creating the bot.

client := mattermost.NewAPIv4Client(url) //create a new Client for URL
client.SetToken(token) //give permission of the bot to the client

Sending message to a channel

Now the client is up and ready to send message to a channel. To achieve that we will use the function CreatePost.

This function takes two arguments:

  • context.Context: In our case we will not worry too much about this one, if you want to learn more about context in golang you can check here
  • Post: a structure post which represents the message we want to publish.

The full definition of the Post structure can be found here. In our simple case we will only focus on two fields:

  • ChannelID
  • Message

The line to create our Post can be seen just below:

post := mattermost.Post{
        ChannelId: channelID,
        Message:   "Hello World!",
    }

We can then use the function to send the message

client.CreatePost(context.Background(), &post)

Putting all together

Now we have all steps to be able to send message using the mattermost golang driver. The full code is shown below

package main

import (
       "context"
       "fmt"

        mattermost "github.com/mattermost/mattermost/server/public/model"
)

func main() {
    url := "myServerURL"
    token := "myToken"
    channelId := "channelId"

    client := mattermost.NewAPIv4Client(url)
    client.SetToken(token)

    post := mattermost.Post {
        ChannelId: channelId,
        Message: "Hello World!",
    }

    _ , _ , err := client.CreatePost(context.Background(), &post)
    if err != nil {
        fmt.Println("could not post message ", err)
    }

}

Do not store your token in clear in the source specially if you use a version control software.

Congratulations! You now have posted your first message using the mattermost golang driver.

In summary this driver gives a nice level of abstraction to not have to deal with HTTP directly. But it can also do much more than just posting a message. To discover all possibilities, feel free to explore other functions offered by the driver.

This blog post is licensed under CC BY-SA 4.0

This website uses cookies. More information about the use of cookies is available in the cookies policy.
Accept