Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
10fe2fd
added slack_token_test
dtingg Sep 9, 2019
914696b
Sabrina - added my slack token test to .gitignore
Galaxylaughing Sep 9, 2019
470c464
Added User.all method
dtingg Sep 10, 2019
aa78142
fixed all_users error
dtingg Sep 10, 2019
7819a1e
removed cassette
dtingg Sep 10, 2019
dabd90c
added channel.all method
dtingg Sep 10, 2019
7803a92
workspace.rb created. tests pass.
TiffanyChio Sep 10, 2019
ce2f6ce
implemented the command line loop
dtingg Sep 10, 2019
632bebf
added find_user method and test
dtingg Sep 10, 2019
4f373cc
added finding channel
TiffanyChio Sep 11, 2019
6832248
updated user and channel tests to check selected
dtingg Sep 11, 2019
2901ef4
refactored self.get to be a Recipient method, implemented 'details' c…
dtingg Sep 11, 2019
9f320f8
added send_message command option; moved workplace-related slack.rb h…
dtingg Sep 11, 2019
0d469a3
added change bot settings
dtingg Sep 11, 2019
d5b7b8b
can change bot settings and save them to json
dtingg Sep 11, 2019
6f47678
added tests for save json
dtingg Sep 12, 2019
3bdeaf1
added get channel message history
dtingg Sep 12, 2019
1cdcddf
added message history for user
dtingg Sep 12, 2019
18d1f11
added tests for get_message_history
dtingg Sep 12, 2019
d059324
Added SlackAPIError and error handling
dtingg Sep 13, 2019
4443b17
final version
dtingg Sep 13, 2019
deb9446
fixed bug for message_history when selected is nil
dtingg Sep 13, 2019
382a8f9
added line break and fixed bug
dtingg Sep 13, 2019
d3074e6
added select user and select channel methods
dtingg Sep 13, 2019
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ build-iPhoneSimulator/
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

# Ignore environemnt variables
# Ignore environment variables
.env

# Ignore cassette files
/specs/cassettes/

# Ignore slack token verification
slack_token_verification_test.rb

Choose a reason for hiding this comment

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

Good on you to have this test!

1 change: 1 addition & 0 deletions bot-settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"bot_name":"Slackbot","bot_avatar":"jack_o_lantern"}
39 changes: 39 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module SlackCLI
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 get_message_history
url = "https://slack.com/api/conversations.history"
query = { token: ENV["SLACK_API_TOKEN"] , channel: slack_id, limit: 25}
response = HTTParty.get(url, query: query)

unless response.code == 200 && response.parsed_response["ok"]
raise SlackAPIError, "Error when getting message history, error: #{response.parsed_response["error"]}"
end

return response
end

def self.all
channels = []

response = Channel.get("channels.list")

response["channels"].each do |channel|
slack_id = channel["id"]
name = channel["name"]
topic = channel["topic"]["value"]
member_count = channel["num_members"]

channels << SlackCLI::Channel.new(slack_id: slack_id, name: name, topic: topic, member_count: member_count)
end
return channels
end
end
end
4 changes: 4 additions & 0 deletions lib/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module SlackCLI
class SlackAPIError < StandardError
end
end
28 changes: 28 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module SlackCLI

class Recipient
attr_reader :slack_id, :name

def initialize(slack_id, name)
@slack_id = slack_id
@name = name
end

def self.get(url)
base_url = "https://slack.com/api/"
url = base_url + url
query = { token: ENV["SLACK_API_TOKEN"] }
response = HTTParty.get(url, query: query)

unless response.code == 200 && response.parsed_response["ok"]

Choose a reason for hiding this comment

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

Nice work adding this exception raising here.

raise SlackAPIError, "Error: #{response.parsed_response["error"]}"
end

return response
end

def self.all
raise NotImplementedError
end
end
end
170 changes: 165 additions & 5 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,171 @@
#!/usr/bin/env ruby
require "dotenv"
require "httparty"
require 'table_print'
require_relative "recipient"
require_relative "user"
require_relative "workspace"
require_relative "channel"
require_relative "errors"

def main
puts "Welcome to the Ada Slack CLI!"
Dotenv.load

PUMPKIN_SPICE = SlackCLI::Workspace.new()

MAIN_MENU = ["List Users", "List Channels", "Select User", "Select Channel", "Details", "Send Message", "Get Message History", "Change Bot Settings", "Quit"]

Choose a reason for hiding this comment

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

Good use of a global constant.


def print_workplace_stats()
puts PUMPKIN_SPICE.get_workplace_stats()
end

# TODO project
def print_menu()
puts "\nMAIN MENU"
MAIN_MENU.each_with_index do |menu_item, index|
puts "#{index + 1}. #{menu_item}"
end
puts "\n"
end

