Skip to content

Instantly share code, notes, and snippets.

@mattak
Last active September 3, 2021 08:05
Show Gist options
  • Save mattak/af5a9d6790b1e5c5a175 to your computer and use it in GitHub Desktop.
Save mattak/af5a9d6790b1e5c5a175 to your computer and use it in GitHub Desktop.
post message to slack
#!bash -e
TOKEN= # slack token is generate from: https://api.slack.com/web
CHANNEL= # name of channels or group
MESSAGE= # message
NICK= # bot name
IS_PRIVATE= # 1 or 0
if [ $IS_PRIVATE -eq 1 ]; then
API_TYPE=groups
else
API_TYPE=channels
fi
# channel info
CHANNELS_RESPONSE=$(curl "https://slack.com/api/$API_TYPE.list?token=$TOKEN")
CHANNEL_ID=$(echo $CHANNELS_RESPONSE | jq ".${API_TYPE}[]" | jq "if .name == \"$CHANNEL\" then .id else null end" -M -c -r | grep -v null)
# post message
curl -X POST \
-F token=$TOKEN \
-F channel=$CHANNEL_ID \
-F "text=$MESSAGE" \
-F username=$NICK \
https://slack.com/api/chat.postMessage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment