vikingsteve
vikingsteve

Reputation: 40398

Mattermost (team chat) - how to post to a channel via curl?

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:

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

Upvotes: 3

Views: 8603

Answers (2)

Walf
Walf

Reputation: 9318

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

Upvotes: 2

vikingsteve
vikingsteve

Reputation: 40398

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

Upvotes: 7

Related Questions