Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
7c83b4f
set up connection to API
seaweeddol Mar 11, 2020
c9aae5e
initialize files
seaweeddol Mar 11, 2020
4a13baa
add details method to User
seaweeddol Mar 12, 2020
bc2d026
add self.list_all method to Channel and User
seaweeddol Mar 12, 2020
1c4b9be
add self.get method to Recipient
seaweeddol Mar 14, 2020
0109b06
update to use self.get inherited from Recipient
seaweeddol Mar 14, 2020
23ce451
add user options for CLI
seaweeddol Mar 14, 2020
05be3b5
add select_channel method to Workspace
seaweeddol Mar 14, 2020
8e6d195
add tests for Recipient class
seaweeddol Mar 14, 2020
863efbc
require dotenv gem in test_helper
seaweeddol Mar 14, 2020
1d3a5fb
add tests for User class
seaweeddol Mar 14, 2020
d399958
update Channel.list_all to return array of Channel objects
seaweeddol Mar 14, 2020
8935712
update User.list_all to return array of User objects
seaweeddol Mar 14, 2020
a43b9e8
add tests for Channel class
seaweeddol Mar 14, 2020
ae74de1
remove unnecessary test from Recipient
seaweeddol Mar 14, 2020
a2503a6
add dotenv
seaweeddol Mar 14, 2020
c67e015
create list_options method and add table_print gem
seaweeddol Mar 14, 2020
362a177
add cassettes
seaweeddol Mar 14, 2020
f776ffe
fix comparison to upcase input & add select_user method
seaweeddol Mar 14, 2020
f2ea83e
add init test to Channel and remove from Recipient
seaweeddol Mar 14, 2020
58968f4
add tests for Workspace - initialize, select_channel, select_user
seaweeddol Mar 14, 2020
5fb346b
add logic for no match found to select_channel and select_user
seaweeddol Mar 14, 2020
a838023
add details method to User and Channel
seaweeddol Mar 14, 2020
505066d
add logic for show_details when @selected is empty
seaweeddol Mar 14, 2020
bfe927e
add options for select user, select channel, and details
seaweeddol Mar 14, 2020
3fa4009
add cassettes
seaweeddol Mar 14, 2020
cdd3875
clean up comments and spacing
seaweeddol Mar 15, 2020
9bf4bb4
add select helper method to Workspace
seaweeddol Mar 15, 2020
8fd02fa
add send_message method to Recipient
seaweeddol Mar 15, 2020
78dd46c
add send_message tests to Channel and User
seaweeddol Mar 15, 2020
bc7221e
update to fix warning messages appearing in rake
seaweeddol Mar 15, 2020
0536b69
add send message option to cli
seaweeddol Mar 15, 2020
d984472
add cassettes
seaweeddol Mar 15, 2020
9ee627c
refactor and clean up code
seaweeddol Mar 15, 2020
5ecd54e
break up main into methods for each option
seaweeddol Mar 15, 2020
3a18cb5
add customize_bot option
seaweeddol Mar 16, 2020
fd4f928
add readers to User
seaweeddol Mar 16, 2020
0095a2b
add customize_bot method to Workspace
seaweeddol Mar 16, 2020
de0f966
commit API call data
seaweeddol Mar 16, 2020
0d0abc9
small re-organization of code
seaweeddol Mar 23, 2020
c6dff6a
add new cassettes
seaweeddol Mar 23, 2020
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
30 changes: 30 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative "recipient"

class Channel < Recipient
attr_reader :topic, :member_count

def initialize(slack_id, name, topic, member_count)
super(slack_id, name)
@topic = topic
@member_count = member_count
end

def details
# return details formatted with table_print gem
return tp self, :slack_id, :name, :topic, :member_count
end

def self.list_all
response = Channel.get("conversations.list")
all = response["channels"].map do |channel|
Channel.new(
channel["name"],
channel["id"],
channel["topic"]["value"],
channel["num_members"]
)
end
return all
end

end
1 change: 1 addition & 0 deletions lib/json/bot-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"{\"profile\":{\"title\":\"\",\"phone\":\"\",\"skype\":\"\",\"real_name\":\"Jessica Stone\",\"real_name_normalized\":\"Jessica Stone\",\"display_name\":\"Jessica\",\"display_name_normalized\":\"Jessica\",\"fields\":null,\"status_text\":\"\",\"status_emoji\":\":rainbow:\",\"status_expiration\":0,\"avatar_hash\":\"g3f062ade197\",\"email\":\"jstone4815@gmail.com\",\"first_name\":\"Jessica\",\"last_name\":\"Stone\",\"image_24\":\"https:\\/\\/secure.gravatar.com\\/avatar\\/3f062ade197077d689d640dc2c62d378.jpg?s=24&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0006-24.png\",\"image_32\":\"https:\\/\\/secure.gravatar.com\\/avatar\\/3f062ade197077d689d640dc2c62d378.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0006-32.png\",\"image_48\":\"https:\\/\\/secure.gravatar.com\\/avatar\\/3f062ade197077d689d640dc2c62d378.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0006-48.png\",\"image_72\":\"https:\\/\\/secure.gravatar.com\\/avatar\\/3f062ade197077d689d640dc2c62d378.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0006-72.png\",\"image_192\":\"https:\\/\\/secure.gravatar.com\\/avatar\\/3f062ade197077d689d640dc2c62d378.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0006-192.png\",\"image_512\":\"https:\\/\\/secure.gravatar.com\\/avatar\\/3f062ade197077d689d640dc2c62d378.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2Fdf10d%2Fimg%2Favatars%2Fava_0006-512.png\",\"status_text_canonical\":\"\"},\"ok\":true,\"username\":\"jessica\"}"
59 changes: 59 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'httparty'
require 'dotenv'

Dotenv.load

BASE_URL = "https://slack.com/api/"
TOKEN = ENV["SLACK_TOKEN"]
QUERY = {
token: TOKEN,
pretty: 1
}

class Recipient
attr_reader :slack_id, :name

def initialize(slack_id, name)
@slack_id = slack_id
@name = name
end

def send_message(message)
response = HTTParty.post(BASE_URL + "chat.postMessage", {
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
query: {
token: TOKEN,
channel: @slack_id,
text: message
}
})

if response.code == 200 && response.parsed_response["ok"]
return true
else
raise SlackApiError, "We encountered a problem: #{response["error"]}"
end
end

def self.get(endpoint)
response = HTTParty.get(BASE_URL + endpoint, query: QUERY)
if response.code == 200 && response["ok"]
return response
else
raise SlackApiError, "We encountered a problem: #{response["error"]}"
end
end

def details
# implement in child class
end

def self.list_all
# implement in child class
end
end

class SlackApiError < Exception
end
88 changes: 86 additions & 2 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,96 @@
#!/usr/bin/env ruby
require_relative 'workspace'
require 'table_print'

WORKSPACE = Workspace.new

def main
puts "Welcome to the Ada Slack CLI!"
workspace = Workspace.new

# TODO project
puts "This workspace has #{WORKSPACE.users.length} users and #{WORKSPACE.channels.length} channels"

input = ""
until input == "quit"
puts "\nWhat would you like to do?"
list_options
input = gets.chomp.downcase

case input
when "1", "list users"
list_users
when "2", "list channels"
list_channels
when "3", "select user"
select_user
when "4", "select channel"
select_channel
when "5", "details"
show_details
when "6", "send message"
send_message
when "7", "customize bot"
customize_bot
when "8", "quit"
input = "quit"
else
puts "That's not a valid option, please try again."
end
end

puts "Thank you for using the Ada Slack CLI"
end

