Skip to content
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 - Ringo & Jasmine #17

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2d084a1
wrote a script to verify that the token works
jasyl Oct 6, 2020
08445a7
added the filter_sensitive_data for SLACK_TOKEN
jasyl Oct 6, 2020
2cf3d20
create channel class and define initialize and self.life methods
ringolingo Oct 6, 2020
f9b04a3
create recipient class and define initialize and self.get methods
ringolingo Oct 6, 2020
95694ac
define main method for list users & channels
ringolingo Oct 6, 2020
ed7061a
create user class and define initalize and self.list methods
ringolingo Oct 6, 2020
02a716c
create workspace class and define initialize and list methods
ringolingo Oct 6, 2020
19549eb
delete duplicate text in test_helper and make test files for classes
ringolingo Oct 6, 2020
e159690
add floo files to .gitignore
ringolingo Oct 6, 2020
7acca4b
create slack_api_error and add exception handling to self.get
ringolingo Oct 6, 2020
16d636d
write tests for recipient
ringolingo Oct 7, 2020
01c1a70
define channel tests and add exception handling
ringolingo Oct 7, 2020
2a3d65c
write user tests and add exception handling
ringolingo Oct 7, 2020
951e680
write tests for workspace
ringolingo Oct 7, 2020
cc601cf
write tests for select method, define select method, implement select…
ringolingo Oct 7, 2020
a5aebc0
wrote tests for and defined details method
ringolingo Oct 7, 2020
be4abee
implement details method
ringolingo Oct 7, 2020
af20912
wrote tests for workspace.details
ringolingo Oct 7, 2020
a1d4f4a
define post method for recipient and write tests
ringolingo Oct 8, 2020
b180d69
define is_selected? and send_message methods and write tests
ringolingo Oct 8, 2020
4f1f9ab
update slack.rb for send message and clean up comments
ringolingo Oct 8, 2020
b522158
define customize_bot method and write tests
ringolingo Oct 8, 2020
edf8037
modify post method for customize_bot and write tests
ringolingo Oct 8, 2020
51fb266
integrate customize_bot to driver code
ringolingo Oct 8, 2020
cde04a0
refactor list method and write tests
ringolingo Oct 9, 2020
ba4e7ff
minor clean up
ringolingo Oct 9, 2020
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
.floo
.flooignore
Empty file added bot-settings.json
Empty file.
41 changes: 41 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'httparty'
require 'dotenv'
Dotenv.load

require_relative 'recipient'

class Channel < Recipient

attr_reader :name, :topic, :member_count

BASE_URL = "https://slack.com/api/conversations.list"

def initialize(slack_id, name, topic, member_count)

raise SlackApiError if [slack_id, name, topic, member_count].include? nil

super(slack_id)
@name = name
@topic = topic
@member_count = member_count
end

def self.list
response = self.get(BASE_URL)
all_our_pretty_channels = []
response["channels"].each_with_index do |channel|
slack_id = channel["id"]
name = channel["name"]
topic = channel["purpose"]["value"]
member_count = channel["num_members"]
all_our_pretty_channels << Channel.new(slack_id, name, topic, member_count)
end
return all_our_pretty_channels
end

def details
output = "Name: #{name}\nSlack ID: #{slack_id}\nMember Count: #{member_count}\nTopic: #{topic}"
return output
end

end
50 changes: 50 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'httparty'
require 'dotenv'
Dotenv.load

require_relative 'slack_api_error'

class Recipient

attr_reader :slack_id

KEY = ENV["SLACK_TOKEN"]

def initialize(slack_id)
@slack_id = slack_id
end

def self.get(url, parameters: { token: KEY } )
response = HTTParty.get(url, query: parameters )
raise SlackApiError, "Error when getting info from #{url}" unless response['ok']
return response
end

def post(message)
post_url = "https://slack.com/api/chat.postMessage"
body = {
token: KEY,
text: message,
channel: slack_id
}

begin
bot_attributes = JSON.parse(File.read("bot-settings.json"))
body.merge!(bot_attributes)
rescue JSON::ParserError
end

response = HTTParty.post(post_url, body: body)

raise SlackApiError, "Error when posting #{message}" unless response['ok']
return "Your message was delivered!"
end

def self.list
raise NotImplementedError, "Call me in a child class"
end

def details
raise NotImplementedError, "Call me in a child class"
end
end
77 changes: 74 additions & 3 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,83 @@
#!/usr/bin/env ruby
# require "dotenv"
require "httparty"
require "pry"
require "colorize"
require_relative 'workspace.rb'
# Dotenv.load

def main
puts "Welcome to the Ada Slack CLI!"
puts "Welcome to the Ada Slack CLI!".colorize(:light_yellow)
workspace = Workspace.new
tp.set :max_width, 200

