-
Notifications
You must be signed in to change notification settings - Fork 26
Branches - Sara + Monick #7
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
6593e4b
77a88ac
e9c203a
9f1b91a
cb6bc93
29649d7
c3365d1
87ac6ef
1a5e09e
a0adb7e
62397bf
f5571b5
513a01a
6337b09
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,46 @@ | ||
| require 'dotenv' | ||
| require 'httparty' | ||
| require 'awesome_print' | ||
| Dotenv.load | ||
|
|
||
| require_relative 'recipient' | ||
|
|
||
| module SlackCli | ||
| class Channel < Recipient | ||
| attr_reader :name, :topic, :id, :num_members | ||
|
|
||
| def initialize(name:, id:, topic:, num_members:) | ||
| @name = name | ||
| @id = id | ||
| @topic = topic | ||
| @num_members = num_members | ||
| end | ||
|
|
||
| def self.list | ||
| method_url = "https://slack.com/api/channels.list" | ||
| query_params = { | ||
| token: ENV["SLACK_TOKEN"] | ||
| } | ||
| response = HTTParty.get(method_url, query: query_params) | ||
|
|
||
| channels = response.parsed_response["channels"] | ||
| i = 0 | ||
| all_channels = [] | ||
| channels.each do |channel| | ||
|
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. Minor suggestion for refactoring: instead of using |
||
| channel_hash = {} | ||
| name = response.parsed_response["channels"][i]["name"] | ||
| topic = response.parsed_response["channels"][i]["topic"]["value"] | ||
| id = response.parsed_response["channels"][i]["id"] | ||
| num_members = response.parsed_response["channels"][i]["num_members"] | ||
| channel_hash[:name] = name | ||
| channel_hash[:topic] = topic | ||
| channel_hash[:id] = id | ||
| channel_hash[:num_members] = num_members | ||
| all_channels.push(channel_hash) | ||
| i += 1 | ||
| end | ||
| return all_channels | ||
| end | ||
|
|
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| require 'dotenv' | ||
| require 'httparty' | ||
| require 'awesome_print' | ||
| Dotenv.load | ||
|
|
||
| module SlackCli | ||
| class Recipient | ||
| def self.send_message(recipient:, message:) | ||
| method_url = "https://slack.com/api/chat.postMessage" | ||
| query_params = { | ||
| token: ENV["SLACK_TOKEN"], | ||
| channel: recipient, | ||
| text: message | ||
| } | ||
| slack_post = HTTParty.post(method_url, query: query_params) | ||
|
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. Something to think about for the future: What happens if this POST request comes back with a response that has an error? Your code currently doesn't handle that! |
||
| return slack_post | ||
| end | ||
| end | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,86 @@ | ||
| #!/usr/bin/env ruby | ||
| require 'dotenv' | ||
| require 'httparty' | ||
| require 'awesome_print' | ||
| Dotenv.load | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| require_relative './channel' | ||
| require_relative 'user' | ||
|
|
||
| # TODO project | ||
| def start_program | ||
| ap "Welcome to the Ada Slack CLI!" | ||
| print "What would you like to do? Type one of the following:\n - list channels\n - list users\n - select user\n - select channel\n - details\n - send message\n - quit\n" | ||
| user_input = gets.chomp.downcase | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| while user_input != "quit" | ||
| case user_input | ||
| when "list channels" | ||
| ap SlackCli::Channel.list | ||
| when "list users" | ||
| ap SlackCli::User.list | ||
| when "select user" | ||
| ap "Type a username or Slack ID." | ||
| search_user = gets.chomp | ||
| selected_recipient = SlackCli::User.list.find do |i| | ||
|
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. Minor suggestion: Instead of the variable name |
||
| i[:user_name]== search_user || i[:id] == search_user | ||
| end | ||
| if selected_recipient == nil | ||
|
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. Minor suggestion: the code |
||
| ap "User not found" | ||
| else | ||
| selected_recipient = SlackCli::User.new( | ||
| name: selected_recipient[:user_name], | ||
| real_name: selected_recipient[:real_name], | ||
| id: selected_recipient[:id] | ||
| ) | ||
| ap "Recipient has been selected." | ||
| end | ||
|
|
||
| when "select channel" | ||
| ap "Type a username or Slack ID." | ||
| search_channel = gets.chomp | ||
| selected_recipient = SlackCli::Channel.list.find do |i| | ||
| i[:name]== search_channel || i[:id] == search_channel | ||
| end | ||
| if selected_recipient == nil | ||
| ap "Channel not found" | ||
| else | ||
| selected_recipient = SlackCli::Channel.new( | ||
| name: selected_recipient[:name], | ||
| topic: selected_recipient[:topic], | ||
| id: selected_recipient[:id], | ||
| num_members: selected_recipient[:num_members] | ||
| ) | ||
| ap "Recipient has been selected." | ||
| end | ||
|
|
||
| when "details" | ||
| if selected_recipient.class == SlackCli::Channel | ||
| ap "Channel name: #{selected_recipient.name}" | ||
| ap "Channel topic: #{selected_recipient.topic}" | ||
| ap "Channel id: #{selected_recipient.id}" | ||
| ap "Channel members count: #{selected_recipient.num_members}" | ||
| elsif selected_recipient.class == SlackCli::User | ||
| ap "Username: #{selected_recipient.name}" | ||
| ap "User real name: #{selected_recipient.real_name}" | ||
| ap "User id: #{selected_recipient.id}" | ||
| else | ||
| ap "No recipient is currently selected. Select a recipient in the main menu." | ||
| end | ||
| ap selected_recipient | ||
| when "send message" | ||
| if selected_recipient == nil | ||
| ap "No recipient selected." | ||
| else | ||
| ap "Type a message to send:" | ||
| message = gets.chomp | ||
| SlackCli::Recipient.send_message(recipient:selected_recipient.id, message:message) | ||
| end | ||
| else | ||
| ap "Not a valid command" | ||
| end | ||
| print "What would you like to do? Type one of the following:\n - list channels\n - list users\n - select user\n - select channel\n - details\n - send message\n - quit\n" | ||
| user_input = gets.chomp.downcase | ||
| end | ||
| ap "Thank you for using the Sara & Monick Slack CLI." | ||
| end | ||
| start_program | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| require 'dotenv' | ||
| require 'httparty' | ||
| require 'awesome_print' | ||
| Dotenv.load | ||
|
|
||
| require_relative 'recipient' | ||
|
|
||
| module SlackCli | ||
| class User < Recipient | ||
| attr_reader :name, :real_name, :id | ||
|
|
||
| def initialize(name:, real_name:, id:) | ||
| @real_name = real_name | ||
| @name = name | ||
| @id = id | ||
| end | ||
|
|
||
| def self.list | ||
| method_url = "https://slack.com/api/users.list" | ||
| query_params = { | ||
| token: ENV["SLACK_TOKEN"] | ||
| } | ||
| response = HTTParty.get(method_url, query: query_params) | ||
|
|
||
| users = response.parsed_response["members"] | ||
| i = 0 | ||
| all_users = [] | ||
| users.each do |user| | ||
| user_hash = {} | ||
| user_name = response.parsed_response["members"][i]["name"] | ||
| real_name = response.parsed_response["members"][i]["real_name"] | ||
| id = response.parsed_response["members"][i]["id"] | ||
| user_hash[:user_name] = user_name | ||
| user_hash[:real_name] = real_name | ||
| user_hash[:id] = id | ||
| all_users.push(user_hash) | ||
| i += 1 | ||
| end | ||
| return all_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.
Minor suggestion: Could this be a constant variable?