-
-
Save dopiaza/6449505 to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# Usage: slackpost <token> <channel> <message> | |
# Enter the name of your slack host here - the thing that appears in your URL: | |
# https://slackhost.slack.com/ | |
slackhost=PUT_YOUR_HOST_HERE | |
token=$1 | |
if [[ $token == "" ]] | |
then | |
echo "No token specified" | |
exit 1 | |
fi | |
shift | |
channel=$1 | |
if [[ $channel == "" ]] | |
then | |
echo "No channel specified" | |
exit 1 | |
fi | |
shift | |
text=$* | |
if [[ $text == "" ]] | |
then | |
echo "No text specified" | |
exit 1 | |
fi | |
escapedText=$(echo $text | sed 's/"/\"/g' | sed "s/'/\'/g" ) | |
json="{\"channel\": \"#$channel\", \"text\": \"$escapedText\"}" | |
curl -s -d "payload=$json" "https://$slackhost.slack.com/services/hooks/incoming-webhook?token=$token" |
@PetroPliuta - You may have found the answer to your question, but when targeting a user, you have to use their id instead of their name.
Example: "<@U3BV0GGY3>" or "U3BV0GGY3" depending on the API usage
In Slack: Click the user, then click their name and go to "View 's profile", then click the Down Arrow next to "Message" and you'll see "Copy member id".
You can also see this if you visit the following link (while you're logged into slack), click your name next to the "user" field and click "Test Method". This will show you a bunch of information about the user.
https://api.slack.com/methods/users.info/test
Here's a version which uses argument flags:
#!/usr/bin/env bash
# Usage: slackpost -w <webhook_url> -c <channel> -u <username> -m <message> [-a <alert_type>]
# Usage: echo <message> | slackpost -w <webhook_url> -c <channel> -u <username> [-a <alert_type>]
# exit immediately if a command exits with a non-zero status
set -e
# error if variable referenced before being set
set -u
# produce failure return code if any command fails in pipe
set -o pipefail
# accepted values: good, warning, danger
alert_type=""
channel=""
message=""
username=""
webhook_url=""
# colon after var means it has a value rather than it being a bool flag
while getopts 'a:c:m:u:w:' OPTION; do
case "$OPTION" in
a)
alert_type="$OPTARG"
;;
c)
channel="$OPTARG"
;;
m)
message="$OPTARG"
;;
u)
username="$OPTARG"
;;
w)
webhook_url="$OPTARG"
;;
?)
echo "script usage: $(basename $0) {-c channel} {-m message} {-u username} {-w webhook} [-a alert_type]" >&2
exit 1
;;
esac
done
shift "$(($OPTIND -1))"
# exit if channel not provided
if [[ -z "$channel" ]]
then
echo "No channel specified"
exit 1
fi
# read piped data as message if message argument is not provided
if [[ -z "$message" ]]
then
message=$*
while IFS= read -r line; do
message="$message$line\n"
done
fi
# exit if username not provided
if [[ -z "$username" ]]
then
echo "No username specified"
exit 1
fi
# exit if webhook not provided
if [[ -z "$webhook_url" ]]
then
echo "No webhook_url specified"
exit 1
fi
# escape message text
escapedText=$(echo $message | sed 's/"/\"/g' | sed "s/'/\'/g")
# create JSON payload
json="{\"channel\": \"$channel\", \"username\":\"$username\", \"icon_emoji\":\"ghost\", \"attachments\":[{\"color\":\"$alert_type\" , \"text\": \"$escapedText\"}]}"
# fire off slack message post
curl -s -d "payload=$json" "$webhook_url"
Can you explain for posting slash command using api ?
I should upload my version, it's updated to use the API better ...
Don't add the payload key before json variable.
And add the token in the headers.
curl -v \
-H 'Content-Type:application/json; charset=utf-8' \
-H 'Authorization: Bearer xoxp-XXXXXX-XXXX-XXXXXXXX \
--data "$json" \
"$slackwebhook"
Ask Linux system to send the traditional Chinese message to slack, and it will change to the code! How do you deal with it? Thx