# TODO project
puts "There are #{workspace.users.length} users and #{workspace.channels.length} channels".colorize(:light_yellow)

options = "\nWhat would you like to do?\n— list users\n— list channels\n— select user\n— select channel\n— details\n- send message\n- customize bot\n— quit\n".colorize(:light_green)

while true
puts options
print "=> "
user_input = gets.chomp.downcase
puts

case user_input

when "list users"
tp *workspace.list("users")

when "list channels"
tp *workspace.list("channels")

when "select user"
puts "please enter user slack ID or username"
input = gets.chomp
looked_for_user = workspace.select("user", input: input)
if looked_for_user
puts "you have selected #{looked_for_user.username}"
else
puts "we could not find that user"
end

when "select channel"
puts "please enter channel slack ID or name"
input = gets.chomp
looked_for_channel = workspace.select("channel", input: input)
if looked_for_channel
puts "you have selected #{looked_for_channel.name}"
else
puts "we could not find that channel"
end

when "details"
puts workspace.details

when "send message"
if workspace.is_selected?
puts "what message would you like to send?"
text = gets.chomp
puts workspace.send_message(text)
else
puts "Please select user or channel, first."
end

when "customize bot"

Choose a reason for hiding this comment

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

Fun addition!

puts "what would you like to name the bot?"
username = gets.chomp
puts "what emoji would you like to give the bot?"
emoji = gets.chomp
workspace.customize_bot(username, emoji)
puts "enjoy your power to change the names and faces of others"

when "quit"
break

else
puts "I told you your options, buddy... try again.".colorize(:light_magenta)
end
end

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

main if __FILE__ == $PROGRAM_NAME
main if __FILE__ == $PROGRAM_NAME
2 changes: 2 additions & 0 deletions lib/slack_api_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class SlackApiError < Exception
end
42 changes: 42 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'httparty'
require 'dotenv'
Dotenv.load

require_relative 'recipient'

class User < Recipient

attr_reader :username, :real_name, :status_text, :status_emoji

BASE_URL = "https://slack.com/api/users.list"

def initialize(slack_id, username, real_name, status_text, status_emoji)

raise SlackApiError if [slack_id, username, real_name, status_text, status_emoji].include? nil

Choose a reason for hiding this comment

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

When I try to actually run the program by calling the main method in slack.rb, the program fails because not all users have all of these attributes. The only one that all users have is slack_id.

In the midst of TDD, it's important to still manually run your code to make sure it all works together for the user, rather than relying only on the tests.

Choose a reason for hiding this comment

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

Suggested change
raise SlackApiError if [slack_id, username, real_name, status_text, status_emoji].include? nil
raise SlackApiError if slack_id.nil?


super(slack_id)
@username = username
@real_name = real_name
@status_text = status_text
@status_emoji = status_emoji
end

def self.list
response = self.get(BASE_URL)
all_our_pretty_users = []
response["members"].each_with_index do |user|
slack_id = user["id"]
username = user["name"]
real_name = user["real_name"]
status_text = user["profile"]["status_text"]
status_emoji = user['profile']['status_emoji']
all_our_pretty_users << User.new(slack_id, username, real_name, status_text, status_emoji)
end
return all_our_pretty_users
end

def details
output = "Username: #{username}\nReal Name: #{real_name}\nSlack ID: #{slack_id}\nStatus: #{status_text}\nStatus Emoji: #{status_emoji}"
return output
end
end
59 changes: 59 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'table_print'
require 'json'
require_relative 'user'
require_relative 'channel'


class Workspace

attr_reader :users, :channels, :selected

def initialize
@users = User.list
@channels = Channel.list
@selected = nil
end

def list(thing_to_print)
if thing_to_print == "users"
return [@users, :slack_id, :real_name, :username]
elsif thing_to_print == "channels"
return [@channels, :name, :member_count, :slack_id, :topic]
end
end

def select(recipient_type, input: "")
if recipient_type == "user"
@selected = @users.find {|user| user.username.downcase == input.downcase || user.slack_id.downcase == input.downcase }
elsif recipient_type == "channel"
@selected = @channels.find {|channel| channel.name.downcase == input.downcase || channel.slack_id.downcase == input.downcase }
end
return @selected
end

def is_selected?
return !!@selected
end

def details
return "Please select user or channel, first." unless is_selected?
return @selected.details
end

def send_message(text)
return "Please select user or channel, first." unless is_selected?
return @selected.post(text)
end

def customize_bot(username, emoji)
# we format in hash
body = {
icon_emoji: emoji,
username: username
}
# save hash to json file
File.open("bot-settings.json","w") do |f|
f.write(body.to_json)
end
end
end
72 changes: 72 additions & 0 deletions test/cassettes/channel_get.yml

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

Loading