Skip to content

Commit

Permalink
Merge pull request #55 from Hoten/master
Browse files Browse the repository at this point in the history
Fix bugs with --streaming option
  • Loading branch information
Chikashi-Kato committed Jul 23, 2018
2 parents a9e97cf + acc9dbc commit 86522a9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
40 changes: 33 additions & 7 deletions slacktee.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ attachment="" # Default color of the attachments. If an empty string is s
me=$(basename "$0")
title=""
mode="buffering"
streaming_batch_time=1
link=""
textWrapper="\`\`\`"
parseMode=""
Expand Down Expand Up @@ -83,6 +84,7 @@ options:
-h, --help Show this help.
-n, --no-buffering Post input values without buffering.
--streaming Post input as it comes in, and update one comment with further input.
--streaming-batch-time n Only update streaming slack output every n seconds. Defaults to 1.
-f, --file Post input values as a file.
-l, --link Add a URL link to the message.
-c, --channel channel_name Post input values to specified channel or user.
Expand Down Expand Up @@ -121,7 +123,7 @@ function send_message()
found_pattern_prefix=""
fi

wrapped_message=$(printf '%s\n%s\n%s' "$textWrapper" "$message" "$textWrapper")
wrapped_message=$(echo "$textWrapper\n$message\n$textWrapper")
message_attr=""
if [[ $message != "" ]]; then
if [[ -n $attachment ]]; then
Expand Down Expand Up @@ -194,7 +196,13 @@ function send_message()

if [[ $mode == "streaming" ]]; then
if [[ -z "$streaming_ts" ]]; then
post_result=$(curl -d "token=$token&username=$username&icon_url=$icon_url&icon_emoji=$icon_emoji&$parseModeUrlEncoded&channel=$channel&text=$wrapped_message" -X POST https://slack.com/api/chat.postMessage 2> /dev/null)
json="{\
\"channel\": \"$channel\", \
\"username\": \"$username\", \
$message_attr \"icon_emoji\": \"$icon_emoji\", \
\"icon_url\": \"$icon_url\" $parseMode}"

post_result=$(curl -H "Authorization: Bearer $token" -H 'Content-type: application/json; charset=utf-8' -X POST -d "$json" https://slack.com/api/chat.postMessage 2> /dev/null)
if [ $? != 0 ]; then
err_exit 1 "$post_result"
fi
Expand All @@ -205,9 +213,20 @@ function send_message()
# timestamp is used as the message id
streaming_ts="$(echo "$post_result" | awk 'match($0, /ts":"([^"]*)"/) {print substr($0, RSTART+5, RLENGTH-6)}'|sed 's/\\//g')"
else
post_result=$(curl -d "token=$token&channel=$streaming_channel_id&ts=$streaming_ts&text=$wrapped_message" -X POST https://slack.com/api/chat.update 2> /dev/null)
if [ $? != 0 ]; then
err_exit 1 "$post_result"
# batch updates every $streaming_batch_time seconds
now=$(date '+%s')
if [ -z "$streaming_last_update" ] || [ "$now" -ge $[streaming_last_update + streaming_batch_time] ]; then
streaming_last_update="$now"
json="{\
\"channel\": \"$streaming_channel_id\", \
\"ts\": \"$streaming_ts\", \
$message_attr \"icon_emoji\": \"$icon_emoji\", \
$parseMode}"

post_result=$(curl -H "Authorization: Bearer $token" -H 'Content-type: application/json; charset=utf-8' -X POST -d "$json" https://slack.com/api/chat.update 2> /dev/null)
if [ $? != 0 ]; then
err_exit 1 "$post_result"
fi
fi
fi
else
Expand Down Expand Up @@ -281,14 +300,14 @@ function process_line()
send_message "$text"
text="$line"
else
text=$(printf '%s\n%s' "$text" "$line")
text=$(echo "$text\n$line")
fi
fi
elif [[ $mode == "streaming" ]]; then
if [[ -z "$text" ]]; then
text="$line"
else
text=$(printf '%s\n%s' "$text" "$line")
text=$(echo "$text\n$line")
fi

send_message "$text"
Expand Down Expand Up @@ -415,6 +434,10 @@ function parse_args()
--streaming)
mode="streaming"
;;
--streaming-batch-time)
streaming_batch_time="$1"
shift
;;
-f|--file)
mode="file"
;;
Expand Down Expand Up @@ -739,6 +762,9 @@ function main()

if [[ "$mode" == "buffering" ]]; then
send_message "$text"
elif [[ "$mode" == "streaming" ]]; then
unset streaming_last_update
send_message "$text"
elif [[ "$mode" == "file" ]]; then
if [[ -s "$filename" ]]; then
channels_param=""
Expand Down
35 changes: 35 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,39 @@ cat $DATA | $SLACKTEE '--no-output' '-t' 'Suppress the standard output (--no-out
done
} | $SLACKTEE --streaming

# Test 20: Streaming batches updates
{
echo "let's count to 20, quickly!"
for i in {1..20}; do
echo $i
sleep 0.1
done
} | $SLACKTEE --streaming

# Test 21: streaming-batch-time
{
echo "The answer to life, the universe, and everything is"
for i in {1..20}; do
echo '.'
sleep 0.3
done
echo 42
} | $SLACKTEE --streaming --streaming-batch-time 2

# Test 22: Streaming mode - test payload is properly escaped
echo "hello ampersand &, equals =, quote ', and double quote \", please don't break my payload." | $SLACKTEE --streaming --streaming-batch-time 2

# Test 23: Newlines show correctly
echo -e "--streaming line one\n\nline three" | $SLACKTEE --streaming
echo -e "--streaming -p line one\n\nline three" | $SLACKTEE --streaming -p
echo -e "line one\n\nline three" | $SLACKTEE
echo -e "-p line one\n\nline three" | $SLACKTEE -p

# Test 24: Long messages
long_message=$(printf '.%.0s' {1..5000})
echo $long_message | $SLACKTEE -p # should be split up over two messages
# these do not work correctly. Fixing seems complicated, and it's an edge case, so let's just document it here
# echo $long_message | $SLACKTEE --streaming
# echo $long_message | $SLACKTEE

echo "Test is done!"

0 comments on commit 86522a9

Please sign in to comment.