slack-post

Program to post message to slack messenger.

Uses slack webhook app as the entry point to posting messages. webhook-api

You will declare a default channel in slack for these messages, this can be overridden so just select something that exists.

The "url" (on line 6) will be obtained on the slack app webpage for setup of webhook app.

Enjoy!

Tom

Usage:

Where:

--channel = an existing slack channel you have created

--service = some label/line-header for the message

--data = the data you want displayed

slack-post --channel 'foo' --service 'bar' --data "file ${filename} has been processed"

/usr/local/bin/slack-post

##############################################

1 #! /bin/bash

2 #

3 #

4 #

5 # our connection URL / secret

6 url="https://hooks.slack.com/services/AAAAAAAA/BBBBBB/CCCCCCCCCCCCC"

7 temp=$(getopt -o hc:s:d: --long help,channel:,service:,data: -n 'slackpost' -- "$@")

8 pname=$(basename $0)

9 function help() {

10 echo "Usage:"

11 echo "${pname} --channel CHANNEL --service SERVICE --data DATA"

12 echo ""

13 echo "--channel: The existing channel (without the leading #)"

14 echo "--service: The (username) posting the entry. Shows in bold at top of message)"

15 echo "--data: The data to display in the post"

16 echo ""

17 exit 0

18 }

19 if [ $# -lt 2 ]

20 then

21 help

22 fi

23 if [ $? != 0 ]

24 then

25 echo "Terminating..." >&2

26 exit 1

27 fi

28 eval set -- "$temp"

29 SERVICE=""

30 CHANNEL=""

31 DATA=""

32 while true

33 do

34 case "$1" in

35 -c | --channel ) CHANNEL="$2"; shift 2 ;;

36 -s | --service ) SERVICE="$2"; shift 2 ;;

37 -d | --data ) DATA="$2"; shift 2 ;;

38 -h | --help ) help ;;

39 -- ) shift; break;;

40 *) break ;;

41 esac

42 done

43 if [ "${SERVICE}X" == "X" ]

44 then help

45 fi

46 if [ "${CHANNEL}X" == "X" ]

47 then help

48 fi

49 if [ "${DATA}X" == "X" ]

50 then help

51 fi

52 curl -X POST -H 'Content-type: application/json' --data "{\"fallback\": \"$DATA\", \"username\": \"$SERVICE\", \"channel\": \"#${CHANNEL}\", \"text\": \"$DATA\"}" $url