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

FIRE: Kalki and Sandy SLACK CLI #20

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
.DS_Store

# Ignore environemnt variables
.env
test/.env

.env
6 changes: 6 additions & 0 deletions .idea/.gitignore

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

7 changes: 7 additions & 0 deletions .idea/.rakeTasks

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

4 changes: 4 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

29 changes: 29 additions & 0 deletions .idea/slack-cli.iml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.6.6
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList['test/*_test.rb']
t.test_files = FileList['test/*_']
end

task default: :test
32 changes: 32 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'httparty'
require 'dotenv/load'
require 'awesome_print'
require_relative 'recipient'

class Channel < Recipient

attr_reader :topic, :member_count

def initialize(name, slack_id, topic, member_count)
super(name, slack_id)
@topic = topic
@member_count = member_count
end

def self.list
response = Channel.get("https://slack.com/api/conversations.list")
user_info_array = response["channels"].map do |channel|
name = channel["name"]
slack_id = channel["id"]
topic = channel["purpose"]["value"]
member_count = channel["num_members"]
Channel.new(name, slack_id, topic, member_count )
end
return user_info_array
end

def details
return "Name: #{@name}\nID: #{@slack_id}\nTopic: #{@topic}\nMember Count: #{@member_count}"
end
end

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

class SlackAPIError < StandardError
end

class Recipient
attr_reader :name, :slack_id

def initialize(name, slack_id)
@name = name
@slack_id = slack_id
raise ArgumentError, 'Input cannot be nil' if name.nil? || slack_id.nil?
raise ArgumentError, 'Input cannot be blank' if name.empty? || slack_id.empty?
end

def details
raise NotImplementedError, 'Implement me in a child class!'
end

def self.get(url)
query_paramaters = {
token: ENV['SLACK_TOKEN']
}
response = HTTParty.get(url, query: query_paramaters)
sleep(1)

unless response['ok'] && response.code == 200
raise SlackAPIError, "Error: #{response['error']}, #{response.code}, #{response.message}"

Choose a reason for hiding this comment

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

I love how much information you included in this message!

end
return response
end
end
90 changes: 86 additions & 4 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,94 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative 'workspace'
require_relative 'user'
require_relative 'channel'
require 'httparty'
require 'dotenv/load'
require 'awesome_print'
require 'colorize'
require 'table_print'
require 'pry'


def main
puts "Welcome to the Ada Slack CLI!"
puts "Welcome to the Ada Slack CLI!\n".yellow

Choose a reason for hiding this comment

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

I like the use of colors. 😃

workspace = Workspace.new

# TODO project
puts "There are #{workspace.users.length} users and #{workspace.channels.length} channels.\n".blue

puts "Thank you for using the Ada Slack CLI"
end
exit_loop = false
until(exit_loop)
puts 'Please select an option from below:'.magenta
puts '1. list users'.magenta
puts '2. list channels'.magenta
puts '3. select user'.magenta
puts '4. select channel'.magenta
puts '5. details'.magenta
puts '6. send message'.magenta
puts '7. quit'.magenta
print 'Choice: '.yellow

user_answer = gets.chomp.downcase

case user_answer
when 'list users', 'users', "1"
tp workspace.users, 'name','slack_id', 'real_name'
when 'list channels', 'channels', "2"
tp workspace.channels, 'name', 'slack_id', 'topic', 'member_count'
when 'select user', "3"
puts 'Please enter the Username or Slack ID'
print 'Select User:'.yellow
username_or_id = gets.chomp.downcase
if workspace.select_user(username_or_id)
puts "Selected User: #{workspace.selected.name}".blue
else
puts 'User Not found. Please Try Again'
end
when 'select channel', "4"
puts 'Please enter the Name or Slack ID'
print 'Select Channel:'.yellow
name_or_id = gets.chomp.downcase
if workspace.select_channel(name_or_id)
puts "Selected Channel: #{workspace.selected.name}".blue
else
puts "Channel Not found. Please Try Again"
end
when 'details', 'detail', '5'
if workspace.details
puts "Here are the details...".light_cyan
puts workspace.details
else
puts 'Please choose a user or channel.'
end

when 'send message', 'message', '6'
message = ""
if workspace.selected
while message.empty?
puts "Please type in the message you would like to send.".light_blue
print "Message:".yellow
message = gets.chomp
end
else
puts "Please select a user or a channel"
next
end

begin
if workspace.send_message(message)
puts "Your message \"#{message}\" was successfully sent to #{workspace.selected.slack_id}!".light_magenta
end
rescue SlackAPIError => error
puts error.message
end

when 'quit', 'q', "7"
puts 'Thank you for using the Ada Slack. Have a good day!'.blue
exit_loop = true
else
puts "Invalid input, try again!\n".red
end
end
end
main if __FILE__ == $PROGRAM_NAME
32 changes: 32 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'httparty'
require 'dotenv/load'
require 'awesome_print'
require 'table_print'
require_relative 'recipient'


class User < Recipient
attr_reader :real_name

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

def self.list
response = User.get("https://slack.com/api/users.list")

user_info_array = response["members"].map do |member|
username = member["name"]
real_name = member["real_name"]
slack_id = member["id"]
User.new(username, slack_id, real_name)
end

return user_info_array
end

def details
return "ID: #{@slack_id}\nName: #{@name}\nReal Name: #{@real_name}"
end
end
25 changes: 25 additions & 0 deletions lib/verification_script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'httparty'
require 'dotenv/load'
#require 'awesome_print'
#require 'table_print'

#verify slack token is working
url = "https://slack.com/api/users.list"
query_paramaters = {
token: ENV['SLACK_TOKEN']
}
response = HTTParty.get(url, query: query_paramaters)

user_info_array = []
response["members"].each do |member|
user_info_hash = {
username: member["name"],
real_name: member["real_name"],
slack_id: member["id"]
}

user_info_array << user_info_hash
end

#tp user_info_array

61 changes: 61 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require_relative 'user'
require_relative 'channel'
require_relative 'recipient'
require 'httparty'
require 'dotenv/load'
require 'colorize'

class Workspace
attr_reader :users, :channels, :selected

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

def select_user(user_name_or_id)
user_name_or_id = user_name_or_id.downcase
@selected = @users.find do |user|
valid_users = [user.name.downcase, user.slack_id.downcase, user.real_name.downcase]
valid_users.include?(user_name_or_id)
end
Comment on lines +19 to +22

Choose a reason for hiding this comment

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

Nice use of find!

end

def select_channel(name_or_id)
name_or_id = name_or_id.downcase
unless name_or_id.nil?
@selected = @channels.find { |channel| [channel.name.downcase, channel.slack_id.downcase].include?(name_or_id) }
end
end

def details
if @selected != nil
return @selected.details
else
return nil
end
end

def send_message(message)
if @selected.nil?
return false
end

url = "https://slack.com/api/chat.postMessage"
params = {
token: ENV['SLACK_TOKEN'],
channel: @selected.slack_id,
text: message
}

response = HTTParty.post(url, body: params)

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

return true
end
end

Loading