Motivation
As we couldn't use Slack at work, we looked for an alternative and found Mattermost.
- 1. It has all features of a state of the art chat tool.
- 2. It has a fancy REST API.
- 3. We can get rid of emails as development and deployment notifications
Sounds awesome. Let's start.
As always with software, documentation is key. The Mattermost REST API does have a fully automated generated API documentation for all the functions. It's way better than nothing. But you will see where the problem with that is.
I want to make sure the next guy trying to do this absolutely simple task to use the Mattermost REST API to make a chat post is way faster then I was. How? He can find a usefull article about that in the internet. So please forgive me how often I mentioned the Mattermost REST API. I just want to make sure this can be found later. Last but not least this article is a kind of a therapy for me. 😁
Setup
So the installation documentation is really good. They even have a fully working docker container prepared. An one line docker install I haven't found the right expression for how awesome this is.
docker run --name mattermost-preview -d --publish 8065:8065 mattermost/mattermost-previewSend a chat post via the Mattermost REST API
Let's get down to business. First thing is authentification. Let's look in the documentation. There is a curl example. The token/bearer relation is also explained. Pretty Nice.
This was easy. So let's post something. Open the documentation. Hmmm. That's a shitload of parameters in the request example. I definitly do not need all of them. But what is optional and what is mandatory? Did we talked about automated documentation? 😁 So in the url is the channel and team referenced. I probably just need the message.
Wrong. You also need the channel id again in the request. WTF #1
So i guessed the channel id and team id are the ones you see also in your browser url.
Wrong. Every Team and Channel has a display name, an url name AND an id.
So just use the API to get the id. Look into the documentation.
curl http://localhost:8065/api/v3/teams/allOh easy. Set the team and fetch the channels. So the new route ends like this.
curl http://localhost:8065/api/v3/teams/I_AM_THE_TEAM_ID/channels/allWrong. The syntax is slightly different. WTF #2
It is documented, but i would expect both routes using the same syntax.
curl http://localhost:8065/api/v3/teams/I_AM_THE_TEAM_ID/channels/And that's it. We now have the channel and team ids. We also know all mandatory parameters to make the post request. Put it together. For example in a bash script. Voila
And please don't ask why the channel_id has to be in the url and in the json. It doesn't make any sense but it will not work without.
curl example
#!/bin/bash
MM_USER="testuser";
MM_PASS="testpassword";
MM_TEAM="4btoed5pz7bi3fxohrp65u8bqy";
MM_CHANNEL="pi6s9sriz3ytux6qnzfewqnzxo";
TOKEN=$(curl -is -d "{\"login_id\":\"$MM_USER\",\"password\":\"$MM_PASS\"}" http://localhost:8065/api/v3/users/login | grep "Token: " | awk -F ': ' '{print $2}' | sed 's/.$//');
curl -is \
-H "Authorization: Bearer $TOKEN" \
-d "{\"message\":\"i am a robot\",\"channel_id\":\"$MM_CHANNEL\"}" \
"http://localhost:8065/api/v3/teams/$MM_TEAM/channels/$MM_CHANNEL/posts/create"