slack.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # #No shebang necessary
  2. # BASH Lib: Simple incoming webhook for slack integration.
  3. #
  4. # The script expects the following parameters to be defined by the upper layer:
  5. # SLACK_NOTIFY_WEBHOOK_URL
  6. # SLACK_BOT_NAME
  7. # SLACK_CHANNEL
  8. #
  9. # Copyright:
  10. #
  11. # Author: Pavlos Emm. Katsoulakis <paul@netdata.cloud
  12. post_message() {
  13. TYPE="$1"
  14. MESSAGE="$2"
  15. CUSTOM_CHANNEL="$3"
  16. case "$TYPE" in
  17. "PLAIN_MESSAGE")
  18. curl -X POST --data-urlencode "payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_BOT_NAME}\", \"text\": \"${MESSAGE}\", \"icon_emoji\": \":space_invader:\"}" "${SLACK_NOTIFY_WEBHOOK_URL}"
  19. ;;
  20. "TRAVIS_MESSAGE")
  21. if [ "${TRAVIS_EVENT_TYPE}" == "pull_request" ] || [ "${TRAVIS_BRANCH}" != "master" ] ; then
  22. echo "Skipping notification due to build type."
  23. return 0
  24. fi
  25. if [ -n "${CUSTOM_CHANNEL}" ]; then
  26. echo "Sending travis message to custom channel ${CUSTOM_CHANNEL}"
  27. OPTIONAL_CHANNEL_INFO="\"channel\": \"${CUSTOM_CHANNEL}\","
  28. fi
  29. POST_MESSAGE="{
  30. ${OPTIONAL_CHANNEL_INFO}
  31. \"text\": \"${TRAVIS_REPO_SLUG}, ${MESSAGE}\",
  32. \"attachments\": [{
  33. \"text\": \"${TRAVIS_JOB_NUMBER}: Event type '${TRAVIS_EVENT_TYPE}', on '${TRAVIS_OS_NAME}' \",
  34. \"fallback\": \"I could not determine the build\",
  35. \"callback_id\": \"\",
  36. \"color\": \"#3AA3E3\",
  37. \"attachment_type\": \"default\",
  38. \"actions\": [
  39. {
  40. \"name\": \"${TRAVIS_BUILD_NUMBER}\",
  41. \"text\": \"Build #${TRAVIS_BUILD_NUMBER}\",
  42. \"type\": \"button\",
  43. \"url\": \"${TRAVIS_BUILD_WEB_URL}\"
  44. },
  45. {
  46. \"name\": \"${TRAVIS_JOB_NUMBER}\",
  47. \"text\": \"Job #${TRAVIS_JOB_NUMBER}\",
  48. \"type\": \"button\",
  49. \"url\": \"${TRAVIS_JOB_WEB_URL}\"
  50. }]
  51. }]
  52. }"
  53. echo "Sending ${POST_MESSAGE}"
  54. curl -X POST --data-urlencode "payload=${POST_MESSAGE}" "${SLACK_NOTIFY_WEBHOOK_URL}"
  55. ;;
  56. *)
  57. echo "Unrecognized message type \"$TYPE\" was given"
  58. return 1
  59. ;;
  60. esac
  61. }