-
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
FIRE: Kalki and Sandy SLACK CLI #20
base: master
Are you sure you want to change the base?
Changes from all commits
ace8848
6a3f9a9
99219f3
8782ed0
156d9b4
2082a8d
1f81672
eea1739
64d530a
dc9e3c6
1906eab
efa950c
a1b4cdc
d2d94a5
1529f1d
5e55a78
73ab05b
3488694
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 |
---|---|---|
|
@@ -3,4 +3,6 @@ | |
.DS_Store | ||
|
||
# Ignore environemnt variables | ||
.env | ||
test/.env | ||
|
||
.env |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2.6.6 |
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 | ||
|
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}" | ||
end | ||
return response | ||
end | ||
end |
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 | ||
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. 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 |
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 |
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 | ||
|
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
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. Nice use of |
||
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 | ||
|
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.
I love how much information you included in this message!