Post message to from bash
March 15, 2017

Post message to from bash

Posted on March 15, 2017  •  2 minutes  • 340 words  •  Suggest Changes

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

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 "🅱️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 🙂


If you enjoyed this website, consider buying me a Dr. Pepper

Buy me a Dr PepperBuy me a Dr Pepper