Skip to content

Instantly share code, notes, and snippets.

@gecko655
Created May 8, 2017 06:03
Show Gist options
  • Save gecko655/06c7d74120bccc6246fd708e9e6316e4 to your computer and use it in GitHub Desktop.
Save gecko655/06c7d74120bccc6246fd708e9e6316e4 to your computer and use it in GitHub Desktop.
bash (とsed (とperl……)) があれば動く
#!/bin/bash
# Usage: slackpost "<webhook_url>" "<channel>" "<username>" "<message>" ["<attached_message>"]
export LANG=ja_JP.utf8
# ------------
webhook_url=$1
if [[ $webhook_url == "" ]]
then
echo "No webhook_url specified"
exit 1
fi
# ------------
shift
channel=$1
if [[ $channel == "" ]]
then
echo "No channel specified"
exit 1
fi
# ------------
shift
username=$1
if [[ $username == "" ]]
then
echo "No username specified"
exit 1
fi
# ------------
shift
text=$1
escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" )
if [[ $text == "" ]]
then
echo "No text specified"
exit 1
fi
shift
attachedText="$1"
escapedAttachedText=$(echo "$attachedText" | sed 's/"/\"/g' | sed "s/'/\'/g" | perl -pe 's/\n/\\n/g' )
if [[ $attachedText != "" ]]
then
attachments=", \"attachments\":[{\"color\":\"danger\" , \"text\":\"$escapedAttachedText\"}]"
fi
json="{\"channel\": \"$channel\", \"username\":\"$username\", \"icon_emoji\":\":video_game:\", \"text\":\"$escapedText\" $attachments}"
echo $json
curl -s -d "payload=$json" "$webhook_url"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment