-
Notifications
You must be signed in to change notification settings - Fork 26
Leaves - Dianna, Sabrina, and Tiffany #4
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
10fe2fd
914696b
470c464
aa78142
7819a1e
dabd90c
7803a92
ce2f6ce
632bebf
4f373cc
6832248
2901ef4
9f320f8
0d469a3
d5b7b8b
6f47678
3bdeaf1
1cdcddf
18d1f11
d059324
4443b17
deb9446
382a8f9
d3074e6
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 @@ | ||
| {"bot_name":"Slackbot","bot_avatar":"jack_o_lantern"} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| module SlackCLI | ||
| class Channel < Recipient | ||
| attr_reader :topic, :member_count | ||
|
|
||
| def initialize(slack_id:, name:, topic:, member_count:) | ||
| super(slack_id, name) | ||
| @topic = topic | ||
| @member_count = member_count | ||
| end | ||
|
|
||
| def get_message_history | ||
| url = "https://slack.com/api/conversations.history" | ||
| query = { token: ENV["SLACK_API_TOKEN"] , channel: slack_id, limit: 25} | ||
| response = HTTParty.get(url, query: query) | ||
|
|
||
| unless response.code == 200 && response.parsed_response["ok"] | ||
| raise SlackAPIError, "Error when getting message history, error: #{response.parsed_response["error"]}" | ||
| end | ||
|
|
||
| return response | ||
| end | ||
|
|
||
| def self.all | ||
| channels = [] | ||
|
|
||
| response = Channel.get("channels.list") | ||
|
|
||
| response["channels"].each do |channel| | ||
| slack_id = channel["id"] | ||
| name = channel["name"] | ||
| topic = channel["topic"]["value"] | ||
| member_count = channel["num_members"] | ||
|
|
||
| channels << SlackCLI::Channel.new(slack_id: slack_id, name: name, topic: topic, member_count: member_count) | ||
| end | ||
| return channels | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| module SlackCLI | ||
| class SlackAPIError < StandardError | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| module SlackCLI | ||
|
|
||
| class Recipient | ||
| attr_reader :slack_id, :name | ||
|
|
||
| def initialize(slack_id, name) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| end | ||
|
|
||
| def self.get(url) | ||
| base_url = "https://slack.com/api/" | ||
| url = base_url + url | ||
| query = { token: ENV["SLACK_API_TOKEN"] } | ||
| response = HTTParty.get(url, query: query) | ||
|
|
||
| unless response.code == 200 && response.parsed_response["ok"] | ||
|
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 work adding this exception raising here. |
||
| raise SlackAPIError, "Error: #{response.parsed_response["error"]}" | ||
| end | ||
|
|
||
| return response | ||
| end | ||
|
|
||
| def self.all | ||
| raise NotImplementedError | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,171 @@ | ||
| #!/usr/bin/env ruby | ||
| require "dotenv" | ||
| require "httparty" | ||
| require 'table_print' | ||
| require_relative "recipient" | ||
| require_relative "user" | ||
| require_relative "workspace" | ||
| require_relative "channel" | ||
| require_relative "errors" | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| Dotenv.load | ||
|
|
||
| PUMPKIN_SPICE = SlackCLI::Workspace.new() | ||
|
|
||
| MAIN_MENU = ["List Users", "List Channels", "Select User", "Select Channel", "Details", "Send Message", "Get Message History", "Change Bot Settings", "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. Good use of a global constant. |
||
|
|
||
| def print_workplace_stats() | ||
| puts PUMPKIN_SPICE.get_workplace_stats() | ||
| end | ||
|
|
||
| # TODO project | ||
| def print_menu() | ||
| puts "\nMAIN MENU" | ||
| MAIN_MENU.each_with_index do |menu_item, index| | ||
| puts "#{index + 1}. #{menu_item}" | ||
| end | ||
| puts "\n" | ||
| end | ||
|
|
||
| def print_users | ||
| puts "\n" | ||
| tp PUMPKIN_SPICE.users, "name", "real_name", "slack_id" | ||
| end | ||
|
|
||
| def print_channels | ||
| puts "\n" | ||
| tp PUMPKIN_SPICE.channels, "name", {"topic" => {:width => 60}}, "member_count", "slack_id" | ||
| end | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| def select_user | ||
| print "Please enter the name or Slack ID of the user you want to select: " | ||
| search_term = gets.chomp | ||
|
|
||
| result = PUMPKIN_SPICE.select_user(search_term) | ||
|
|
||
| puts | ||
| if result | ||
| puts "The user #{result.name} was found and selected." | ||
| else | ||
| puts "No user was found." | ||
| end | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| def select_channel | ||
| print "Please enter the name or Slack ID of the channel you want to select: " | ||
| search_term = gets.chomp | ||
|
|
||
| result = PUMPKIN_SPICE.select_channel(search_term) | ||
|
|
||
| puts | ||
| if result | ||
| puts "The channel #{result.name} was found and selected." | ||
| else | ||
| puts "No channel was found." | ||
| end | ||
| end | ||
|
|
||
| def print_details() | ||
| puts | ||
| if PUMPKIN_SPICE.selected.class == SlackCLI::User | ||
| tp PUMPKIN_SPICE.selected, "name", "real_name", "slack_id" | ||
| elsif PUMPKIN_SPICE.selected.class == SlackCLI::Channel | ||
| tp PUMPKIN_SPICE.selected, "name", {"topic" => {:width => 60}}, "member_count", "slack_id" | ||
| else | ||
| puts "There is no recipient selected." | ||
| end | ||
| end | ||
|
|
||
| def get_message() | ||
| puts | ||
| if PUMPKIN_SPICE.selected | ||
| print "Please enter a message: " | ||
| message = gets.chomp | ||
|
|
||
| PUMPKIN_SPICE.send_message(message) | ||
| else | ||
| puts "You need to select a recipient." | ||
| end | ||
| end | ||
|
|
||
| def format_message_history(response) | ||
| table = [] | ||
|
|
||
| response["messages"].each do |message| | ||
| if message["username"] | ||
| table.push({name: message["username"], text: message["text"]}) | ||
| else | ||
| found_user = PUMPKIN_SPICE.find_user(message["user"]) | ||
|
|
||
| modified_message = message["text"].gsub(/^<@\w+>/, "#{found_user.name}") | ||
|
|
||
| table.push({name: found_user.name, text: modified_message}) | ||
| end | ||
| end | ||
|
|
||
| table.reverse! | ||
|
|
||
| tp table, {:name=>{:width => 45}}, {:text=>{:width => 80}} | ||
| end | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| puts | ||
| # lists number of users and channels | ||
| print_workplace_stats() | ||
|
|
||
| again = true | ||
| while again | ||
| # lists menu options | ||
| print_menu() | ||
| print "What would you like to do? " | ||
| answer = gets.chomp.downcase | ||
|
|
||
| case answer | ||
| when "list users", "1", "one" | ||
| print_users | ||
|
|
||
| when "list channels", "2", "two" | ||
| print_channels | ||
|
|
||
| when "select user", "3", "three" | ||
| select_user | ||
|
|
||
| when "select channel", "4", "four" | ||
| select_channel | ||
|
|
||
| when "details", "5", "five" | ||
| print_details | ||
|
|
||
| when "send message", "6", "six" | ||
| get_message | ||
|
|
||
| when "get message history", "7", "seven" | ||
| if PUMPKIN_SPICE.selected | ||
| response = PUMPKIN_SPICE.selected.get_message_history | ||
| puts | ||
| format_message_history(response) | ||
| else | ||
| puts "\nYou need to select a recipient." | ||
| end | ||
|
|
||
| when "change bot settings", "8", "eight" | ||
| print "Please enter the bot's new name: " | ||
| name = gets.chomp | ||
| PUMPKIN_SPICE.bot_name = name | ||
| puts "The bot's new name is #{name}!" | ||
|
|
||
| print "Please enter the bot's new avatar: " | ||
| avatar = gets.chomp | ||
| PUMPKIN_SPICE.bot_avatar = avatar | ||
| puts "The bot's new avatar is #{avatar}!" | ||
|
|
||
| when "quit", "9", "nine", "exit" | ||
| PUMPKIN_SPICE.save_settings | ||
| again = false | ||
| end | ||
| end | ||
| puts "\nThank you for using the Ada Slack CLI" | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| module SlackCLI | ||
| class User < Recipient | ||
| attr_reader :real_name | ||
|
|
||
| def initialize(slack_id:, name:, real_name:) | ||
| super(slack_id, name) | ||
| @real_name = real_name | ||
| end | ||
|
|
||
| def get_message_history | ||
| im_url = "https://slack.com/api/im.list" | ||
| im_query = { token: ENV["SLACK_API_TOKEN"]} | ||
| im_response = HTTParty.get(im_url, query: im_query) | ||
|
|
||
| unless im_response.code == 200 && im_response.parsed_response["ok"] | ||
|
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. Good use of a custom exception. Consider using a begin/rescue in your CLI so that it doesn't break when this happens. |
||
| raise SlackAPIError, "Error when getting im list, error: #{im_response.parsed_response["error"]}" | ||
| end | ||
|
|
||
| ims = im_response["ims"] | ||
|
|
||
| direct_channel = ims.find do |im| | ||
| im["user"] == slack_id | ||
| end | ||
|
|
||
| direct_channel_id = direct_channel["id"] | ||
|
|
||
| url = "https://slack.com/api/conversations.history" | ||
| query = { token: ENV["SLACK_API_TOKEN"] , channel: direct_channel_id} | ||
| response = HTTParty.get(url, query: query) | ||
|
|
||
| unless response.code == 200 && response.parsed_response["ok"] | ||
| raise SlackAPIError, "Error when getting conversations history, error: #{response.parsed_response["error"]}" | ||
| end | ||
|
|
||
| return response | ||
| end | ||
|
|
||
| def self.all | ||
| users = [] | ||
|
|
||
| response = User.get("users.list") | ||
|
|
||
| response["members"].each do |member| | ||
| slack_id = member["id"] | ||
| name = member["name"] | ||
| real_name = member["real_name"] | ||
|
|
||
| users << SlackCLI:: User.new(slack_id: slack_id, name: name, real_name: real_name) | ||
| end | ||
| return users | ||
| end | ||
| 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.
Good on you to have this test!