-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rachael and Christabel Slack-CLI #3
base: master
Are you sure you want to change the base?
Changes from all commits
3e73a21
3c79295
aeed83c
7989599
1871099
48afa3f
9d4a62c
cb74164
eef19de
37a7b14
76923af
8d853c0
0ba025b
8611f02
1a73d91
73e2c7d
6a1f624
fcc0b2f
89aacbf
4286d73
909558b
413bc43
e0292e5
44b98c9
f197b06
5bcbeb7
7e22e63
66a0947
52c5fa9
8d47a90
9a92c48
cb11811
3ae4924
edbced7
3e58c1b
f28a63b
ccda1c6
f65cf01
dab22bb
b049650
aa96783
dcd3e2f
96a1faf
00d37c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ | |
|
||
# Ignore environemnt variables | ||
.env | ||
.floo | ||
.flooignore | ||
.idea/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require_relative 'recipient' | ||
|
||
CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list" | ||
|
||
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.to_i | ||
end | ||
|
||
def details | ||
return "Channel id: #{@slack_id}\nChannel name: #{@name}\nTopic: #{@topic}\nNumber of members: #{@member_count}" | ||
end | ||
|
||
def self.list_all | ||
query = {token: ENV["SLACK_TOKEN"]} | ||
|
||
request = self.get(CONVERSATIONS_LIST_URL, query: query) | ||
|
||
return request["channels"].map do |channel| | ||
self.new(channel["id"], channel["name"], channel["purpose"]["value"], channel["num_members"].to_i) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'httparty' | ||
|
||
POST_URL = "https://slack.com/api/chat.postMessage" | ||
|
||
class Recipient | ||
attr_reader :slack_id, :name | ||
|
||
def initialize(slack_id, name) | ||
@slack_id = slack_id | ||
@name = name | ||
end | ||
|
||
def send_message(message) | ||
params = {token: ENV['SLACK_TOKEN'], channel: @slack_id, text: message} | ||
return self.class.error_message(HTTParty.post(POST_URL, body: params)) | ||
end | ||
|
||
def self.get(url, params) | ||
return error_message(HTTParty.get(url,params)) | ||
end | ||
|
||
def details | ||
raise NotImplementedError, 'Implement me in a child class!' | ||
end | ||
def self.list_all | ||
raise NotImplementedError, 'Implement me in a child class!' | ||
end | ||
|
||
private | ||
|
||
def self.error_message(response) | ||
if response["ok"] != true | ||
raise ArgumentError, "API request failed with error: #{response["error"]}." | ||
else | ||
return response | ||
end | ||
end | ||
Comment on lines
+31
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of a helper method |
||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,98 @@ | ||
#!/usr/bin/env ruby | ||
require 'dotenv' | ||
require "table_print" | ||
require_relative 'workspace' | ||
|
||
|
||
def main | ||
Dotenv.load | ||
|
||
puts "Welcome to the Ada Slack CLI!" | ||
workspace = Workspace.new | ||
workspace.load_users | ||
workspace.load_channels | ||
puts "Amount of users in workspace: #{workspace.users.length}" | ||
puts "Amount of channels in workspace: #{workspace.channels.length}" | ||
|
||
|
||
puts "1. list users\n2. list channels\n3. select user\n4. select channel\n5. quit" | ||
selection = number_validation(gets.chomp, 5) | ||
|
||
# TODO project | ||
while (1..4).include? selection | ||
case selection | ||
when 1 | ||
tp workspace.users, "slack_id", "name", "real_name", "status_text", "status_emoji" | ||
when 2 | ||
tp workspace.channels, "slack_id", "name", "topic", "member_count" | ||
when 3, 4 | ||
puts "Please input a name or slack id" | ||
if selection == 3 | ||
selected_recipient = workspace.select_user(gets.chomp) | ||
else | ||
selected_recipient = workspace.select_channel(gets.chomp) | ||
end | ||
|
||
if selected_recipient | ||
puts "Would you like details? (yes/no)" | ||
details_selection = input_validation(gets.chomp.downcase) | ||
if details_selection == "yes" | ||
puts workspace.show_details(selected_recipient) | ||
end | ||
puts "Would you like to #{selected_recipient.class == User ? "send a message" : "post"} to #{selected_recipient.name}? (yes/no)" | ||
message_selection = input_validation(gets.chomp.downcase) | ||
if message_selection == "yes" | ||
puts "Please write your message." | ||
message = gets.chomp | ||
selected_recipient.send_message(message) | ||
end | ||
else | ||
puts "No #{(selection == 3) ? "user" : "channel"} found." | ||
end | ||
Comment on lines
+35
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice choice to only prompt the user for details of sending message if a user has been selected. Consider breaking the functionality to show details and send message into helper methods. |
||
|
||
end | ||
|
||
puts "Make another selection:" | ||
puts "1. list users\n2. list channels\n3. select user\n4. select channel\n5. quit" | ||
selection = number_validation(gets.chomp, 5) | ||
end | ||
|
||
puts "Thank you for using the Ada Slack CLI" | ||
end | ||
|
||
def number_validation(input, max_num) | ||
input = translate_input(input) | ||
|
||
until (1..max_num).include? input | ||
puts "Invalid input. Please re-enter a selection." | ||
input = translate_input(gets.chomp) | ||
end | ||
|
||
return input | ||
end | ||
|
||
def input_validation(input) | ||
until ["yes", "no"].include? input | ||
puts "Invalid input. Please re-enter either a yes or no." | ||
input = gets.chomp.downcase | ||
end | ||
return input | ||
end | ||
|
||
def translate_input(string_input) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great use of a helper method. |
||
case string_input.downcase | ||
when "list users", "1" | ||
return 1 | ||
when "list channels", "2" | ||
return 2 | ||
when "select user", "3" | ||
return 3 | ||
when "select channel", "4" | ||
return 4 | ||
when "quit", "5" | ||
return 5 | ||
else | ||
return string_input | ||
end | ||
end | ||
|
||
main if __FILE__ == $PROGRAM_NAME |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require_relative 'recipient' | ||
|
||
USERS_LIST_URL = "https://slack.com/api/users.list" | ||
|
||
class User < Recipient | ||
attr_reader :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 "User id: #{@slack_id}\nUsername: #{@name}\nReal name: #{@real_name}\nStatus text: #{@status_text}\nStatus emoji: #{@status_emoji}" | ||
end | ||
|
||
def self.list_all | ||
query = {token: ENV['SLACK_TOKEN']} | ||
|
||
request = self.get(USERS_LIST_URL, query: query) | ||
|
||
@users = request["members"].map do |user| | ||
self.new(user["id"],user["name"],user["real_name"],user["profile"]["status_text"],user["profile"]["status_emoji"]) | ||
|
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'dotenv' | ||
require 'httparty' | ||
require 'awesome_print' | ||
|
||
Dotenv.load # allows access to ENV['VARIABLE_NAME'] | ||
|
||
CONVERSATIONS_LIST_URL = "https://slack.com/api/conversations.list" | ||
|
||
def verification | ||
query = { | ||
token: ENV["SLACK_TOKEN"] | ||
} | ||
|
||
request = HTTParty.get(CONVERSATIONS_LIST_URL, query: query) | ||
|
||
if request.code != 200 || request["ok"] != true | ||
return "API request failed with error code #{request.code} and #{request["error"]}." | ||
else | ||
channel_names = request["channels"].map { |channel| channel["name"] } | ||
return channel_names | ||
end | ||
end | ||
|
||
ap verification |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require_relative 'user' | ||
require_relative 'channel' | ||
|
||
class Workspace | ||
attr_reader :users, :channels | ||
|
||
def initialize | ||
@users = [] | ||
@channels = [] | ||
end | ||
|
||
def load_channels | ||
@channels = Channel.list_all | ||
end | ||
|
||
def load_users | ||
@users = User.list_all | ||
end | ||
|
||
def select_channel(search_term) | ||
return @channels.find{|channel| channel.name == search_term.downcase || channel.slack_id == search_term.upcase} | ||
end | ||
|
||
def select_user(search_term) | ||
return @users.find{|user| user.name == search_term.downcase || user.slack_id == search_term.upcase} | ||
end | ||
|
||
def show_details(recipient) | ||
if recipient | ||
return recipient.details | ||
else | ||
return "Invalid recipient. Unable to display details" | ||
end | ||
end | ||
|
||
end | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a lot in one line. Consider making the post request, storing the response in a variable like
response
and then calling the helper methoderror_message
to increase readability.