def list_options
options = ["list users", "list channels", "select user", "select channel", "details", "send message", "customize bot", "quit"]
options.each_with_index do |option, i|
puts "#{i + 1}. #{option}"
end
end

def send_message
if WORKSPACE.selected == ""
puts "Please select a recipient before sending a message."
return
end

message = ""
until message != ""
puts "Enter the message you would like to send to the selected recipient: "
message = gets.chomp
end
WORKSPACE.selected.send_message(message)
end

def list_users
tp WORKSPACE.users, :slack_id, :name, :real_name
end

def list_channels
tp WORKSPACE.channels, :slack_id, :name, :topic, :member_count
end

def select_user
print "\nProvide the name or Slack ID of the user you want to select: "
WORKSPACE.select_user(gets.chomp.upcase)
puts "No match found." if WORKSPACE.selected == ""
end

def select_channel
print "\nProvide the name or Slack ID of the channel you want to select: "
WORKSPACE.select_channel(gets.chomp.upcase)
puts "No match found." if WORKSPACE.selected == ""
end

def show_details
puts "Please make a selection first" if !WORKSPACE.show_details
end

def customize_bot
print "Enter a new name for the bot: "
new_bot_name = gets.chomp
print "Enter a new status emoji for the bot (Syntax > :emoji_name:): "
new_status_emoji = gets.chomp
WORKSPACE.customize_bot(new_bot_name, new_status_emoji)
end

main if __FILE__ == $PROGRAM_NAME
32 changes: 32 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative 'recipient'
require 'table_print'

class User < Recipient
attr_reader :slack_id, :name, :real_name, :status_text, :status_emoji

def initialize(slack_id, name, real_name, status_text, status_emoji)
super(slack_id, name)
@real_name = real_name
@status_text = status_text
@status_emoji = status_emoji
end

def details
return tp self, :slack_id, :name, :real_name, :status_text, :status_emoji
end

def self.list_all
response = self.get("users.list")
all = response["members"].map do |member|
User.new(
member["id"],
member["name"],
member["real_name"],
member["profile"]["status_text"],
member["profile"]["status_emoji"]
)
end
return all
end

end
76 changes: 76 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require_relative "user"
require_relative "channel"
require 'json'

class Workspace
attr_reader :users, :channels
attr_accessor :selected

def initialize
@users = User.list_all
@channels = Channel.list_all
@selected = ""
end

def select_channel(input)
select("channel", input)
end

def select_user(input)
select("user", input)
end

# helper method for select_channel and select_user
def select(type, input)
instance_variable_get("@#{type}s").each do |item|
if item.slack_id.upcase == input.upcase || item.name.upcase == input.upcase
@selected = item
break
else
@selected = ""
end
end
end

def show_details
return @selected == "" ? nil : @selected.details
end

# https://api.slack.com/methods/users.profile.set
def customize_bot(username, emoji)
response = HTTParty.post(BASE_URL + "users.profile.set", {
headers: {
"Content-Type": "application/json",
},
query: {
token: TOKEN,
profile: ({
"display_name": username,
"status_emoji": emoji
}).to_json
}
})

if response.code == 200 && response["ok"]
# https://stackoverflow.com/questions/27163085/read-and-write-json-data-from-form-to-file
# opens a file using the path provided
# w+: Read-write - overwrites the existing file or creates new file if no file exists
File.open("./lib/json/bot-settings.json", "w+") do |file|
# generates a JSON file from the argument provided (response) and writes it into the file
file.write(JSON.generate(response))
end
else
if response["error"] == "profile_status_set_failed_not_emoji_syntax"
puts "Please try again, and use proper emoji syntax (:emoji_name:)"
elsif response["error"] == "profile_status_set_failed_not_valid_emoji"
puts "Please try again, #{emoji} is not in our library."
else
puts "#{response["error"]}"
end
end
end

# TODO: As a user, I can see a history of messages sent to the currently selected recipient. If I change recipients, the message list should also change.
def message_history
end
end
Loading