Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ resolution <cea|dmt [modes]> sets the screen resolution
system [cpu|ram|disk|volt|temperature] display real system informations
message gitter <apitoken|authorize> sets api/channel info in config file and sends/recieves message
<send|show|read|mark>
slack <apitoken|send|channels> sets api/channel info in config file and sends message
slack <apitoken|send|channels> sets api/channel info in config file, shows channels and sends message
<read> read new messages
shutdown [now|in|force] shutdown the system
```

Expand Down
1 change: 1 addition & 0 deletions _treehouses
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion modules/help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ resolution <cea|dmt [modes]> sets the screen resolution
system [cpu|ram|disk|volt|temperature] display real system informations
message gitter <apitoken|authorize> sets api/channel info in config file and sends/recieves message
<send|show|read|mark>
slack <apitoken|send|channels> sets api/channel info in config file and sends message
slack <apitoken|channels|send> sets api/channel info in config file, shows channels and sends message
<read> reads new messages
shutdown [now|in|force] shutdown the system
EOF
echo "$helpdefault"
Expand Down
61 changes: 60 additions & 1 deletion modules/message.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <oauth access token>"
fi
Expand Down Expand Up @@ -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 <group>"
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
Expand Down Expand Up @@ -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
}
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.83",
"version": "1.24.84",
"remote": "4000",
"description": "Thin command-line interface for Raspberry Pi low level configuration.",
"main": "cli.sh",
Expand Down