Skip to content

Instantly share code, notes, and snippets.

@ypchen
Last active February 17, 2019 11:40
Show Gist options
  • Save ypchen/b464cc9ed30e7d66a583d76b38e13809 to your computer and use it in GitHub Desktop.
Save ypchen/b464cc9ed30e7d66a583d76b38e13809 to your computer and use it in GitHub Desktop.
Send a message to Slack
#!/bin/bash
# default
if [ -z ${CURL+x} ]; then
CURL="/usr/bin/curl"
fi
if [ -z ${ECHO+x} ]; then
ECHO="/usr/bin/echo"
fi
if [ -z ${CUT+x} ]; then
CUT="/usr/bin/cut"
fi
# webhook_url, MSG_PREFIX, & CURL, ECHO, CUT if necessary
CONF="${HOME}/.Slack.conf"
source ${CONF}
if [ -z ${MSG_PREFIX+x} ]; then
MSG_PREFIX=""
fi
if [ -z ${MSG_APPENDIX+x} ]; then
MSG_APPENDIX=""
fi
if [ -z ${text+x} ]; then
text="${1}"
fi
prefixMode=$(${ECHO} "${text}" | ${CUT} -c 1-4)
if [ "${prefixMode}" == "YPM0" ]; then
text=$(${ECHO} "${text}" | ${CUT} -c 5-)
elif [ "${prefixMode}" == "YPM1" ]; then
text=$(${ECHO} "${text}" | ${CUT} -c 5-)
msg=$(eval ${ECHO} "${text}")
text=$(${ECHO} -e "${msg}")
fi
text="${MSG_PREFIX}${text}${MSG_APPENDIX}"
curl --insecure -X POST -H 'Content-type: application/json' --data '{"text":"'"${text}"'"}' "${webhook_url}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment