Skip to content
Open
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
Binary file added .DS_Store
Binary file not shown.
34 changes: 34 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative 'recipient'

module SlackBot
class Channel < Recipient
attr_reader :topic, :member_count

def initialize(slack_id:, name:, topic: nil, member_count:)
super(slack_id: slack_id, name: name)

@topic = topic || nil
@member_count = member_count
end

def details
tp self, "slack_id", "name", "topic", "member_count"
end

def self.list
response = Channel.get("https://slack.com/api/conversations.list")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put this URL in a variable, just for clarity's sake. I'm being picky.

channel_list = []

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

return channel_list
end
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 @@
require 'httparty'

module SlackBot
class Recipient
attr_reader :slack_id, :name

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

if @slack_id.class != String || @slack_id.length != 9
raise ArgumentError.new("Failure: slack_id must be a 9 character string")
end
end

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that this is at a low level of the program rather than being passed around


if response["ok"] == false
raise Exception
else
return response
end
end

def send_message(slack_id:, message:)
response = HTTParty.post("https://slack.com/api/chat.postMessage", query: {token: ENV["SLACK_TOKEN"], channel: slack_id, text: message})

if response["ok"] == false
raise Exception, "Failure: message did not send.".colorize(:red)
else
return response
end
end

private

def details
raise NotImplementedError, 'Implement me in a child class!'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

end

def self.list
raise NotImplementedError, 'Implement me in a child class!'
end
end
end
79 changes: 73 additions & 6 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,78 @@
#!/usr/bin/env ruby
require 'dotenv'
require 'httparty'
require_relative 'recipient'
require_relative 'channel'
require_relative 'user'
require_relative 'workspace'
require 'colorize'
require 'table_print'

def main
puts "Welcome to the Ada Slack CLI!"
Dotenv.load

# !/usr/bin/env ruby

# TODO project
# unless ENV['SLACK_TOKEN']
# puts "Could not load API key, please store in the environment variable 'SLACK_TOKEN'"
# exit
# end

puts "Thank you for using the Ada Slack CLI"
def main
workspace = SlackBot::Workspace.new
valid_inputs = ["list users", "list channels", "select user", "select channel", "details", "send_message", "quit"]
user_input = nil

until valid_inputs.include?(user_input)
puts "\nPlease make a selection:"
puts "List Users"
puts "List Channels"
puts "Select User"
puts "Select Channel"
puts "Details"
puts "Send Message"
puts "Quit"
print "Selection: "
user_input = gets.chomp.downcase

case user_input
when "list users"
tp workspace.users, "slack_id", "name", "real_name"
user_input = nil
when "list channels"
tp workspace.channels, "slack_id", "name", "topic", "member_count"
user_input = nil
when "select user"
print "Enter a Username or Slack ID: "
user_criteria = gets.chomp
workspace.select_user(user_criteria)
user_input = nil
when "select channel"
print "Enter a Channel Name or Slack ID: "
channel_criteria = gets.chomp
workspace.select_channel(channel_criteria)
user_input = nil
when "details"
if workspace.selected == nil
puts "No user or channel has been selected.".colorize(:red)
user_input = nil
else
workspace.show_details
user_input = nil
end
when "send message"
if workspace.selected == nil
puts "No user or channel has been selected.".colorize(:red)
user_input = nil
else
puts "What message you would like to send to #{workspace.selected.name}: "
message = gets.chomp
workspace.send_message(message: message)
user_input = nil
end
when "quit"
puts "\nThank you for using the Ada Slack CLI".colorize(:yellow)
exit
end
end
end

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

module SlackBot
class User < Recipient
attr_reader :real_name, :status_text, :status_emoji

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

@real_name = real_name
@status_text = status_text || nil
@status_emoji = status_emoji || nil
end

def details
output =tp self, "slack_id", "name", "real_name"
return output
end

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

response["members"].each do |member|
users << User.new(
slack_id: member["id"],
name: member["name"],
real_name: member["real_name"]
)
end

return users
end
end
end
59 changes: 59 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'table_print'

require_relative 'channel'
require_relative 'user'

module SlackBot
class Workspace
attr_reader :users, :channels, :selected

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

def select_channel(criteria)
@channels.each do |channel|
if channel.name == criteria.downcase
@selected = channel
return puts "Channel #{@selected.name} is selected"
end
end
@channels.each do |channel|
if channel.slack_id == criteria.upcase
@selected = channel
return puts "Channel #{@selected.name} is selected"
end
end
raise ArgumentError, "Invalid Enty: No such channel."
end

def select_user(criteria)
@users.each do |user|
if user.name == criteria.downcase
@selected = user
return puts "User #{@selected.name} is selected"
end
end
@users.each do |user|
if user.slack_id == criteria.upcase
@selected = user
return puts "User #{@selected.name} is selected"
end
end
raise ArgumentError, "Invalid Enty: No such user."
end

def show_details
@selected.details
end

def send_message(message:)
response = @selected.send_message(message: message, slack_id: @selected.slack_id)
if response["ok"] == true
return puts "message sent"
end
end
end
end
88 changes: 88 additions & 0 deletions test/cassettes/channel-list.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading