Skip to content

Instantly share code, notes, and snippets.

@alexleone
Last active July 25, 2022 16:00
Show Gist options
  • Save alexleone/0f04ed7c217cb1ff76e73d538f9d3f4f to your computer and use it in GitHub Desktop.
Save alexleone/0f04ed7c217cb1ff76e73d538f9d3f4f to your computer and use it in GitHub Desktop.
Slack Bot Command Line via Webhook
# This script is used to send a slack notification to the channel configured in the webhook url
# See this page for creating a webhook in slack https://slack.com/help/articles/115005265063-Incoming-webhooks-for-Slack
# To use this script, change the fallback values for the params below
# Update the path to the package.json or set the $VERSION variable before calling this script
# Usage afte updating the script just execute it to send the message ./slack-bot.sh
FALLBACK="${TITLE:-:rocket: Deployed app}"
PRETEXT="${TITLE:-:rocket: Deployed app}"
COLOR="${COLOR:-#36a64f}"
AUTHOR_NAME="${AUTHOR_NAME:-Custom Slack Bot}"
AUTHOR_LINK="${AUTHOR_LINK:-https://sprouttechlab.com}"
AUTHOR_ICON="${AUTHOR_ICON:-https://sprout-tech-assets.s3.amazonaws.com/icons/leaf-sprout-logo.png}"
TITLE="${TITLE:-Title}"
TITLE_LINK="${TITLE_LINK:-https://sprouttechlab.com}"
TEXT="${TEXT:-Text}"
FOOTER_IMAGE_URL="${FOOTER_IMAGE_URL:-https://sprout-tech-assets.s3.amazonaws.com/icons/leaf-32x32.png}"
THUMB_URL="${THUMB_URL:-https://sprout-tech-assets.s3.amazonaws.com/icons/leaf-32x32.png}"
FOOTER_TEXT="${FOOTER_TEXT:-Text}"
FOOTER_ICON_URL="${FOOTER_ICON_URL:-https://sprout-tech-assets.s3.amazonaws.com/icons/leaf-sprout-logo-black.png}"
TIMESTAMP=$(date +%s)
#This will read the version from your package.json - (Update path below or set $VERSION)
DEPLOYMENT_VERSION=$(grep -m1 version package.json | awk -F: '{ print $2 }' | sed 's/[", ]//g')
VERSION="${VERSION:-$DEPLOYMENT_VERSION}"
ENVIRONMENT="${ENVIRONMENT:-Unkonwn}"
FIELDS='"fields": [ { "title": "Environment", "value": "'${ENVIRONMENT}'", "short": true }, { "title": "Version", "value": "'${VERSION}'", "short": true } ]'
# Use this is you need to further customize the message
# FULL_MESSAGE_DATA='{ "attachments": [ { "fallback": "'${FALLBACK}'", "color": "'${COLOR}'", "pretext": "'${PRETEXT}'", "author_name": "'${AUTHOR_NAME}'", "author_link": "'${AUTHOR_LINK}'", "author_icon": "'${AUTHOR_ICON}'", "title": "'${TITLE}'", "title_link": "'${TITLE_LINK}'", "text": "'${TEXT}'", "image_url": "'${FOOTER_IMAGE_URL}'", "thumb_url": "'${THUMB_URL}'", "footer": "'${FOOTER_TEXT}'", "footer_icon": "'${FOOTER_ICON_URL}'", "ts": '${TIMESTAMP}' } ] }';
SIMPLE_MESSAGE_DATA='{ "attachments": [ { "fallback": "'${FALLBACK}'", "color": "'${COLOR}'", "author_name": "'${AUTHOR_NAME}'", "author_link": "'${AUTHOR_LINK}'", "author_icon": "'${AUTHOR_ICON}'", "title": "'${TITLE}'", "title_link": "'${TITLE_LINK}'", "text": "'${TEXT}'", "footer": "'${FOOTER_TEXT}'", "footer_icon": "'${FOOTER_ICON_URL}'", "ts": '${TIMESTAMP}', '${FIELDS}' } ] }';
# Update the hook url to the slack channel
SLACK_HOOK_URL="https://hooks.slack.com/services/XXXXX/XYXYXYXYXYXYXYXYXYYXYXYXYYXYX"
curl -X POST -H 'Content-type: application/json' --data "$SIMPLE_MESSAGE_DATA" ${SLACK_HOOK_URL}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment