Send a Message to Slack From a Bash Script



RECOMMENDED: Click here to fix Windows errors and optimize system performance

With the help of a few webhooks, you can turn Slack into a notification center for your command line. You can do a lot, from simple message feeds to custom Slack integrations for your application.

This type of integration works especially well for notification-based events, such as when something is updated or new content is posted somewhere, as RSS feeds do. You can also create custom integrations for your own applications that use the same API and even include interactive dialogs and menus for user feedback.

This bash script will send a message to Slack. It can be easily modified to suit your needs, but it can also be used as is. This script is very useful for sending notifications for background tasks, for example.

Slack is a very good team collaboration tool, which we use every day to exchange information. Remember the good old days on the irc channels? Slack is conceptually the same, but implemented with the latest and greatest web technologies (web sockets, rest APIs, etc.). It has also been created with many extensibility/integration points, which opens up many interesting options when it comes to sending automatic notifications from scripts and programs.

Create an App in Slack

  • Go to https://api.slack.com/slack-apps
  • Creation of an application
  • Login with your workspace


Update:

We now recommend using this tool for your error. Additionally, this tool fixes common computer errors, protects you against file loss, malware, hardware failures and optimizes your PC for maximum performance. You can fix your PC problems quickly and prevent others from happening with this software:

  • Step 1 : Download PC Repair & Optimizer Tool (Windows 10, 8, 7, XP, Vista – Microsoft Gold Certified).
  • Step 2 : Click “Start Scan” to find Windows registry issues that could be causing PC problems.
  • Step 3 : Click “Repair All” to fix all issues.

download



Create a Slack App and a Webhook

  • Accessing and activating incoming webhooks
  • You can see an example of your Webhook cURLthen
  • Create a channel in your Slack ex: testalerts and assign step c below
  • Add a new webhook to the Post To workspace: #testalerts
  • URL of your workspace
curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/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

Test by POST’ing data to your webhook

$ curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/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

Write Shell Script to check a Webservice URL and check for HTTP 200 OK in the header

Shell Script
#!/bin/bash
#################################################################
# SHELL SCRIPT TO MONITOR PUBLIC URL AND REST API GATEWAY
# Created By : Shadab Mohammad
# Company : Whitehat Agency, Sydney
# Created Date : 25/10/2018
# Modified Date : 29/10/2018
#
#################################################################
## Check ASIC WebService ##
#################################################################
if curl -s --head --request GET https://abr.business.gov.au | grep "200 OK" > /dev/null; then
echo "https://abr.business.gov.au is UP"
else
echo "https://abr.business.gov.au is DOWN"
curl -X POST -H 'Content-type: application/json' --data '{"text":"https://abr.business.gov.au is DOWN"}' https://hooks.slack.com/services/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
fi

The shell script above checks the header for 200 OK messages, if it is found it sends an alarm to the channel via the slack webhook. It can be added to the crontab to check the status of the web service every 5 minutes. If the web service is down, it sends an alert to the release channel.

Using Formatted Messages and Attachments in Slack

The Message API can send more than just plain text. If you want your messages to be more stylized, you need to add a few more parameters to the JSON object that sends the curl.

You can use markdown and other simple formats in the same way as you are used to using the Slack message box. Note, however, that to link to @ing channels and users, you must specify the channel ID and user ID, not plain text names, otherwise it will not work.

For everything else, you should use Slack’s Block Kit Builder, a WYSIWYG editor for Slack’s messages that allows you to create interactive and richly integrated content, and will even create JSON for you. Items such as buttons and menus can be configured to send POST requests to a URL you specify, allowing your users to communicate with the application sending the message.

However, the Block Kit Builder is not entirely necessary because the old methods are not deprecated. If you want to use only functions such as attachments, you can use the old message builder to create and preview the JSON.

https://easyoradba.com/2018/10/29/send-alerts-to-slack-with-bash-shell-script/



RECOMMENDED: Click here to troubleshoot Windows errors and optimize system performance