Send a Message to Slack From a Bash Script



Updated May 2024: Stop getting error messages and slow down your system with our optimization tool. Get it now at this link
  1. Download and install the repair tool here.
  2. Let it scan your computer.
  3. The tool will then repair your computer.

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

May 2024 Update:

You can now prevent PC problems by using this tool, such as protecting you against file loss and malware. Additionally, it is a great way to optimize your computer for maximum performance. The program fixes common errors that might occur on Windows systems with ease - no need for hours of troubleshooting when you have the perfect solution at your fingertips:

  • 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 Now

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/



Expert Tip: This repair tool scans the repositories and replaces corrupt or missing files if none of these methods have worked. It works well in most cases where the problem is due to system corruption. This tool will also optimize your system to maximize performance. It can be downloaded by Clicking Here

Scroll to Top