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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@

# Ignore environemnt variables
.env
lib/.env
coverage
Empty file added .idea/.gitignore
Empty file.
7 changes: 7 additions & 0 deletions .idea/.rakeTasks

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

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

4 changes: 4 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

32 changes: 32 additions & 0 deletions .idea/slack-cli.iml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

232 changes: 232 additions & 0 deletions .idea/workspace.xml

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require_relative 'recipient'

class Channel < Recipient

Choose a reason for hiding this comment

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

👍

attr_reader :name, :topic, :member_count, :slack_id

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

raise ArgumentError, "slack ID, name, topic, and member count are all required." unless topic || member_count
end

def self.list_all
response = self.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.

Nice use of a helper method in the parent class.

channels = []
response["channels"].each do |channel|
channels << Channel.new(channel["id"], channel["name"], channel["topic"]["value"], channel["num_members"])
end
return channels
end

end
47 changes: 47 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'httparty'
#require_relative 'workspace'
require 'dotenv'
require 'table_print'

class Recipient
Dotenv.load ##inside or outside?
BASE_URL = "https://slack.com/api/"
SLACK_TOKEN = ENV['SLACK_TOKEN']

attr_reader :slack_id, :name

def initialize(slack_id, name)#should we get rid of name????
@slack_id = slack_id
@name = name

raise ArgumentError, "slack ID and name are required." unless slack_id || name
end

def self.get(url)
return HTTParty.get(url, query: {token: SLACK_TOKEN})
end
Comment on lines +20 to +22

Choose a reason for hiding this comment

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

👍


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

def self.send_message(slack_id, message)
raise Exception.new("message is required") if message == ""
url = "https://slack.com/api/chat.postMessage"
response = HTTParty.post(
url,
body: {
token: SLACK_TOKEN,
text: message,
channel: slack_id
},
headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
)
if response.code != 200 || response.parsed_response["ok"] == false
raise Exception.new("Http request code #{response.code} error #{response.parsed_response["error"]}")
end
return response
end


end
95 changes: 93 additions & 2 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,103 @@
#!/usr/bin/env ruby
require 'httparty'
require_relative 'workspace'
require 'dotenv'
require 'table_print'


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

loop do
puts "(~ ̄▽ ̄)~ What would you like to do with CLI? ~( ̄▽ ̄~) \n1. list users \n2. list channels \n3. select user \n4. select channel \n5. detail\n (about the selected user or channel) \n6. send message \n7. quit"
input = gets.chomp

# TODO project
case input
when "list users", "1"
tp @workspace.users
puts "\n\n"
when "list channels", "2"
tp @workspace.channels
puts "\n\n"
when "select user", "3"
select_a_user
when "select channel", "4"
select_a_channel
when "detail", "5"
if @workspace.selected == nil
puts "ヽ(ˋДˊ)ノ"
puts "Please select a user or channel first!"
puts "\n\n"
else
tp @workspace.selected
puts "\n\n"
end
when "send message", "6"
write_message
puts "\n\n"
when "quit", "7"
puts "乀(ˉεˉ乀) bye~"
break
else
puts "ヽ(ˋДˊ)ノ"
puts "Please enter one of the menu items or their corresponding number values."
puts "\n\n"
end
end

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

def write_message
if @workspace.selected == nil
puts "ヽ(ˋДˊ)ノ \nPlease select a user or channel first!"
else
puts "What is your message?"
message = gets.chomp
if message == ""
puts "ヽ(ˋДˊ)ノ \nNo message was inputted."
return false
end
puts "ヽ(ˋ▽ˊ)ノ \nOk, we sent off your message!" if @workspace.send_message(message).parsed_response["ok"] == true
end
end

def select_a_user
loop do
puts "What's the user's slack id or username? enter 7 or quit to exit."
id_or_name = gets.chomp
if id_or_name == "7" || id_or_name == "quit"
break
end
begin
@workspace.select_user(id_or_name)
rescue Exception
puts "ヽ(ˋДˊ)ノ \nPlease enter a valid username or id.\n\n"
else
puts "ヽ(ˋ▽ˊ)ノ \nOk, #{id_or_name} is selected.\n\n"
break
end
end
end

def select_a_channel
loop do
puts "What's the channel's slack id or username? enter 7 or quit to exit."
id_or_name = gets.chomp
if id_or_name == "7" || id_or_name == "quit"
break
end
begin
@workspace.select_channel(id_or_name)
rescue Exception
puts "ヽ(ˋДˊ)ノ \nPlease enter a valid channel name or id.\n\n"
else
puts "ヽ(ˋ▽ˊ)ノ \nOk, #{id_or_name} is selected.\n\n"
break
end
end
end


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

class User < Recipient

Choose a reason for hiding this comment

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

👍

attr_reader :name, :real_name, :slack_id

def initialize(slack_id, name, real_name)
super(slack_id, name) #super(name)?????
@real_name = real_name

raise ArgumentError, "real name is required." unless real_name
end

def self.list_all
response = self.get("https://slack.com/api/users.list")
users = []
response["members"].each do |member|
users << User.new(member["id"], member["name"], member["real_name"] || member["profile"]["real_name"])
end
return users
end
end


38 changes: 38 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative 'recipient'
require_relative 'user'
require_relative 'channel'
require 'table_print'

class Workspace
attr_reader :users, :channels
attr_accessor :selected

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

end

def select_user(name_or_id)
@selected = @users.find {|user| user.slack_id == name_or_id}
@selected = @users.find {|user| user.name == name_or_id} unless @selected
raise Exception.new("no user found.") unless @selected
return @selected
end

def select_channel(name_or_id)
@selected = @channels.find {|channel| channel.slack_id == name_or_id}
@selected = @channels.find {|channel| channel.name == name_or_id} unless @selected
raise Exception.new("no channel found.") unless @selected
return @selected
end

def send_message(message)
raise Exception.new("nothing selected, please select a user/channel to send to first!") if @selected == nil
channel = @selected.slack_id
Recipient.send_message(channel, message)
# return true
end
end

74 changes: 74 additions & 0 deletions test/cassettes/list_all_channels.yml

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

Loading