Slack is a very good team collaboration tool that we use on a daily basis for sharing information. Remember the good old times on the irc channels? Slack is conceptually the same, but implemented with the latest and greatest web technologies out there (web sockets, rest api, etc). Also it was created with a lot extensibility/integration points, which opens a lot of cool options when it comes to send automatic notifications from scripts and programs.

The easiest way is simply send a HTTP POST request from your script, using curl. You will need two things:

Go to slack home using a web browser, and select Add Integrations. Scroll to the bottom, and select Incoming WebHooks (click on Add). You have the option to select your channel (or create a new one), and you will see your hook url which looks something like https://hooks.slack.com/services/${ID}/. Give it a name and a custom icon.

Then, you can use this curl command to post:

#!/bin/bash

function post_to_slack () {
  # format message as a code block ```${msg}```
  SLACK_MESSAGE="\`\`\`$1\`\`\`"
  SLACK_URL=https://hooks.slack.com/services/your-service-identifier-part-here
 
  case "$2" in
    INFO)
      SLACK_ICON=':slack:'
      ;;
    WARNING)
      SLACK_ICON=':warning:'
      ;;
    ERROR)
      SLACK_ICON=':bangbang:'
      ;;
    *)
      SLACK_ICON=':slack:'
      ;;
  esac
 
  curl -X POST --data "payload={\"text\": \"${SLACK_ICON} ${SLACK_MESSAGE}\"}" ${SLACK_URL}
}

post_to_slack "Hello, World" "INFO"
exit 0