-
Notifications
You must be signed in to change notification settings - Fork 26
Leaves - Hallie/Yitgop - Slack-CLI #18
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
base: master
Are you sure you want to change the base?
Changes from all commits
808cec0
3e7b460
d4f2459
883542f
2e469c2
31f3621
ffac747
09082b1
3efa872
d84ee69
5a46dce
7db1885
f491669
dc1c704
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| require_relative 'recipient' | ||
|
|
||
| module SlackBot | ||
| class Channel < Recipient | ||
| attr_reader :topic, :member_count | ||
|
|
||
| def initialize(slack_id:, name:, topic: nil, member_count:) | ||
| super(slack_id: slack_id, name: name) | ||
|
|
||
| @topic = topic || nil | ||
| @member_count = member_count | ||
| end | ||
|
|
||
| def details | ||
| tp self, "slack_id", "name", "topic", "member_count" | ||
| end | ||
|
|
||
| def self.list | ||
| response = Channel.get("https://slack.com/api/conversations.list") | ||
| channel_list = [] | ||
|
|
||
| response["channels"].each do |channel| | ||
| channel_list << Channel.new( | ||
| slack_id: channel["id"], | ||
| name: channel["name"], | ||
| topic: channel["topic"]["value"], | ||
| member_count: channel["num_members"] | ||
| ) | ||
| end | ||
|
|
||
| return channel_list | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| require 'httparty' | ||
|
|
||
| module SlackBot | ||
| class Recipient | ||
| attr_reader :slack_id, :name | ||
|
|
||
| def initialize(slack_id:, name:) | ||
| @slack_id = slack_id | ||
| @name = name | ||
|
|
||
| if @slack_id.class != String || @slack_id.length != 9 | ||
| raise ArgumentError.new("Failure: slack_id must be a 9 character string") | ||
| end | ||
| end | ||
|
|
||
| def self.get(url) | ||
| response = HTTParty.get(url, query: {token: ENV["SLACK_TOKEN"]}) | ||
|
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 that this is at a low level of the program rather than being passed around |
||
|
|
||
| if response["ok"] == false | ||
| raise Exception | ||
| else | ||
| return response | ||
| end | ||
| end | ||
|
|
||
| def send_message(slack_id:, message:) | ||
| response = HTTParty.post("https://slack.com/api/chat.postMessage", query: {token: ENV["SLACK_TOKEN"], channel: slack_id, text: message}) | ||
|
|
||
| if response["ok"] == false | ||
| raise Exception, "Failure: message did not send.".colorize(:red) | ||
| else | ||
| return response | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def details | ||
| raise NotImplementedError, 'Implement me in a child class!' | ||
|
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! |
||
| end | ||
|
|
||
| def self.list | ||
| raise NotImplementedError, 'Implement me in a child class!' | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,78 @@ | ||
| #!/usr/bin/env ruby | ||
| require 'dotenv' | ||
| require 'httparty' | ||
| require_relative 'recipient' | ||
| require_relative 'channel' | ||
| require_relative 'user' | ||
| require_relative 'workspace' | ||
| require 'colorize' | ||
| require 'table_print' | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| Dotenv.load | ||
|
|
||
| # !/usr/bin/env ruby | ||
|
|
||
| # TODO project | ||
| # unless ENV['SLACK_TOKEN'] | ||
| # puts "Could not load API key, please store in the environment variable 'SLACK_TOKEN'" | ||
| # exit | ||
| # end | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| def main | ||
| workspace = SlackBot::Workspace.new | ||
| valid_inputs = ["list users", "list channels", "select user", "select channel", "details", "send_message", "quit"] | ||
| user_input = nil | ||
|
|
||
| until valid_inputs.include?(user_input) | ||
| puts "\nPlease make a selection:" | ||
| puts "List Users" | ||
| puts "List Channels" | ||
| puts "Select User" | ||
| puts "Select Channel" | ||
| puts "Details" | ||
| puts "Send Message" | ||
| puts "Quit" | ||
| print "Selection: " | ||
| user_input = gets.chomp.downcase | ||
|
|
||
| case user_input | ||
| when "list users" | ||
| tp workspace.users, "slack_id", "name", "real_name" | ||
| user_input = nil | ||
| when "list channels" | ||
| tp workspace.channels, "slack_id", "name", "topic", "member_count" | ||
| user_input = nil | ||
| when "select user" | ||
| print "Enter a Username or Slack ID: " | ||
| user_criteria = gets.chomp | ||
| workspace.select_user(user_criteria) | ||
| user_input = nil | ||
| when "select channel" | ||
| print "Enter a Channel Name or Slack ID: " | ||
| channel_criteria = gets.chomp | ||
| workspace.select_channel(channel_criteria) | ||
| user_input = nil | ||
| when "details" | ||
| if workspace.selected == nil | ||
| puts "No user or channel has been selected.".colorize(:red) | ||
| user_input = nil | ||
| else | ||
| workspace.show_details | ||
| user_input = nil | ||
| end | ||
| when "send message" | ||
| if workspace.selected == nil | ||
| puts "No user or channel has been selected.".colorize(:red) | ||
| user_input = nil | ||
| else | ||
| puts "What message you would like to send to #{workspace.selected.name}: " | ||
| message = gets.chomp | ||
| workspace.send_message(message: message) | ||
| user_input = nil | ||
| end | ||
| when "quit" | ||
| puts "\nThank you for using the Ada Slack CLI".colorize(:yellow) | ||
| exit | ||
| end | ||
| end | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| main if __FILE__ == $PROGRAM_NAME |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| require_relative 'recipient' | ||
|
|
||
| module SlackBot | ||
| class User < Recipient | ||
| attr_reader :real_name, :status_text, :status_emoji | ||
|
|
||
| def initialize(slack_id:, name:, real_name:, status_text: nil, status_emoji: nil) | ||
| super(slack_id: slack_id, name: name) | ||
|
|
||
| @real_name = real_name | ||
| @status_text = status_text || nil | ||
| @status_emoji = status_emoji || nil | ||
| end | ||
|
|
||
| def details | ||
| output =tp self, "slack_id", "name", "real_name" | ||
| return output | ||
| end | ||
|
|
||
| def self.list | ||
| response = User.get("https://slack.com/api/users.list") | ||
| users = [] | ||
|
|
||
| response["members"].each do |member| | ||
| users << User.new( | ||
| slack_id: member["id"], | ||
| name: member["name"], | ||
| real_name: member["real_name"] | ||
| ) | ||
| end | ||
|
|
||
| return users | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| require 'table_print' | ||
|
|
||
| require_relative 'channel' | ||
| require_relative 'user' | ||
|
|
||
| module SlackBot | ||
| class Workspace | ||
| attr_reader :users, :channels, :selected | ||
|
|
||
| def initialize | ||
| @users = User.list | ||
| @channels = Channel.list | ||
| @selected = nil | ||
| end | ||
|
|
||
| def select_channel(criteria) | ||
| @channels.each do |channel| | ||
| if channel.name == criteria.downcase | ||
| @selected = channel | ||
| return puts "Channel #{@selected.name} is selected" | ||
| end | ||
| end | ||
| @channels.each do |channel| | ||
| if channel.slack_id == criteria.upcase | ||
| @selected = channel | ||
| return puts "Channel #{@selected.name} is selected" | ||
| end | ||
| end | ||
| raise ArgumentError, "Invalid Enty: No such channel." | ||
| end | ||
|
|
||
| def select_user(criteria) | ||
| @users.each do |user| | ||
| if user.name == criteria.downcase | ||
| @selected = user | ||
| return puts "User #{@selected.name} is selected" | ||
| end | ||
| end | ||
| @users.each do |user| | ||
| if user.slack_id == criteria.upcase | ||
| @selected = user | ||
| return puts "User #{@selected.name} is selected" | ||
| end | ||
| end | ||
| raise ArgumentError, "Invalid Enty: No such user." | ||
| end | ||
|
|
||
| def show_details | ||
| @selected.details | ||
| end | ||
|
|
||
| def send_message(message:) | ||
| response = @selected.send_message(message: message, slack_id: @selected.slack_id) | ||
| if response["ok"] == true | ||
| return puts "message sent" | ||
| end | ||
| end | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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'd put this URL in a variable, just for clarity's sake. I'm being picky.