3

We are using mattermost as an internally hosted alternative to slack.

How can I write to a channel in mattermost, for example by a script, using curl?

I need to know:

  • how to get an "integration key" from mattermost
  • format of the curl command

An example of my script that works with slack:

SLACK_URL='https://hooks.slack.com/services/my-long-integration-key'
message='Project XYZ was released successfully.'

curl -X POST -H "Content-Type: application/json" \
    --data "{ \"channel\": \"#releases\", \"username\": \"$me\", \"text\": \"$MESSAGE\" }" \
    $SLACK_URL &> /dev/null

Indeed there is a Mattermost API (equivalent to slack-api) but I am struggling to a good example of what I want to do.

Thanks

vikingsteve
  • 38,481
  • 23
  • 112
  • 156

2 Answers2

7

Here's the format, using curl and a json payload:

curl -i -X POST -d 'payload={"text": "Hello, world!", "username":"xxx", "channel":"yyy"}' \
https://mattermost.intern.mycompany.com/hooks/abcdefg1234567
vikingsteve
  • 38,481
  • 23
  • 112
  • 156
2

For others, I recommend using the jq program in addition to curl (your distro probably has a pacakage for it in the standard repos). It will turn any text input into valid JSON data.

E.g. a script called matmo.sh:

#!/bin/bash
mattermost_hook_url='https://mattermost.example.com/hooks/long-random-hook-id'
jq --slurp --raw-input --compact-output --arg channel "$1" --arg username "$2" '{$channel, $username, text:.}' \
    | curl -H 'Content-Type: application/json' --data @- "$mattermost_hook_url" &> /dev/null

Then pipe to it like so:

command-that-produces-output | ./matmo.sh '#releases' releasebot
Walf
  • 8,535
  • 2
  • 44
  • 59