-
Notifications
You must be signed in to change notification settings - Fork 50
Space - Antonia #45
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?
Space - Antonia #45
Changes from all commits
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,32 @@ | ||
| require_relative 'recipient' | ||
|
|
||
| class Channel < Recipient | ||
| attr_reader :topic, :member_count | ||
| def initialize(topic:, member_count:, name:, slack_id:) | ||
| super(slack_id: slack_id, name: name) | ||
| @topic = topic | ||
| @member_count = member_count | ||
| end | ||
|
|
||
| def details | ||
| return details = "Slack ID: #{self.slack_id}, Name: #{self.name}, Topic: #{self.topic}, Member Count: #{self.member_count}" | ||
| end | ||
|
|
||
| def self.list_all | ||
|
|
||
| data = self.get("https://slack.com/api/conversations.list") | ||
|
|
||
| channels = [] | ||
|
|
||
| data["channels"].each do |item| | ||
| channels << Channel.new( | ||
| name: item["name"], | ||
| slack_id: item["id"], | ||
| topic: item["topic"]["value"], | ||
| member_count: item["num_members"] | ||
| ) | ||
| end | ||
|
|
||
| return channels | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| require 'httparty' | ||
|
|
||
| class Recipient | ||
|
|
||
| attr_reader :slack_id, :name | ||
|
|
||
| def initialize(slack_id:, name:) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| end | ||
|
|
||
| def send_message(message) | ||
| #send message using HTTParty | ||
| response = HTTParty.post("https://slack.com/api/chat.postMessage", body: {token: ENV['SLACK_TOKEN'], text: message, channel: @slack_id}) | ||
| if response.code != 200 || response['ok'] == false | ||
| raise SlackAPIError | ||
| end | ||
|
|
||
| return true | ||
|
|
||
| end | ||
|
|
||
| def self.get(url) | ||
| response = HTTParty.get(url, query: {token: ENV['SLACK_TOKEN']}) | ||
| if response.code != 200 || response['ok'] == false | ||
| raise SlackAPIError | ||
| end | ||
|
|
||
| return response | ||
|
|
||
| end | ||
|
|
||
| # def self.list_all | ||
| # end | ||
|
|
||
|
|
||
| end | ||
|
|
||
| class SlackAPIError < Exception | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,67 @@ | ||
| #!/usr/bin/env ruby | ||
| require 'dotenv' | ||
| require 'httparty' | ||
| require 'table_print' | ||
|
|
||
| require_relative 'workspace' | ||
|
|
||
| #try to place dotenv.load on first file that loads | ||
| Dotenv.load | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| workspace = Workspace.new | ||
|
|
||
| input = nil | ||
| # TODO project | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| until input == 'quit' | ||
| puts "\n What would you like to do? Choose one of the following numbers:" | ||
| puts "\n 1. List channels \n 2. List users \n 3. Select Channel \n 4. Select User \n 5. Get details \n 6. Send message" | ||
| puts "\n Or type QUIT to exit" | ||
|
|
||
| input = gets.chomp | ||
| begin | ||
| case input | ||
| when "1" | ||
| tp workspace.channels, "slack_id", "name", "member_count", "topic" | ||
| when "2" | ||
| tp workspace.users, "slack_id", "name", "real_name", "status_text", "status_emoji" | ||
| when "3" | ||
| selected_channel = nil | ||
| # selected_channel = workspace.select_channel | ||
| while selected_channel.nil? | ||
| puts "Please enter a channel name or ID" | ||
| input = gets.chomp | ||
| selected_channel = workspace.select_channel(input) | ||
| end | ||
|
|
||
| when "4" | ||
| selected_user = nil | ||
| while selected_user.nil? | ||
| puts "\nPlease enter a user name or ID" | ||
| input = gets.chomp | ||
| selected_user = workspace.select_user(input) | ||
| end | ||
| when "5" | ||
| if workspace.selected.nil? | ||
| puts "\nPlease select a channel or user to view their details!" | ||
| else | ||
| puts workspace.show_details | ||
|
|
||
| end | ||
| when "6" | ||
| if workspace.selected.nil? | ||
| puts "\nPlease select a channel or user to send a message to! Or enter QUIT to exit" | ||
| else | ||
| puts "\nPlease enter your message" | ||
| message = gets.chomp | ||
| workspace.selected.send_message(message) | ||
| end | ||
| when "quit" || "QUIT" || "Quit" | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| require_relative 'recipient' | ||
|
|
||
| class User < Recipient | ||
| attr_reader :real_name, :status_text, :status_emoji | ||
|
|
||
| def initialize(real_name:, status_text:, status_emoji:, name:, slack_id:) | ||
| super(slack_id: slack_id, name: name) | ||
| @real_name = real_name | ||
| @status_text = status_text | ||
| @status_emoji = status_emoji | ||
| end | ||
|
|
||
| def details | ||
| return details = "Slack ID: #{self.slack_id}, Name: #{self.name}, Real Name: #{self.real_name}, Status Text: #{self.status_text}, Status Emoji: #{self.status_emoji}" | ||
| end | ||
|
|
||
| def self.list_all | ||
| #get the data | ||
| data = self.get("https://slack.com/api/users.list") | ||
|
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. As a general rule, every API call should include some error handling. (There is always going to be the possibility of a network problem, the API url getting changed, etc.) |
||
| users = [] | ||
| data["members"].each do |user| | ||
| users << User.new( | ||
| name: user["name"], | ||
| slack_id: user["id"], | ||
| real_name: user["profile"]["real_name"], | ||
| status_emoji: user["profile"]["status_emoji"], | ||
| status_text: user["profile"][["status_text"]] | ||
| ) | ||
| end | ||
|
|
||
| return users | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| require_relative 'user' | ||
| require_relative 'channel' | ||
| require_relative 'recipient' | ||
|
|
||
| class Workspace | ||
| attr_reader :users, :channels, :selected | ||
|
|
||
| def initialize | ||
| @users = User.list_all | ||
| @channels = Channel.list_all | ||
| @selected = nil | ||
| end | ||
|
|
||
| def select_channel(input) | ||
| @selected = @channels.find { |channel| channel.name == input.downcase || channel.slack_id == input.upcase} | ||
| end | ||
|
|
||
| def select_user(input) | ||
| @selected = @users.find { |user| user.name == input.downcase || user.slack_id == input.upcase} | ||
| end | ||
|
|
||
| def show_details | ||
| return @selected.details | ||
| end | ||
|
|
||
| def send_message(message) | ||
| return @selected.send_message(message) | ||
| 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.
This honestly is generally pretty sufficient! The one thing I would add is figuring out what part of the response gives a useful message explaining what went wrong and making sure to record that as a property of SlackAPIError.
If it's unclear what I mean by that, please ask!