├── .gitignore ├── LICENSE ├── README.md └── postToSlack.sh /.gitignore: -------------------------------------------------------------------------------- 1 | # webstorm 2 | .idea -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Sul Aga 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Post messages to slack from Bash 2 | This source code is for blog Post messages to slack from Bash -------------------------------------------------------------------------------- /postToSlack.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | function usage { 4 | programName=$0 5 | echo "description: use this program to post messages to Slack channel" 6 | echo "usage: $programName [-t \"sample title\"] [-b \"message body\"] [-c \"mychannel\"] [-u \"slack url\"]" 7 | echo " -t the title of the message you are posting" 8 | echo " -b The message body" 9 | echo " -c The channel you are posting to" 10 | echo " -u The slack hook url to post to" 11 | exit 1 12 | } 13 | 14 | while getopts ":t:b:c:u:h" opt; do 15 | case ${opt} in 16 | t) msgTitle="$OPTARG" 17 | ;; 18 | u) slackUrl="$OPTARG" 19 | ;; 20 | b) msgBody="$OPTARG" 21 | ;; 22 | c) channelName="$OPTARG" 23 | ;; 24 | h) usage 25 | ;; 26 | \?) echo "Invalid option -$OPTARG" >&2 27 | ;; 28 | esac 29 | done 30 | 31 | if [[ ! "${msgTitle}" || ! "${slackUrl}" || ! "${msgBody}" || ! "${channelName}" ]]; then 32 | echo "all arguments are required" 33 | usage 34 | fi 35 | 36 | read -d '' payLoad << EOF 37 | { 38 | "channel": "#${channelName}", 39 | "username": "$(hostname)", 40 | "icon_emoji": ":sunglasses:", 41 | "attachments": [ 42 | { 43 | "fallback": "${msgTitle}", 44 | "color": "good", 45 | "title": "${msgTitle}", 46 | "fields": [{ 47 | "title": "message", 48 | "value": "${msgBody}", 49 | "short": false 50 | }] 51 | } 52 | ] 53 | } 54 | EOF 55 | 56 | 57 | statusCode=$(curl \ 58 | --write-out %{http_code} \ 59 | --silent \ 60 | --output /dev/null \ 61 | -X POST \ 62 | -H 'Content-type: application/json' \ 63 | --data "${payLoad}" ${slackUrl}) 64 | 65 | echo ${statusCode} --------------------------------------------------------------------------------