diff --git a/README.md b/README.md index abf00d769..c6b189d37 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,8 @@ resolution sets the screen resolution system [cpu|ram|disk|volt|temperature] display real system informations message gitter sets api/channel info in config file and sends/recieves message - slack sets api/channel info in config file and sends message + slack sets api/channel info in config file, shows channels and sends message + read new messages shutdown [now|in|force] shutdown the system ``` diff --git a/_treehouses b/_treehouses index 719d16b16..dfc7a4fa2 100644 --- a/_treehouses +++ b/_treehouses @@ -238,6 +238,7 @@ treehouses message gitter mark treehouses message slack apitoken treehouses message slack channels treehouses message slack send +treehouses message slack read treehouses networkmode treehouses networkmode info treehouses ntp internet diff --git a/modules/help.sh b/modules/help.sh index 4f0d5af66..171936c40 100644 --- a/modules/help.sh +++ b/modules/help.sh @@ -142,7 +142,8 @@ resolution sets the screen resolution system [cpu|ram|disk|volt|temperature] display real system informations message gitter sets api/channel info in config file and sends/recieves message - slack sets api/channel info in config file and sends message + slack sets api/channel info in config file, shows channels and sends message + reads new messages shutdown [now|in|force] shutdown the system EOF echo "$helpdefault" diff --git a/modules/message.sh b/modules/message.sh index b608391ef..9fb3e014c 100644 --- a/modules/message.sh +++ b/modules/message.sh @@ -232,7 +232,7 @@ function message { echo "To get an authorized access token" echo "" echo "Navigate to https://api.slack.com/apps and create an APP. Provide a name for the APP and select the \"Development Slack Workspace (eg : Open Learning Exchange)\" from the drop down list" - echo "Go to \"OAuth & Permission\" under \"features \" and select the scope under \"User Token Scopes\" and add \"chat:write\" for the APP from the drop down list" + echo "Go to \"OAuth & Permission\", select the scope from \"User Token Scopes\" and add \"chat:write\", \"channel:read\", \"channel:history\", \"group:read\" and \"users.read\" for the APP from the drop down list" echo "Then install APP to the workspace and click the allow button to give permissions in the redirected link and then you will get the \"OAuth access token\"" echo "Run $BASENAME message slack apitoken " fi @@ -266,6 +266,62 @@ function message { log_comment_and_exit1 "Error:You do not have an authorized access token" "To get access token, run $BASENAME message slack apitoken" fi ;; + 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 " + elif ! check_channel $channel; then + log_and_exit1 "invalid channel ID" + 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 '"') + channel_history=$(curl -s -F token=$access_token -F channel=$channel https://slack.com/api/conversations.history) + time=($(echo $channel_history | python -m json.tool | jq '.messages[].ts' 2> /dev/null | tr -d '"')) + unread_time=() + for i in "${time[@]}"; do + if [ $last_read == $i ]; then + break + else + unread_time+=($i) + fi + done + unread_time=($(printf '%s\n' "${unread_time[@]}" | tac | tr '\n' ' ')) + if [ ${#unread_time[@]} -eq 0 ]; then + echo "You have no unread messages at the moment" + else + for i in "${unread_time[@]}"; do + msg_info=$(curl -s -F token=$access_token -F channel=$channel -F latest=$i -F limit=1 -F inclusive=true https://slack.com/api/conversations.history) + curl -s -F token=$access_token -F channel=$channel -F ts=$i https://slack.com/api/conversations.mark > "$LOGFILE" + msg=$(echo $msg_info | python -m json.tool | jq '.messages[].text' | tr -d '"') + userid=$(echo $msg_info | python -m json.tool | jq '.messages[].user' | tr -d '"') + name_info=$(curl -s -F token=$access_token -F user=$userid -F latest=$i https://slack.com/api/users.info) + name=$(echo $name_info | python -m json.tool | jq '.user.profile.real_name' | tr -d '"') + curl -s -F token=$access_token -F channel=$channel -F ts=$i https://slack.com/api/conversations.mark > "$LOGFILE" + time_info=$(date -d @$i) + date=$(echo ${time_info} | cut -d " " -f1-3) + year=$(echo ${time_info} | cut -d " " -f6) + send_time=$(echo ${time_info} | cut -d " " -f4) + echo "Date: $date $year" + echo "Time: $send_time" + echo "From: $name" + echo "Message: $msg" + done + fi + fi + fi + ;; *) log_help_and_exit1 "Error: This command does not exist" message esac @@ -315,4 +371,7 @@ function message_help { echo " $BASENAME message slack send \"channel_name or channel ID\" \"Hi, you are very awesome\"" echo " Sends a message to a slack channel using channel name, eg, channel: #channel_name" echo + echo " $BASENAME message slack read \"channel ID\"" + echo " Reads messages from a slack channel using channelID" + echo } diff --git a/package.json b/package.json index b0e0e72f1..5b41c48f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@treehouses/cli", - "version": "1.24.83", + "version": "1.24.84", "remote": "4000", "description": "Thin command-line interface for Raspberry Pi low level configuration.", "main": "cli.sh",