-
Notifications
You must be signed in to change notification settings - Fork 27
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
Ports - Angela and Shamira #3
base: master
Are you sure you want to change the base?
Changes from all commits
ebfcdff
c8a83ff
7fbfc4e
4a0c8c4
62d357d
f319692
fdc8b53
b269ec9
c9c7242
f1851b1
821a745
ba5104d
c732249
c555943
6947a93
112a72f
b8bb13d
3365cec
9bc79b5
c2551d7
e3c6412
49c24cd
4a81dcb
955e8b1
fd84b3b
777140e
dbbfdac
43a8e26
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require "httparty" | ||
require "dotenv" | ||
require "pry" | ||
Dotenv.load | ||
|
||
class Channel | ||
# attr_reader :name, :topic, :member_count, :id | ||
BASE_URL = "https://slack.com/api/channels.list" | ||
SLACK_TOKEN = ENV["SLACK_TOKEN"] | ||
|
||
def self.list | ||
query_param = { | ||
token: SLACK_TOKEN, | ||
} | ||
response = HTTParty.get(BASE_URL, query: query_param) | ||
|
||
list = [] | ||
response["channels"].each do |channel| | ||
channel_info = [] | ||
channel_info << channel["name"] | ||
channel_info << channel["topic"]["value"] | ||
channel_info << channel["num_members"] | ||
channel_info << channel["id"] | ||
|
||
list << channel_info | ||
end | ||
|
||
return list | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require "httparty" | ||
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. Looks like you got started on this but didn't get finished. I suggest removing it from git. |
||
require "dotenv" | ||
Dotenv.load | ||
|
||
class Recipient | ||
attr_reader :slack_id, :name | ||
BASE_URL = "https://slack.com/api/chat.postMessage" | ||
SLACK_TOKEN = ENV["SLACK_TOKEN"] | ||
|
||
def initialize(slack_id) | ||
@slack_id = slack_id | ||
@name = name | ||
end | ||
|
||
# class SlackApiError < StandardError | ||
# raise SlackApiError, "This id does not exist" | ||
# end | ||
|
||
def send_message(message, user: true) | ||
SlackApi::self_message(message, slack_id) | ||
# query_param = { | ||
# token: SLACK_TOKEN, | ||
# channel: slack_id, | ||
# text: message, | ||
# as_user: user, | ||
# } | ||
|
||
# response = HTTParty.post( | ||
# "#{BASE_URL}", | ||
# body: { | ||
# token: SLACK_TOKEN, | ||
# text: message, | ||
# channel: slack_id, | ||
# }, | ||
# ) | ||
|
||
# if !response.code == 200 && response.parsed_response["ok"] | ||
# raise SlackApiError, "Oops something went wrong" | ||
# end | ||
|
||
# return response.code == 200 && response.parsed_response["ok"] | ||
|
||
# if response.code != 200 raise SlackApiError, "This user/channel does not exist" | ||
|
||
end | ||
|
||
def self.get(url, params) | ||
# SLACK_TOKEN = ENV["SLACK_TOKEN"] | ||
# param = { | ||
# token: SLACK_TOKEN, | ||
# } | ||
# response = HTTParty.get(url, query: param) | ||
end | ||
|
||
private | ||
|
||
def details | ||
end | ||
|
||
def self.list | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
require "httparty" | ||
require "dotenv" | ||
require_relative "workspace" | ||
require_relative "user" | ||
require_relative "channel" | ||
|
||
Dotenv.load | ||
|
||
module SlackApi | ||
BASE_URL = "https://slack.com/api/chat.postMessage" | ||
SLACK_TOKEN = ENV["SLACK_TOKEN"] | ||
|
||
class SlackApiError < StandardError; end | ||
|
||
def self.send_message(message, user) | ||
user_name = User.list.map do |user_list| | ||
user_list[0] | ||
end | ||
# array of all channel names | ||
channel_name = Channel.list.map do |channel| | ||
channel[0] | ||
end | ||
|
||
#### TODO: CHANGE VARIABLE NAME | ||
practice = [] | ||
User.list.each do |user_name_list| | ||
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. Here you end up making the same API call multiple times, this is inefficient. It might be much more useful to have |
||
if user_name_list[0] == user | ||
practice << user_name_list[2] | ||
end | ||
end | ||
|
||
test_workspace = Workspace.new | ||
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. Why do you create a Workspace in this |
||
if channel_name.include?(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. This |
||
response = HTTParty.post( | ||
"#{BASE_URL}", | ||
headers: { "Content-Type" => "application/x-www-form-urlencoded" }, | ||
body: { | ||
token: SLACK_TOKEN, | ||
text: message, | ||
channel: test_workspace.select_channel(user), #.select_user(chose_user) | ||
as_user: true, | ||
}, | ||
) | ||
elsif user_name.include?(user) | ||
response = HTTParty.post( | ||
"#{BASE_URL}", | ||
headers: { "Content-Type" => "application/x-www-form-urlencoded" }, | ||
body: { | ||
token: SLACK_TOKEN, | ||
text: message, | ||
channel: practice[0], | ||
as_user: true, | ||
}, | ||
) | ||
else | ||
raise SlackApiError, "The input #{user} is not included in our channels." | ||
end | ||
|
||
if response["ok"] | ||
return true | ||
else | ||
raise SlackApiError, "Error when posting #{message} to #{user}, error: #{response["error"]}" | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,104 @@ | ||
#!/usr/bin/env ruby | ||
require "dotenv" | ||
require_relative "user" | ||
require_relative "channel" | ||
require_relative "workspace" | ||
require_relative "recipient" | ||
|
||
def display_options | ||
options = ["1. List Users", | ||
"2. List Channels", | ||
"3. Select User", | ||
"4. Select Channel", | ||
"5. Details", | ||
"6. Send Message", | ||
"7. Quit"] | ||
options.each do |one_option| | ||
puts one_option | ||
end | ||
end | ||
|
||
def main | ||
channel_name = Channel.list.map do |channel| | ||
channel[0] | ||
end | ||
user_name = User.list.map do |user| | ||
user[0] | ||
end | ||
puts "Welcome to the Ada Slack CLI!" | ||
puts "We currently have '#{User.list.length}' members and '#{Channel.list.length} channels.'" | ||
puts "" | ||
puts "Please choose one of the the following options" | ||
display_options | ||
selection = gets.chomp.downcase | ||
until selection == "quit" | ||
case selection | ||
when "list users" | ||
puts "Here is the list of users and their details:" | ||
User.list.each do |user| | ||
puts "Username: #{user[0]}" | ||
puts "Real Name: #{user[1]}" | ||
puts "Slack_ID: #{user[2]}" | ||
puts "" | ||
end | ||
puts "What would you like to do next? " | ||
display_options | ||
selection = gets.chomp.downcase | ||
when "list channels" | ||
puts "Here is the list of channels and their details: " | ||
Channel.list.each do |channel| | ||
puts "Name: #{channel[0]}" | ||
puts "Topic: #{channel[1]}" | ||
puts "Member Count: #{channel[2]}" | ||
puts "Slack_ID: #{channel[3]}" | ||
puts "" | ||
end | ||
puts "What would you like to do next? " | ||
display_options | ||
selection = gets.chomp.downcase | ||
when "select user" | ||
puts "Here are all the members' user names: #{user_name}" | ||
chose = gets.chomp.downcase #chose_user | ||
workspace = Workspace.new | ||
details = workspace.select_user(chose) | ||
puts "You just selected #{details}" | ||
puts "What would you like to do next? " | ||
display_options | ||
selection = gets.chomp.downcase | ||
when "select channel" | ||
puts "Here are all the channels' names: #{channel_name}" | ||
chose = gets.chomp.downcase #chose_channel | ||
workspace = Workspace.new | ||
details = workspace.select_channel(chose) | ||
puts "You just selected #{details}" | ||
puts "What would you like to do next? " | ||
display_options | ||
selection = gets.chomp.downcase | ||
when "details" | ||
if details == nil | ||
puts "You must select user or channel first." | ||
else | ||
puts workspace.show_details(details) | ||
end | ||
puts "What would you like to do next? " | ||
selection = gets.chomp.downcase | ||
when "send message" | ||
if details == nil | ||
puts "You must select user or channel first." | ||
else | ||
puts "Please input your message:" | ||
message = gets.chomp.downcase | ||
|
||
# TODO project | ||
SlackApi.send_message(message, chose) | ||
end | ||
puts "What would you like to do next? " | ||
display_options | ||
selection = gets.chomp.downcase | ||
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. You need to have a default option for this |
||
end | ||
end | ||
|
||
puts "Thank you for using the Ada Slack CLI" | ||
end | ||
|
||
main if __FILE__ == $PROGRAM_NAME | ||
main if __FILE__ == $PROGRAM_NAME | ||
# main |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require "httparty" | ||
require "dotenv" | ||
Dotenv.load | ||
|
||
class User | ||
BASE_URL = "https://slack.com/api/users.list" | ||
SLACK_TOKEN = ENV["SLACK_TOKEN"] | ||
|
||
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. All the comments about the |
||
def self.list | ||
query_param = { | ||
token: SLACK_TOKEN, | ||
} | ||
response = HTTParty.get(BASE_URL, query: query_param) | ||
|
||
list = [] | ||
response["members"].each do |member| | ||
member_info = [] | ||
member_info << member["name"] | ||
member_info << member["real_name"] | ||
member_info << member["id"] | ||
|
||
list << member_info | ||
end | ||
|
||
return list | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require "dotenv" | ||
require "httparty" | ||
require_relative "user" | ||
require_relative "channel" | ||
Dotenv.load | ||
|
||
class Workspace | ||
attr_reader :user, :channel | ||
attr_accessor :selected | ||
|
||
class SlackApiError < StandardError; end | ||
|
||
def select_channel(selected) | ||
selected | ||
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. What is the purpose of this method? It takes an argument and just returns the argument? |
||
end | ||
|
||
def select_user(selected) | ||
selected | ||
end | ||
|
||
def show_details(selected) | ||
user_name = User.list.map do |user| | ||
user[0] | ||
end | ||
channel_name = Channel.list.map do |channel| | ||
channel[0] | ||
end | ||
|
||
if channel_name.include?(selected) | ||
Channel.list.each do |instance| | ||
if selected == instance[0] | ||
return instance | ||
end | ||
end | ||
elsif user_name.include?(selected) | ||
User.list.each do |instance| | ||
if selected == instance[0] | ||
return instance | ||
end | ||
end | ||
end | ||
|
||
if selected == "" | ||
raise SlackApiError, "No user of channel was selected!" | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# puts "Welcome to the Ada Slack CLI!" | ||
# puts "We currently have #{User.list.length} members and #{Channel.list.length} channels." | ||
|
||
puts "" | ||
puts "Please choose one of the the following number options" | ||
puts "1. List Users" | ||
puts "2. List Channels" | ||
puts "3. Select User" | ||
puts "4. Select Channel" | ||
puts "5. Quit" | ||
|
||
selection = gets.chomp | ||
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. If this file isn't in your submission, please don't include it in the PR. |
||
|
||
loop do | ||
case selection | ||
when "1" | ||
puts "Here is the list of users:" | ||
# puts User.list | ||
when "2" | ||
puts "Here is the list of channels: " | ||
# puts Channel.list | ||
when "3" | ||
chose_user = gets.chomp | ||
workspace = Workspace.new | ||
# puts workspace.show_details_user(chose_user) | ||
when "4" | ||
chose_channel = gets.chomp | ||
workspace = Workspace.new | ||
# puts workspace.show_details_channel(chose_channel) | ||
when "5" | ||
break | ||
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.
This class is just one class method. Instead I suggest having a
Channel
class with instance variables and methods. Yourlist
method could then return an array of Channel instances.