[Solved] Incoming webhooks, not ok

When using curl to call a webhook to post something new, Mattermost responds with “ok” to every successful call.

This is a little vexing when scripting messages as I don’t want to know when something works, only when it doesn’t work. (I’m a linux admin, we’re like that) It’s a problem when using cron as I get a message every time it sends something. I’d rather only get a message when it breaks.

Is there any way to tell mattermost to not respond with “ok” on success, yet still allow any error messages to be output?

I know I could redirect output to &>/dev/null but that would also suppress output when something goes wrong.I could probably write a wrapper that eats any output that’s just “ok”, but wondered if anyone else had a more elegant solution.

Thanks

Example webhook command;

curl -s -k -X POST -H "Content-Type: application/json" -d "{"username": "botname", "text": "message content"}" webhook_url'

Hi dartymoor and welcome to the Mattermost forums!

You can check for the HTTP return code of the request. Anything that’s not 200/ok is not OK and contains an error.

I didn’t find a way to let curl output the HTTP status/code directly or even better, return it and it’s also not optimal that the webhook response is sometimes json and sometimes not, but to workaround that, you can use the following bash snippet:

#!/bin/bash

if [ $# -lt 2 ]; then
  echo "usage: $(basename $0) channelid message..."
  exit
fi
WEBHOOKID="<your-webhook-id>"
CHANNELID=$1
shift
MESSAGE="$*"

OUTPUT=$(curl -s -w "\n%{http_code}" -X POST -H "Content-Type: application/json" -d "{\"text\": \"$MESSAGE\", \"channel\": \"$CHANNELID\" }" https://<your-mattermost-domain>/hooks/$WEBHOOKID)
RESPONSE=$(echo -e "$OUTPUT" | sed -e '$ d')
CODE=$(echo -e "$OUTPUT" | tail -1)
if [ "$CODE" != "200" ]; then
  echo "Error: $RESPONSE"
fi

This will produce the following output:

# Working Call
$ ./mmpost.sh @agriesser test
$

# Invalid channel ID
$ ./mmpost.sh @agriesser123 test
Error: {"id":"web.incoming_webhook.user.app_error","message":"Couldn't find the user.","detailed_error":"","request_id":"kpweuwwscpgqppqtpqmk4qrjgo","status_code":400}
$

# invalid webhook ID
$ ./mmpost.sh @agriesser test
Error: {"id":"web.incoming_webhook.invalid.app_error","message":"Invalid webhook.","detailed_error":"","request_id":"f3k5x871hirkup8x9ambqy7b9y","status_code":400}

Does that help?

Thank you for your considered reply - and yes, that does very much help.

I had pretty much resigned that I’d need to write a wrapper for the call and it’s good to know I’m not missing the obvious. But I hadn’t considered using the http error code, which is a neater and more reliable way of handling it than just parsing the stdout response.

Thanks again, this is most useful.

Glad to help :slight_smile: I’ve marked my answer as solution to this thread, hope that’s OK.