def print_users
puts "\n"
tp PUMPKIN_SPICE.users, "name", "real_name", "slack_id"
end

def print_channels
puts "\n"
tp PUMPKIN_SPICE.channels, "name", {"topic" => {:width => 60}}, "member_count", "slack_id"
end

puts "Thank you for using the Ada Slack CLI"
def select_user
print "Please enter the name or Slack ID of the user you want to select: "
search_term = gets.chomp

result = PUMPKIN_SPICE.select_user(search_term)

puts
if result
puts "The user #{result.name} was found and selected."
else
puts "No user was found."
end
end

main if __FILE__ == $PROGRAM_NAME
def select_channel
print "Please enter the name or Slack ID of the channel you want to select: "
search_term = gets.chomp

result = PUMPKIN_SPICE.select_channel(search_term)

puts
if result
puts "The channel #{result.name} was found and selected."
else
puts "No channel was found."
end
end

def print_details()
puts
if PUMPKIN_SPICE.selected.class == SlackCLI::User
tp PUMPKIN_SPICE.selected, "name", "real_name", "slack_id"
elsif PUMPKIN_SPICE.selected.class == SlackCLI::Channel
tp PUMPKIN_SPICE.selected, "name", {"topic" => {:width => 60}}, "member_count", "slack_id"
else
puts "There is no recipient selected."
end
end

def get_message()
puts
if PUMPKIN_SPICE.selected
print "Please enter a message: "
message = gets.chomp

PUMPKIN_SPICE.send_message(message)
else
puts "You need to select a recipient."
end
end

def format_message_history(response)
table = []

response["messages"].each do |message|
if message["username"]
table.push({name: message["username"], text: message["text"]})
else
found_user = PUMPKIN_SPICE.find_user(message["user"])

modified_message = message["text"].gsub(/^<@\w+>/, "#{found_user.name}")

table.push({name: found_user.name, text: modified_message})
end
end

table.reverse!

tp table, {:name=>{:width => 45}}, {:text=>{:width => 80}}
end

def main
puts "Welcome to the Ada Slack CLI!"
puts
# lists number of users and channels
print_workplace_stats()

again = true
while again
# lists menu options
print_menu()
print "What would you like to do? "
answer = gets.chomp.downcase

case answer
when "list users", "1", "one"
print_users

when "list channels", "2", "two"
print_channels

when "select user", "3", "three"
select_user

when "select channel", "4", "four"
select_channel

when "details", "5", "five"
print_details

when "send message", "6", "six"
get_message

when "get message history", "7", "seven"
if PUMPKIN_SPICE.selected
response = PUMPKIN_SPICE.selected.get_message_history
puts
format_message_history(response)
else
puts "\nYou need to select a recipient."
end

when "change bot settings", "8", "eight"
print "Please enter the bot's new name: "
name = gets.chomp
PUMPKIN_SPICE.bot_name = name
puts "The bot's new name is #{name}!"

print "Please enter the bot's new avatar: "
avatar = gets.chomp
PUMPKIN_SPICE.bot_avatar = avatar
puts "The bot's new avatar is #{avatar}!"

when "quit", "9", "nine", "exit"
PUMPKIN_SPICE.save_settings
again = false
end
end
puts "\nThank you for using the Ada Slack CLI"
end

main if __FILE__ == $PROGRAM_NAME

53 changes: 53 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module SlackCLI
class User < Recipient
attr_reader :real_name

def initialize(slack_id:, name:, real_name:)
super(slack_id, name)
@real_name = real_name
end

def get_message_history
im_url = "https://slack.com/api/im.list"
im_query = { token: ENV["SLACK_API_TOKEN"]}
im_response = HTTParty.get(im_url, query: im_query)

unless im_response.code == 200 && im_response.parsed_response["ok"]

Choose a reason for hiding this comment

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

Good use of a custom exception. Consider using a begin/rescue in your CLI so that it doesn't break when this happens.

raise SlackAPIError, "Error when getting im list, error: #{im_response.parsed_response["error"]}"
end

ims = im_response["ims"]

direct_channel = ims.find do |im|
im["user"] == slack_id
end

direct_channel_id = direct_channel["id"]

url = "https://slack.com/api/conversations.history"
query = { token: ENV["SLACK_API_TOKEN"] , channel: direct_channel_id}
response = HTTParty.get(url, query: query)

unless response.code == 200 && response.parsed_response["ok"]
raise SlackAPIError, "Error when getting conversations history, error: #{response.parsed_response["error"]}"
end

return response
end

def self.all
users = []

response = User.get("users.list")

response["members"].each do |member|
slack_id = member["id"]
name = member["name"]
real_name = member["real_name"]

users << SlackCLI:: User.new(slack_id: slack_id, name: name, real_name: real_name)
end
return users
end
end
end
Loading