slack, webhooks,

Send Slack notifications from a Bash script

Jan 06, 2022 · 1 min read · Post a comment

Slack is one of the most popular and used messaging apps by companies of any kind of business size, from small companies and startups to large enterprises. From DevOps point of view, the need for automation, observability and monitoring is still there, so let me show you a basic example on how we could use the Slack API to send notifications.

Prerequisites

  • Bash environment
  • Slack account

Solution

Step 1. Login to Slack and create a new Slack app.

Step 2. Once done, you’ll be redirected to your new app. On the left side, under Features click Incoming Webhooks to activate it.
bash slack incoming webhooks

Step 3. Scroll down and under Webhook URLs for Your Workspace click Add New Webhook to Workspace.
bash slack add new webhook

Step 4. Choose the channel name and click Allow.

Step 5. Copy your newly generated webhook’s URL.

Step 6. Start writing your bash script. For instance:

#!/bin/bash

MONITORING_SLACK_WEBHOOK="<paste_slack_webhook_url_here>"

<do_some_bash_script_logic_here>

curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello from Bash!"}' $MONITORING_SLACK_WEBHOOK

Conclusion

This should be enough to get you started. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.