-
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
Earth/Taylor/Lina #4
base: master
Are you sure you want to change the base?
Changes from all commits
bf20c30
db5c4c4
f283b95
311416b
e76755e
e37e9c5
6bba394
4dadccc
4ca0d9a
a2b1188
37c45a0
2de0975
89a28a4
5088311
37fb327
1b2f6f0
df97b73
8bd08bd
43cc026
fbcfd96
0b5e6f5
ca6cecd
6cbcc79
15bc92f
0351ca2
e32cb06
39bd27a
b566978
7f03131
147f03b
0d50faa
980bcd9
3b5efdf
804f7bf
95c727f
ab7f1df
b8bb0d0
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,5 @@ | |
|
||
# Ignore environemnt variables | ||
.env | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require_relative 'recipient' | ||
|
||
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 | ||
end | ||
|
||
def details | ||
"Slack ID: #{@slack_id}\nName: #{@name}\nTopic: #{@topic}\nMember Count: #{@member_count}" | ||
end | ||
|
||
def self.list_all | ||
url = "https://slack.com/api/conversations.list" | ||
query_params = {token: ENV["SLACK_TOKEN"]} | ||
|
||
response = self.get(url, query_params) | ||
|
||
channels_list = response["channels"].map do |channel| | ||
self.from_api(channel) | ||
end | ||
|
||
return channels_list | ||
end | ||
|
||
private | ||
|
||
def self.from_api(recipient) | ||
topic = { | ||
"value"=>recipient["topic"]["value"], | ||
"creater"=>recipient["topic"]["creator"], | ||
"last_set"=>recipient["topic"]["last_set"] | ||
} | ||
|
||
return new( | ||
slack_id: recipient["id"], | ||
name: recipient["name"], | ||
topic: topic, | ||
member_count: recipient["num_members"] | ||
) | ||
end | ||
|
||
end | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'dotenv' | ||
require 'httparty' | ||
|
||
Dotenv.load | ||
|
||
class SlackTokenError < StandardError; end | ||
|
||
class Recipient | ||
|
||
attr_reader :slack_id, :name | ||
|
||
def initialize(slack_id, name) | ||
@slack_id = slack_id | ||
@name = name | ||
end | ||
|
||
def self.get(url, params) | ||
response = HTTParty.get(url, query: params) | ||
|
||
if response["ok"] != true | ||
raise SlackTokenError, "API call failed error message: #{response["error"]}" | ||
end | ||
|
||
return response | ||
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.from_api(recipient) | ||
raise NotImplementedError, 'Implement me in a child class!' | ||
end | ||
|
||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,76 @@ | ||
#!/usr/bin/env ruby | ||
require_relative 'workspace' | ||
|
||
require 'table_print' | ||
|
||
def main | ||
puts "Welcome to the Ada Slack CLI!" | ||
workspace = Workspace.new | ||
|
||
# TODO project | ||
choices = ["list users", "list channels", "select user", "select channel", "details", "send message", "quit"] | ||
|
||
program_running = true | ||
|
||
while program_running | ||
puts "Please choose from the following:" | ||
|
||
choices.each_with_index do |choice, i| | ||
puts "#{i + 1}: #{choice}" | ||
end | ||
|
||
user_input = gets.chomp.downcase | ||
|
||
case user_input | ||
when "list users" | ||
puts "" | ||
tp workspace.users, :slack_id, :name, :real_name, :status_text, :status_emoji | ||
puts "" | ||
when "list channels" | ||
puts "" | ||
tp workspace.channels, :slack_id, :name, :topic, :member_count | ||
puts "" | ||
when "select user" | ||
puts "User name or ID?" | ||
user = gets.chomp | ||
selected_item = workspace.select_user(user) | ||
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. Because slack.rb is already doing a lot, it would make the program easier for people to read and understand if the Workspace class had an instance variable had an instance variable to track which Recipient is currently selected instead of tracking it here. |
||
if selected_item == nil | ||
puts "Hmm, no user matches that name or ID" | ||
end | ||
when "select channel" | ||
puts "Name or ID?" | ||
channel = gets.chomp | ||
selected_item = workspace.select_channel(channel) | ||
if selected_item == nil | ||
puts "Hmm, no channel matches that name or ID" | ||
end | ||
when "details" | ||
if selected_item != nil | ||
puts "" | ||
puts selected_item.details | ||
puts "" | ||
else | ||
puts "A channel or user has not been selected to show details" | ||
end | ||
when "send message" | ||
|
||
if selected_item != nil | ||
puts "What's your message?" | ||
text = gets.chomp | ||
|
||
workspace.send_message(selected_item.name, text) | ||
else | ||
puts "A channel or user has not been selected to send message" | ||
end | ||
|
||
when "quit" | ||
program_running = false | ||
else | ||
puts "Invalid choice! Please try again!" | ||
end | ||
end | ||
|
||
puts "Thank you for using the Ada Slack CLI" | ||
|
||
end | ||
|
||
main if __FILE__ == $PROGRAM_NAME | ||
main if __FILE__ == $PROGRAM_NAME |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require_relative 'recipient' | ||
|
||
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 | ||
"Slack ID: #{@slack_id}\nUsername: #{@name}\nReal Name: #{@real_name}\nStatus Text: #{@status_text}\nStatus Emoji: #{@status_emoji}" | ||
end | ||
|
||
def self.list_all | ||
url = "https://slack.com/api/users.list" | ||
query_params = {token: ENV["SLACK_TOKEN"]} | ||
|
||
response = self.get(url, query_params) | ||
|
||
users_list = response["members"].map do |member| | ||
self.from_api(member) | ||
end | ||
|
||
return users_list | ||
end | ||
|
||
private | ||
|
||
def self.from_api(recipient) | ||
return new( | ||
slack_id: recipient["id"], | ||
name: recipient["name"], | ||
real_name: recipient["real_name"], | ||
status_text: recipient["profile"]["status_text"], | ||
status_emoji: recipient["profile"]["status_emoji"] | ||
) | ||
end | ||
|
||
end | ||
|
||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require 'dotenv' | ||
require 'httparty' | ||
|
||
require_relative 'user' | ||
require_relative 'channel' | ||
|
||
Dotenv.load | ||
|
||
class SlackApiError < StandardError; end | ||
|
||
class Workspace | ||
|
||
attr_reader :users, :channels | ||
|
||
def initialize | ||
@users = User.list_all | ||
@channels = Channel.list_all | ||
end | ||
|
||
def select_user(input) | ||
selected = @users.find { |user| user.slack_id.upcase == input.upcase || user.name.upcase == input.upcase} | ||
|
||
return selected | ||
|
||
end | ||
|
||
def select_channel(input) | ||
selected = @channels.find { |channel| channel.slack_id.upcase == input.upcase || channel.name.upcase == input.upcase} | ||
|
||
return selected | ||
end | ||
|
||
def send_message(selected_item, text) | ||
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. This only seems to work for sending messages to channels but not users. Did y'all realize that as you were building it and testing it out? |
||
url = "https://slack.com/api/chat.postMessage" | ||
response = HTTParty.post(url, | ||
body: { | ||
token: ENV["SLACK_TOKEN"], | ||
text: text, | ||
channel: selected_item | ||
}, | ||
headers: { 'Content-Type' => 'application/x-www-form-urlencoded' } | ||
) | ||
unless response.parsed_response["ok"] == true | ||
raise SlackApiError, "Error: #{response.parsed_response["error"]}" | ||
end | ||
|
||
return true | ||
end | ||
|
||
end | ||
|
||
|
||
|
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.
Clever and clean that this is a separate method and you made it private!