Last active
July 4, 2022 18:08
-
-
Save adamzwakk/f74ca3c6bde639a2d86c9dfb1f59e665 to your computer and use it in GitHub Desktop.
Slack Helper to send stdout or messages from bash/shell to Slack message API quickly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## Defaults | |
SLACK_TOKEN="YOUR TOKEN" | |
CHANNEL="YOUR CHANNEL" | |
IN="" | |
MESS="" | |
while getopts c:m: flag | |
do | |
case "${flag}" in | |
m) MESS="${OPTARG}";; | |
esac | |
done | |
if [ -z "$MESS" ]; then | |
# Use stdout from pipe | |
[ -t 0 ] && [ -t 2 ] && echo "No input specified" && exit 1 | |
IN="$(cat)" | |
elif [ ! -z "$MESS" ] | |
# Use -m "message" | |
IN="${MESS}" | |
else | |
# Idk you didnt really specify anything did you | |
echo "No input specified" && exit 1 | |
fi | |
curl -H "Authorization: Bearer ${SLACK_TOKEN}" -s \ | |
-H "Content-type: application/json; charset=utf-8" \ | |
--data "{\"channel\":\"${CHANNEL}\",\"blocks\":[{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"*Message from $(hostname):*\n ${IN}\"}}]}" \ | |
-X POST https://slack.com/api/chat.postMessage > /dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment