Skip to content

Commit

Permalink
'treehouses message slack <action> <channelname>' (fixes #2019) (#2020)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <dogi@users.noreply.github.com>
  • Loading branch information
Neethu-Mohan and dogi authored Nov 21, 2020
1 parent 2d7c07c commit 5f75ace
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 45 deletions.
91 changes: 47 additions & 44 deletions modules/message.sh
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,31 @@ function message {
esac
;;
slack)
function check_channel {
channel_list=$(curl -s -F token=$access_token https://slack.com/api/conversations.list)
channel_list=($(echo $channel_list | python -m json.tool | jq '.channels[].id' | tr -d '"'))
for i in "${channel_list[@]}"; do
if [ $i == $1 ]; then
return 0
break
fi
done
return 1
}
function get_channel_id {
name="$1"
count=0
conversation_list=$(curl -s -F token=$access_token https://slack.com/api/conversations.list)
channel_name=($(echo $conversation_list | python -m json.tool | jq '.channels[].name' | tr -d '"'))
for i in "${channel_name[@]}"; do
if [[ $name == "$i" ]]; then
channel_id=$(echo $conversation_list | python -m json.tool | jq '.channels['$count'].id' | tr -d '"')
echo $channel_id
break
fi
((count=count+1))
done
}
case "$2" in
apitoken)
if [[ $3 != "" ]]; then
Expand Down Expand Up @@ -250,8 +275,10 @@ function message {
send)
channel=$3
if check_apitoken slack; then
if [[ $3 == "" ]]; then
log_comment_and_exit1 "ERROR: Group information is missing" "usage: $BASENAME message slack send <group>"
if [[ $channel == "" ]]; then
log_comment_and_exit1 "ERROR: Channel information is missing" "usage: $BASENAME message slack send <group>"
elif [[ "${channel:0:1}" == [a-z] ]]; then
channel=$(get_channel_id $3)
fi
shift; shift; shift;
message=$*
Expand All @@ -268,22 +295,14 @@ function message {
;;
show)
channel=$3
function check_channel {
channel_list=$(curl -s -F token=$access_token https://slack.com/api/conversations.list)
channel_list=($(echo $channel_list | python -m json.tool | jq '.channels[].id' | tr -d '"'))
for i in "${channel_list[@]}"; do
if [ $i == $1 ]; then
return 0
break
fi
done
return 1
}
if check_apitoken slack; then
if [[ $channel == "" ]]; then
log_comment_and_exit1 "ERROR: Group information is missing" "usage: $BASENAME message slack read <group>"
elif ! check_channel $channel; then
log_and_exit1 "invalid channel ID"
log_comment_and_exit1 "ERROR: Channel information is missing" "usage: $BASENAME message slack read <group>"
elif [[ "${channel:0:1}" == [a-z] ]]; then
channel=$(get_channel_id $3)
fi
if ! check_channel $channel; then
log_and_exit1 "invalid channel ID or channel name"
else
channel_info=$(curl -s -F token=$access_token -F channel=$channel https://slack.com/api/conversations.info)
last_read=$(echo $channel_info | python -m json.tool | jq '.channel.last_read' | tr -d '"')
Expand Down Expand Up @@ -322,22 +341,14 @@ function message {
;;
read)
channel=$3
function check_channel {
channel_list=$(curl -s -F token=$access_token https://slack.com/api/conversations.list)
channel_list=($(echo $channel_list | python -m json.tool | jq '.channels[].id' | tr -d '"'))
for i in "${channel_list[@]}"; do
if [ $i == $1 ]; then
return 0
break
fi
done
return 1
}
if check_apitoken slack; then
if [[ $channel == "" ]]; then
log_comment_and_exit1 "ERROR: Group information is missing" "usage: $BASENAME message slack read <group>"
elif ! check_channel $channel; then
log_and_exit1 "invalid channel ID"
log_comment_and_exit1 "ERROR: Channel information is missing" "usage: $BASENAME message slack read <group>"
elif [[ "${channel:0:1}" == [a-z] ]]; then
channel=$(get_channel_id $3)
fi
if ! check_channel $channel; then
log_and_exit1 "invalid channel ID or channel name"
else
channel_info=$(curl -s -F token=$access_token -F channel=$channel https://slack.com/api/conversations.info)
last_read=$(echo $channel_info | python -m json.tool | jq '.channel.last_read' | tr -d '"')
Expand Down Expand Up @@ -378,22 +389,14 @@ function message {
;;
mark)
channel=$3
function check_channel {
channelList=$(curl -s -F token=$access_token https://slack.com/api/conversations.list)
channelList=($(echo $channelList | python -m json.tool | jq '.channels[].id' | tr -d '"'))
for i in "${channelList[@]}"; do
if [ $i == $1 ]; then
return 0
break
fi
done
return 1
}
if check_apitoken slack; then
if [[ $channel == "" ]]; then
log_comment_and_exit1 "ERROR: Group information is missing" "usage: $BASENAME message slack read <group>"
elif ! check_channel $channel; then
log_and_exit1 "invalid channel ID"
log_comment_and_exit1 "ERROR: Channel information is missing" "usage: $BASENAME message slack read <group>"
elif [[ "${channel:0:1}" == [a-z] ]]; then
channel=$(get_channel_id $3)
fi
if ! check_channel $channel; then
log_and_exit1 "invalid channel ID or channel name"
else
channel_info=$(curl -s -F token=$access_token -F channel=$channel https://slack.com/api/conversations.info)
last_read=$(echo $channel_info | python -m json.tool | jq '.channel.last_read' | tr -d '"')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@treehouses/cli",
"version": "1.24.85",
"version": "1.24.86",
"remote": "4000",
"description": "Thin command-line interface for Raspberry Pi low level configuration.",
"main": "cli.sh",
Expand Down

0 comments on commit 5f75ace

Please sign in to comment.