Skip to content
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

.DS_Store

# Ignore environemnt variables
.env
.env
41 changes: 41 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#coded along with Devin for Wave 1

require 'httparty'
require_relative 'recipient'


class Channel < Recipient
attr_reader :topic, :member_count

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

def details
return [{
"name" => @name,
"slack_id" => @slack_id,
"topic" => @topic,
"member_count" => @member_count
}]
end

def self.list_all
data = self.get("https://api.slack.com/api/conversations.list")
channels = []

data["channels"].each do |channel|
channels << Channel.new(
name: channel["name"],
slack_id: channel["id"],
topic: channel["topic"]["value"],
member_count: channel["num_members"]
)
end

return channels
end

end
46 changes: 46 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#coded along with Devin for Wave 1

require 'httparty'

class Recipient
attr_reader :slack_id, :name

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

def send(message)
response = HTTParty.post(
"https://api.slack.com/api/chat.postMessage",
body: {
token: ENV['SLACK_TOKEN'],
text: message,
channel: @slack_id
},
headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
)
end

def details
raise NotImplementedError, "Define this method in a child class"
end

def self.get(url)
response = HTTParty.get(url, query: {token: ENV['SLACK_TOKEN']})

if response.code != 200 || response["ok"] == false
raise SlackAPIError, "We encountered a problem: #{response["error"]}"
end

return response
end

def self.list_all
raise NotImplementedError, "Define this method in a child class"
end
end


class SlackAPIError < Exception
end
66 changes: 62 additions & 4 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
#coded along with Devin for Wave 1
#!/usr/bin/env ruby

require 'httparty'
require 'dotenv'
Dotenv.load
require 'table_print'
require_relative 'workspace'


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

puts "\n"
puts "Welcome to the Ada Slack CLI! This Slack workspace currently has #{workspace.users.length} users and #{workspace.channels.length} channels."

user_input = input_prompt

#really liked this way of formatting after seeing it on the Wave 1 Live Code vs using if-elsif statements
##changed my formatting to match.
until user_input == "Quit"

case user_input
when "List users"
tp workspace.users, "slack_id", "name", "real_name", "status_emoji", "status_text"
puts "\n"

when "List channels"
tp workspace.channels, "name", "slack_id", "topic", "member_count"
puts "\n"

when "Select user"
print "Please enter user name or ID: "
user_input = gets.chomp.upcase
workspace.select_user(user_input)
puts "\n"

when "Select channel"
print "Please enter channel name or ID: "
user_input = gets.chomp.upcase
workspace.select_channel(user_input)
puts "\n"

when "Details"
workspace.show_details

when "Send message"
workspace.send_message

else
puts "Sorry, I didn't understand your request. Please try again."
end

user_input = input_prompt
end

puts "Thank you for using the ADA Slack CLI!"
puts "\n"

end

# TODO project
def input_prompt
puts "What would you like to do? Your options are:"
puts "List users \nList channels \nSelect user \nSelect channel \nDetails \nSend message \nQuit"
puts "\n"

puts "Thank you for using the Ada Slack CLI"
return gets.chomp.capitalize
end

main if __FILE__ == $PROGRAM_NAME
39 changes: 39 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#coded along with Devin for Wave 1
require 'httparty'
require_relative 'recipient'

class User < Recipient
attr_reader :real_name, :status_text

def initialize(real_name:, status_text:, name:, slack_id:)
super(slack_id: slack_id, name: name)

@real_name = real_name
@status_text = status_text
end

def details
return [{
"name" => @name,
"slack_id" => @slack_id,
"real_name" => @real_name,
"status_text" => @status_text
}]
end

def self.list_all
data = self.get("https://api.slack.com/api/users.list")
users = []

data["members"].each do |user|
users << User.new(
name: user["name"],
slack_id: user["id"],
real_name: user["profile"]["real_name"],
status_text: user["profile"]["status_text"]
)
end

return users
end
end
58 changes: 58 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#coded along with Devin for Wave 1

require_relative 'user'
require_relative 'channel'
require 'dotenv'
Dotenv.load

class Workspace
attr_reader :users, :channels, :selected

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

def select_user(user_input)
@users.each do |user|
if (user.name.upcase || user.slack_id.upcase) == user_input
@selected = user
return @selected
end
end
end

def select_channel(user_input)
@channels.each do |channel|
if (channel.name.upcase || channel.slack_id.upcase) == user_input
@selected = channel
return @selected
end
end
end

def show_details
if @selected == nil
puts "Unable to show details, as no recipient is selected."
puts "\n\n"
else
puts "The current recipient is: #{@selected.name}, #{@selected.slack_id}."
puts "\n\n"
end
end

def send_message
if @selected == nil
puts "Please select a recipient before attempting to send a messsage."
puts "\n\n"
else
puts "Please enter your message: "
message = gets.chomp
@selected.send(message)
end
end

end


Loading