Post message to from bash

Posted 15 March, 2017

We recently moved over from Slack to a self-hosted communication app : . One of the things I really enjoyed on slack, was the fact that there is a huge amount of integrations. Even from bash one can write to slack. This is particularly nice for reports of cron jobs, previously I relied on e-mails, but those tend to get ignored/spammed/… not read. So I let cron jobs also slack me. On there are also incoming hooks, but I could not find a script that made it dummy prove to use like the one from Sul Aga so I took his code and adapted it to work with Here is how you can do the same.

Create a new incoming webhook

under administration –> integrations : add new incoming webhook

  • enabled: true
  • channel : #channel
  • script-enabled : true
  • script :
    class Script {
      process_incoming_request({ request }) {
        return {
            text: request.content.text
        return {
           error: {
             success: false,
             message: 'Error example'

The only thing you need now is the Webhook URL.

Sending messages : The script

Based on this script.

#!/usr/bin/env bash

function usage {
    echo "description: use this program to post messages to channel"
    echo "usage: $programName [-b \"message body\"] [-u \" url\"]"
    echo "      -b    The message body"
    echo "      -u    The hook url to post to"
    exit 1

while getopts ":b:u:h" opt; do
  case ${opt} in
    u) rocketUrl="$OPTARG"
    b) msgBody="$OPTARG"
    h) usage
    \?) echo "Invalid option -$OPTARG" >&2

if [[ ! "${rocketUrl}" || ! "${msgBody}" ]]; then
    echo "all arguments are required"

read -d '' payLoad << EOF
{"text": "${msgBody}"}

echo $payLoad

statusCode=$(curl \
        --write-out %{http_code} \
        --silent \
        --output /dev/null \
        -X POST \
        -H 'Content-type: application/json' \
        --data "${payLoad}" ${rocketUrl})

echo ${statusCode}

Now to use it is simple; Take webhook url, and start posting messages.

./ -b "everything is fine today" -u ROCKET_CHAT_URL

And that’s it folks ! Happy spamming 🙂

Show Comments (9)
© SvennD