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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

.DS_Store

# Ignore environemnt variables
# Ignore environment variables
.env
35 changes: 35 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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

# displays channels details to command line
def details
tp self, "slack_id", "name", "topic", "member_count"
end

# gathers relevant information about channels in this workspace
def self.list_all
data = self.get("https://slack.com/api/conversations.list")

channel_info = []

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

return channel_info
end

end
56 changes: 56 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require "httparty"

class Recipient

attr_reader :slack_id, :name

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

# ----- IMPLEMENTED METHODS -----

# gets desired data from Slack API
def self.get(url)
requested_data = HTTParty.get(url, query: {token: ENV["BOT_TOKEN"]})

if requested_data.code != 200 || requested_data["ok"] == false
raise SlackAPIError, "Encountered error: #{requested_data["error"]}"
end

return requested_data
end

# post a message to a slack channel
def send_message(message)
message_reciever = self.slack_id

sent_message_details = HTTParty.post("https://slack.com/api/chat.postMessage",
query: {token: ENV["BOT_TOKEN"], channel: message_reciever, text: message} )

if sent_message_details["ok"] == false
raise SlackAPIError, "Encountered error: #{sent_message_details["error"]}"
else
puts "\n"
puts "Message sent successfully."
end

return sent_message_details
end

# ----- ABSTRACT METHODS -----

def self.list_all
raise NotImplementedError, "Implement me in a child class!"
end

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

end

# custom exception created here so that it is accessible by all other classes in this project
class SlackAPIError < Exception
end
81 changes: 78 additions & 3 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,87 @@
#!/usr/bin/env ruby
require 'dotenv'
require "table_print"
require_relative "workspace"

Dotenv.load

# prompts user for their input and records it
def prompt_action
puts "You have six options: list users, list channels, select user, select channel, display details, send message, or quit."
puts "\n"
print "Please enter your choice: "
return gets.chomp.downcase
end

# controls command line interactivity
def main
puts "Welcome to the Ada Slack CLI!"

workspace = Workspace.new

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

user_input = prompt_action

until user_input == "quit"

case user_input

when "list users"
tp workspace.users, "slack_id", "name", "real_name"
puts "\n"

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

when "select user"
print "Please enter the user's username or ID: "
requested_user = gets.chomp
puts workspace.select_user(requested_user)
puts "\n"

when "select channel"
print "Please enter the channel's name or ID: "
requested_channel = gets.chomp
puts workspace.select_channel(requested_channel)
puts "\n"

when "display details"
if workspace.selected == nil
puts "You must \"select user\" or \"select channel\" first."
puts "\n"
else
workspace.display_details
user_input = nil
puts "\n"
end

when "send message"
if workspace.selected == nil
puts "You must \"select user\" or \"select channel\" first."
puts "\n"
else
print "Enter your message: "
message = gets.chomp
workspace.send_message(message)
user_input = nil
puts "\n"
end

else
puts "\n"
puts "I cannot perform \"#{user_input}\". Please try again ->"
puts "\n"
end

user_input = prompt_action # call again until valid input is provided
end

puts "\n"
puts "Okay, thank you for using the Ada Slack CLI. Goodbye!"

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

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

class User < Recipient

attr :real_name, :status_text, :status_emoji

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

# displays user details to command line
def details
tp self, "slack_id", "name", "real_name"
end

# gathers relevant information about users in this workspace
def self.list_all
data = self.get("https://slack.com/api/users.list")

user_info = []

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

return user_info
end

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

class Workspace

attr_reader :users, :channels, :selected

def initialize(users: [], channels: [])
@users = User.list_all
@channels = Channel.list_all
@selected = nil
end

def select_user(requested_user)
users.each do |user|
if user.name == requested_user || user.slack_id == requested_user
@selected = user
return "User \"#{selected.real_name}\" has been selected."
end
end

return "Sorry, user \"#{requested_user}\" does not exist in this workspace."
end

def select_channel(requested_channel)
channels.each do |channel|
if channel.name == requested_channel || channel.slack_id == requested_channel
@selected = channel
return "Channel titled \"#{selected.name}\" has been selected."
end
end

return "Sorry, channel \"#{requested_channel}\" does not exist in this workspace."
end

# displays workspace info to command line based on selected recipient
def display_details
return @selected.details
end

# posts a message in the selected channel in workspace
def send_message(message_text)
@selected.send_message(message_text)
end

end
Loading