diff --git a/README.md b/README.md index c6b189d37..3dd1b915c 100644 --- a/README.md +++ b/README.md @@ -164,10 +164,10 @@ magazines downloads specific magazine issue as a [url] shows the homepage URL of magazine 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 +message gitter sets api/channel info in config file and sends/recieves messages in gitter - slack sets api/channel info in config file, shows channels and sends message - read new messages + slack sets api/channel info in config file and sends/recieves messages in slack + shutdown [now|in|force] shutdown the system ``` diff --git a/_treehouses b/_treehouses index dfc7a4fa2..f9ef95395 100644 --- a/_treehouses +++ b/_treehouses @@ -238,6 +238,8 @@ treehouses message gitter mark treehouses message slack apitoken treehouses message slack channels treehouses message slack send +treehouses message slack show +treehouses message slack mark treehouses message slack read treehouses networkmode treehouses networkmode info diff --git a/modules/help.sh b/modules/help.sh index 171936c40..79e9946fe 100644 --- a/modules/help.sh +++ b/modules/help.sh @@ -140,10 +140,10 @@ magazines downloads specific magazine issue as a [url] shows the homepage URL of magazine 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 +message gitter sets api/channel info in config file and sends/recieves messages in gitter - slack sets api/channel info in config file, shows channels and sends message - reads new messages + slack sets api/channel info in config file and sends/recieves messages in slack + shutdown [now|in|force] shutdown the system EOF echo "$helpdefault" diff --git a/modules/message.sh b/modules/message.sh index 9fb3e014c..8309149cd 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\", 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 "Go to \"OAuth & Permission\" under \"features \" and select the scope under \"User Token Scopes\" and add \"chat:write\", \"channels:write\", \"channel:read\", \"channel:history\", \"groups:write\", \"group:read\", \"mpim:write\", \"im:write\" 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,60 @@ 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 ;; + 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 " + 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) + 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 '"') + 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 + ;; read) channel=$3 function check_channel { @@ -322,6 +376,49 @@ function message { fi fi ;; + 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 " + 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 + curl -s -F token=$access_token -F channel=$channel -F ts=$i https://slack.com/api/conversations.mark > "$LOGFILE" + done + echo "Done. Messages marked as read " + fi + fi + fi + ;; *) log_help_and_exit1 "Error: This command does not exist" message esac @@ -371,7 +468,13 @@ 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 show \"channel ID\"" + echo " Shows messages of a slack channel using channel ID" + echo echo " $BASENAME message slack read \"channel ID\"" echo " Reads messages from a slack channel using channelID" echo + echo " $BASENAME message slack mark \"channel ID\"" + echo " Marks messages of a slack channel using channel ID" + echo } diff --git a/package.json b/package.json index 5b41c48f5..521be382c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@treehouses/cli", - "version": "1.24.84", + "version": "1.24.85", "remote": "4000", "description": "Thin command-line interface for Raspberry Pi low level configuration.", "main": "cli.sh",