forked from AdaGold/slack-cli
-
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
Alice/Ting-Yi - SlackCli #11
Open
aliceboone
wants to merge
6
commits into
Ada-C14:master
Choose a base branch
from
aliceboone:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
source 'https://rubygems.org' | ||
ruby '~> 2.6.5' | ||
gem 'dotenv' | ||
gem 'guard' | ||
gem 'guard-minitest' | ||
gem 'httparty' | ||
gem 'minitest-vcr' | ||
gem 'table_print' | ||
gem 'vcr' | ||
gem 'webmock' | ||
gem 'rake' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
addressable (2.7.0) | ||
public_suffix (>= 2.0.2, < 5.0) | ||
coderay (1.1.3) | ||
crack (0.4.4) | ||
dotenv (2.7.6) | ||
ffi (1.13.1) | ||
formatador (0.2.5) | ||
guard (2.16.2) | ||
formatador (>= 0.2.4) | ||
listen (>= 2.7, < 4.0) | ||
lumberjack (>= 1.0.12, < 2.0) | ||
nenv (~> 0.1) | ||
notiffany (~> 0.0) | ||
pry (>= 0.9.12) | ||
shellany (~> 0.0) | ||
thor (>= 0.18.1) | ||
guard-compat (1.2.1) | ||
guard-minitest (2.4.6) | ||
guard-compat (~> 1.2) | ||
minitest (>= 3.0) | ||
hashdiff (1.0.1) | ||
httparty (0.18.1) | ||
mime-types (~> 3.0) | ||
multi_xml (>= 0.5.2) | ||
listen (3.2.1) | ||
rb-fsevent (~> 0.10, >= 0.10.3) | ||
rb-inotify (~> 0.9, >= 0.9.10) | ||
lumberjack (1.2.8) | ||
method_source (1.0.0) | ||
mime-types (3.3.1) | ||
mime-types-data (~> 3.2015) | ||
mime-types-data (3.2020.0512) | ||
minispec-metadata (2.0.0) | ||
minitest | ||
minitest (5.14.2) | ||
minitest-vcr (1.4.0) | ||
minispec-metadata (~> 2.0) | ||
minitest (>= 4.7.5) | ||
vcr (>= 2.9) | ||
multi_xml (0.6.0) | ||
nenv (0.3.0) | ||
notiffany (0.1.3) | ||
nenv (~> 0.1) | ||
shellany (~> 0.0) | ||
pry (0.13.1) | ||
coderay (~> 1.1) | ||
method_source (~> 1.0) | ||
public_suffix (4.0.6) | ||
rake (13.0.1) | ||
rb-fsevent (0.10.4) | ||
rb-inotify (0.10.1) | ||
ffi (~> 1.0) | ||
shellany (0.0.1) | ||
table_print (1.5.7) | ||
thor (1.0.1) | ||
vcr (6.0.0) | ||
webmock (3.9.1) | ||
addressable (>= 2.3.6) | ||
crack (>= 0.3.2) | ||
hashdiff (>= 0.4.0, < 2.0.0) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
dotenv | ||
guard | ||
guard-minitest | ||
httparty | ||
minitest-vcr | ||
rake | ||
table_print | ||
vcr | ||
webmock | ||
|
||
RUBY VERSION | ||
ruby 2.6.5p114 | ||
|
||
BUNDLED WITH | ||
2.1.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require_relative 'recipient' | ||
|
||
module SlackCli | ||
# Create class and constructor for channels | ||
class Channels < Recipient | ||
attr_reader :slack_id, :channel_name, :topic, :member_count | ||
|
||
def initialize(slack_id:, channel_name:, topic:, member_count:) | ||
super (slack_id) | ||
|
||
@channel_name = channel_name | ||
@topic = topic | ||
@member_count = member_count.to_i | ||
end | ||
|
||
# Define how channel details will be displayed | ||
def details | ||
details_channel = | ||
" - Slack ID: #{@slack_id}\n"+ | ||
" - Channel Name: #{@channel_name}\n"+ | ||
" - Topic: #{@topic}\n"+ | ||
" - Member Count: #{@member_count}" | ||
return details_channel | ||
end | ||
|
||
# Define how to get channel data from the API using the URL | ||
def self.load_data | ||
response = super('conversations.list') | ||
channel_list = [] | ||
response["channels"].each do |channel| | ||
channel_list.push(Channels.new(slack_id: channel["id"], channel_name: channel["name"], topic: channel["topic"]["value"], member_count: channel["num_members"].to_i)) | ||
end | ||
return channel_list | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'httparty' | ||
|
||
BASE_URL = "https://slack.com/api/" | ||
|
||
module SlackCli | ||
class SlackCliError < StandardError; end | ||
# Create class and constructor for recipient | ||
class Recipient | ||
attr_reader :slack_id | ||
|
||
def initialize(slack_id) | ||
self.class.validate_id(slack_id) | ||
@slack_id = slack_id | ||
end | ||
|
||
# Request to get data from the API | ||
def self.load_data(specific_list) | ||
url = "#{BASE_URL}#{specific_list}" | ||
query_items = {token: ENV["SLACK_TOKEN"]} | ||
response = HTTParty.get(url, query: query_items) | ||
return response | ||
Comment on lines
+20
to
+21
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. Great error handling on line 42! This could use something similar here |
||
end | ||
|
||
# Validate ID | ||
def self.validate_id(slack_id) | ||
if slack_id.nil? | ||
raise ArgumentError, 'ID cannot be blank or less than one.' | ||
end | ||
end | ||
|
||
# Request to post data to the API | ||
def send_msg(message) | ||
url = "#{BASE_URL}chat.postMessage" | ||
|
||
response = HTTParty.post(url, | ||
headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }, | ||
body: { | ||
token: ENV["SLACK_TOKEN"], | ||
channel: slack_id, | ||
text: message | ||
}) | ||
unless response.code == 200 || response.parsed_response["ok"] | ||
raise SlackCli::SlackCliError, "Error: #{response.parsed_response["error"]}" | ||
end | ||
return true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,63 @@ | ||
#!/usr/bin/env ruby | ||
require 'dotenv' | ||
require 'table_print' | ||
|
||
require_relative 'workspace' | ||
# Define method to display menu | ||
def menu | ||
puts "Choose a number for the options bellow!:\n 1 - List Users\n"+" 2 - List Channels\n"+" 3 - Select user\n"+" 4 - Select channel\n"+" 5 - Details\n"+" 6 - Send a message\n"+" 7 - Exit\n" | ||
end | ||
|
||
def main | ||
puts "Welcome to the Ada Slack CLI!" | ||
workspace = Workspace.new | ||
|
||
# TODO project | ||
Dotenv.load # Raise an error if Key is not correct | ||
unless ENV["SLACK_TOKEN"] | ||
raise ArgumentError, "Could not load API Key" | ||
end | ||
# Define how user will see menu options and iterate with menu | ||
puts "***** Welcome to the Ada Slack CLI! *****" | ||
workspace = SlackCli::Workspace.new | ||
done = false | ||
for_detail = "" | ||
until done | ||
menu | ||
user_command = gets.chomp | ||
case user_command | ||
when "7" | ||
done = true | ||
when "1" | ||
tp workspace.all_users, "slack_id", "username", "real_name" | ||
when "2" | ||
tp workspace.all_channels, "slack_id", "channel_name", "topic", "member_count" | ||
when "3" | ||
puts "Enter a username or slack_id:" | ||
user_input = gets.chomp | ||
for_detail = workspace.selected_user(user_input) | ||
if for_detail.nil? | ||
puts "Can not find the user." | ||
end | ||
when "4" | ||
puts "Enter a channel name or slack_id:" | ||
channel_input = gets.chomp | ||
for_detail = workspace.selected_channel(channel_input) | ||
if for_detail.nil? | ||
puts "Can not find the channel." | ||
end | ||
|
||
when "5" | ||
puts for_detail.details | ||
when "6" | ||
if for_detail.nil? | ||
puts "Yor message can not be sent!" | ||
else | ||
puts "Please type the message you want to send!" | ||
send_msg_input = gets.chomp | ||
for_detail.send_msg(send_msg_input) | ||
puts "Your message was sent with success!" | ||
end | ||
else | ||
puts "Invalid number, try again!" | ||
end | ||
end | ||
puts "Thank you for using the Ada Slack CLI" | ||
end | ||
|
||
main if __FILE__ == $PROGRAM_NAME |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require_relative 'recipient' | ||
|
||
# Create class and constructor for users | ||
module SlackCli | ||
class Users < Recipient | ||
attr_reader :slack_id, :username, :real_name | ||
|
||
def initialize(slack_id:, username:, real_name:) | ||
super(slack_id) | ||
|
||
@username = username | ||
@real_name = real_name | ||
end | ||
|
||
# Define how user details will be displayed | ||
def details | ||
details_user = | ||
" - Slack ID: #{@slack_id}\n"+ | ||
" - Username: #{@username}\n"+ | ||
" - Real Name: #{@real_name}" | ||
|
||
return details_user | ||
end | ||
|
||
# Define how to get users data from the API using the URL | ||
def self.load_data | ||
response = super('users.list') | ||
user_list = [] | ||
response["members"].each do |user| | ||
user_list.push(Users.new(slack_id: user["id"], username: user["name"], real_name: user["real_name"])) | ||
end | ||
return user_list | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require_relative 'channels' | ||
require_relative 'users' | ||
|
||
module SlackCli | ||
# Create class and constructor for workspace | ||
class Workspace | ||
attr_reader :users, :channels | ||
|
||
def initialize | ||
@users = Users.load_data | ||
@channels = Channels.load_data | ||
end | ||
|
||
# Define method to display all users | ||
def all_users | ||
return @users | ||
end | ||
# Define method to display all channels | ||
def all_channels | ||
return @channels | ||
end | ||
|
||
# Define method to select all users | ||
def selected_user(user_input) | ||
user_by_name = @users.find {|user| user.username == user_input} | ||
user_by_id = @users.find {|user| user.slack_id == user_input} | ||
if user_by_name != nil | ||
@selected_user = user_by_name | ||
elsif user_by_id != nil | ||
@selected_user = user_by_id | ||
end | ||
return @selected_user | ||
end | ||
|
||
# Define method to select all channels | ||
def selected_channel(channel_input) | ||
channel_by_name = @channels.find {|channel| channel.channel_name == channel_input} | ||
channel_by_id = @channels.find {|channel| channel.slack_id == channel_input} | ||
if channel_by_name != nil | ||
@selected_channel= channel_by_name | ||
elsif channel_by_id != nil | ||
@selected_channel = channel_by_id | ||
end | ||
return @selected_channel | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Since this method should be defined for all children of Recipient, it is appropriate for there to be a template method called
details
in the Recipient class.