-
Notifications
You must be signed in to change notification settings - Fork 26
Leaves - Samantha & Julia #19
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
74e0a9e
c8ec539
42ccaf6
87487e6
865d4c0
4615362
f73e4ba
1e404da
a296fd4
a93d007
654f453
a5f4665
638df86
28c942e
7eb0e64
99e7d8b
e56fa67
ce0a3c4
bb5619b
c4be21b
952b415
322c459
b909f0e
9e07dbd
fadcfc2
aa7ad08
a8e70c3
8bcc446
53f06cd
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" | ||
| require "httparty" | ||
| require 'pry' | ||
|
|
||
| module Slack | ||
| class Channel < Recipient | ||
| attr_reader :topic, :member_count | ||
|
|
||
| def initialize(slack_id, name, topic = nil, member_count) | ||
| super(slack_id, name) | ||
| @topic = topic | ||
| @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") | ||
| channels = [] | ||
|
|
||
| response["channels"].each do |channel| | ||
| channels << Channel.new( | ||
| slack_id = channel["id"], | ||
| name = channel["name"], | ||
| topic = channel["topic"]["value"], | ||
| member_count = channel["num_members"], | ||
| ) | ||
| end | ||
| return channels | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| require "httparty" | ||
| require 'pry' | ||
|
|
||
| class API_Error < StandardError | ||
| end | ||
| module Slack | ||
| class Recipient | ||
| URL = "https://slack.com/api/users.list" | ||
| attr_reader :slack_id, :name | ||
|
|
||
| def initialize(slack_id, name) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| end | ||
|
|
||
| def details | ||
| raise API_Error.new, 'Implement me in a child class!' | ||
| end | ||
|
|
||
| def self.list | ||
| raise API_Error.new, 'Implement me in a child class!' | ||
| end | ||
|
Comment on lines
+16
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. Yeah! |
||
|
|
||
| def self.get(url) | ||
| response = HTTParty.get(url, query: {token: ENV['SLACK_API_TOKEN']}) | ||
| return response | ||
| end | ||
|
|
||
| def send_message(slack_id, message) | ||
| response = HTTParty.post("https://slack.com/api/chat.postMessage", query: {token: ENV['SLACK_API_TOKEN'], channel: slack_id, text: message}) | ||
|
|
||
| unless response.code == 200 | ||
| raise ExceptionError, "Message not sent." | ||
| end | ||
| return response | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,77 @@ | ||||||||||||||||
| #!/usr/bin/env ruby | ||||||||||||||||
| require "dotenv" | ||||||||||||||||
| require "httparty" | ||||||||||||||||
| require "table_print" | ||||||||||||||||
|
|
||||||||||||||||
| def main | ||||||||||||||||
| puts "Welcome to the Ada Slack CLI!" | ||||||||||||||||
| require_relative "recipient" | ||||||||||||||||
| require_relative "user" | ||||||||||||||||
| require_relative "channel" | ||||||||||||||||
| require_relative "workspace" | ||||||||||||||||
|
|
||||||||||||||||
| # TODO project | ||||||||||||||||
| Dotenv.load | ||||||||||||||||
|
|
||||||||||||||||
| puts "Thank you for using the Ada Slack CLI" | ||||||||||||||||
| def main | ||||||||||||||||
| workspace = Slack::Workspace.new | ||||||||||||||||
| puts "\n" | ||||||||||||||||
| puts "Welcome to the Ada Slack CLI! This Slack workspace currently has #{workspace.users.count} users and #{workspace.channels.count} channels. Press enter to continue." | ||||||||||||||||
|
|
||||||||||||||||
| user_input = gets.chomp | ||||||||||||||||
|
|
||||||||||||||||
| until user_input == "quit" | ||||||||||||||||
|
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'd do this slightly differently.
Suggested change
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'd maybe make it a bool, and give it a better name, but this way you address the quit case separately. |
||||||||||||||||
| print "Please choose an option: list users, list channels, select user, select channel, details, send message, or quit: " | ||||||||||||||||
| user_input = gets.chomp.downcase | ||||||||||||||||
|
|
||||||||||||||||
| case user_input | ||||||||||||||||
| when "list users" | ||||||||||||||||
| tp workspace.users, "slack_id", "name", "real_name" | ||||||||||||||||
| puts "\n" | ||||||||||||||||
| user_input = nil | ||||||||||||||||
|
|
||||||||||||||||
| when "list channels" | ||||||||||||||||
| tp workspace.channels, "name", "topic", "member_count", "slack_id" | ||||||||||||||||
| puts "\n" | ||||||||||||||||
| user_input = nil | ||||||||||||||||
|
|
||||||||||||||||
| when "select user" | ||||||||||||||||
| print "Please enter the user name or ID: " | ||||||||||||||||
| name_or_id = gets.chomp | ||||||||||||||||
| puts workspace.select(name_or_id) | ||||||||||||||||
| puts "\n" | ||||||||||||||||
|
|
||||||||||||||||
| when "select channel" | ||||||||||||||||
| print "Please enter the channel name or ID: " | ||||||||||||||||
| name_or_id = gets.chomp | ||||||||||||||||
| puts workspace.select(name_or_id) | ||||||||||||||||
| puts "\n" | ||||||||||||||||
|
|
||||||||||||||||
| when "details" | ||||||||||||||||
| if workspace.selected == nil | ||||||||||||||||
| puts "Please select a user or channel." | ||||||||||||||||
| user_input = nil | ||||||||||||||||
| puts "\n" | ||||||||||||||||
| else | ||||||||||||||||
| workspace.show_details | ||||||||||||||||
| user_input = nil | ||||||||||||||||
| puts "\n" | ||||||||||||||||
| end | ||||||||||||||||
| when "send message" | ||||||||||||||||
| if workspace.selected == nil | ||||||||||||||||
| puts "Please select a user or channel." | ||||||||||||||||
| user_input = nil | ||||||||||||||||
| puts "\n" | ||||||||||||||||
| else | ||||||||||||||||
| print "Please enter your message: " | ||||||||||||||||
| message = gets.chomp | ||||||||||||||||
| slack_id = workspace.select(name_or_id) | ||||||||||||||||
| workspace.user_message(message, slack_id) | ||||||||||||||||
| puts "\n" | ||||||||||||||||
| end | ||||||||||||||||
| else | ||||||||||||||||
| puts "Sorry, I didn't understand your request. Please try again." | ||||||||||||||||
|
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. The |
||||||||||||||||
| puts "\n" | ||||||||||||||||
| end | ||||||||||||||||
| end | ||||||||||||||||
| puts "Thank you for using the ADA Slack CLI!" | ||||||||||||||||
| puts "\n" | ||||||||||||||||
| end | ||||||||||||||||
|
|
||||||||||||||||
| main if __FILE__ == $PROGRAM_NAME | ||||||||||||||||
| main if __FILE__ == $PROGRAM_NAME | ||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| require_relative "recipient" | ||
| require "httparty" | ||
| require 'pry' | ||
|
|
||
| module Slack | ||
| class User < Recipient | ||
| attr_reader :real_name | ||
|
|
||
| def initialize(slack_id, name, real_name) | ||
| super(slack_id, name) | ||
| @real_name = real_name | ||
| end | ||
|
|
||
| def details | ||
| tp self, "slack_id", "name", "real_name" | ||
| end | ||
|
|
||
| def self.list | ||
| response = User.get("https://slack.com/api/users.list") | ||
| users = [] | ||
|
|
||
| response["members"].each do |user| | ||
| users << User.new( | ||
| slack_id = user["id"], | ||
| name = user["name"], | ||
| real_name = user["real_name"], | ||
| ) | ||
| end | ||
| return users | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| require_relative "user" | ||
| require_relative "channel" | ||
|
|
||
| module Slack | ||
| class Workspace | ||
| attr_reader :users, :channels, :selected | ||
|
|
||
| def initialize | ||
| @users = User.list | ||
| @channels = Channel.list | ||
| @selected = nil | ||
| end | ||
|
|
||
| def select(name_or_id) | ||
| @channels.each do |channel| | ||
| if channel.name == name_or_id || channel.slack_id == name_or_id | ||
| @selected = channel | ||
| return "Okay, #{@selected.name} is selected." | ||
| else | ||
| @users.each do |user| | ||
| if user.name == name_or_id || user.slack_id == name_or_id | ||
| @selected = user | ||
| return "Okay, #{@selected.name} is selected." | ||
| end | ||
| end | ||
| end | ||
| end | ||
| puts "Sorry, I couldn't find that user or channel." | ||
| end | ||
|
|
||
| def show_details | ||
| return @selected.details | ||
| end | ||
|
|
||
| def user_message(message, slack_id) | ||
| @selected.send_message(@selected.slack_id, message) | ||
| return "Success! Message sent to #{@selected.name}." | ||
| 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.
nice move implementing this here!