-
Notifications
You must be signed in to change notification settings - Fork 27
Ports - Kate - Myriam #21
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
9db497c
b333f46
1abc147
4845c96
05a4eeb
76c2517
6c737b5
47195a3
96ae0ce
02445df
1a48bc2
7e652fb
0125360
3c795fa
bb00d2d
d5493b2
44feabb
acdd5df
1ac7513
122ac43
22ff3a4
18a2447
43f095d
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,66 @@ | ||
| require "httparty" | ||
| require "dotenv" | ||
| require_relative "recipient" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class Channel < Recipient | ||
| URL = "https://slack.com/api/channels.list".freeze | ||
| CHAT_URL = "https://slack.com/api/chat.postMessage".freeze | ||
| KEY = ENV["SLACK_API_TOKEN"] | ||
|
|
||
| attr_reader :topic, :members | ||
|
|
||
| def initialize(id, name, topic, members) | ||
| super(id, name) | ||
| @topic = topic | ||
| @members = members | ||
| end | ||
|
|
||
| def self.get(url, param) | ||
| query = { token: param } | ||
| response = HTTParty.get(url, query: query) | ||
| response | ||
| end | ||
|
|
||
| def self.list(url, param) | ||
| response = get(url, param) | ||
| raise StandardError, "Error #{response.code} : #{response['message']}" if response["ok"].nil? | ||
|
|
||
| formatted_list = [] | ||
| channels = response["channels"].each do |channel| | ||
| slack_id = channel["id"] | ||
| channel_name = channel["name"] | ||
| topic = channel["topic"]["value"] | ||
| members = channel["members"].length | ||
| formatted_list << new(slack_id, channel_name, topic, members) | ||
| end | ||
| formatted_list | ||
| end | ||
|
|
||
| def details | ||
|
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. Just a note this method could also be inherited from |
||
| info_string = "\nSlack ID : #{id}" + | ||
| "\nChannel name : #{name}" + | ||
| "\nTopic : #{topic}" + | ||
| "\nMember count: #{members}" | ||
| info_string | ||
| end | ||
|
|
||
| def self.message(text, channel) | ||
| response = HTTParty.post("#{CHAT_URL}", | ||
| headers: { "Content-Type" => "application/x-www-form-urlencoded" }, | ||
| body: { | ||
| token: KEY, | ||
| channel: channel, | ||
| text: text | ||
| } | ||
| ) | ||
| if response["ok"] | ||
| return true | ||
| else | ||
| raise ArgumentError, "Error when sending message to #{channel}." | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| module SlackCLI | ||
| class Recipient | ||
| CHAT_URL = "https://slack.com/api/chat.postMessage".freeze | ||
| attr_reader :id, :name | ||
|
|
||
| def initialize(id, name) | ||
| @name = name | ||
| @id = id | ||
| end | ||
|
|
||
| def self.get(url, params) | ||
| HTTParty.get(url, query: params) | ||
| end | ||
|
|
||
| def send_message(message) | ||
|
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. Notice how this method is identical in Channel? |
||
| query_parameters = { | ||
| token: ENV["SLACK_API_TOKEN"], | ||
| channel: id, | ||
| text: message | ||
| } | ||
| response = HTTParty.post( | ||
| CHAT_URL, | ||
| body: query_parameters, | ||
| headers: { "Content-Type" => "application/x-www-form-urlencoded" } | ||
| ) | ||
|
|
||
| response.code == 200 && response["ok"] | ||
| end | ||
|
|
||
| def display_details | ||
|
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. You call it |
||
| raise NotImplementedError, "Implement me from a child class!" | ||
| end | ||
|
|
||
| def self.get | ||
|
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.
|
||
| raise NotImplementedError, "Implement me from a child class!" | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,136 @@ | ||
| #!/usr/bin/env ruby | ||
| require_relative "workspace" | ||
| require_relative "user" | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| module SlackCLI | ||
| class Main | ||
| def self.main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
| option = 0 | ||
| until option == 5 | ||
| puts "\n\n---Options---\n\n" | ||
| puts " 1. List Users" | ||
| puts " 2. List Channels" | ||
| puts " 3. Select User" | ||
| puts " 4. Select Channels" | ||
| puts " 5. Quit" | ||
| puts "\nPlease, choose one of the options available" | ||
| option = gets.chomp.to_i | ||
| options(option) if option != 5 | ||
| end | ||
| puts "Thank you for using the Ada Slack CLI\n\n" | ||
| exit | ||
| end | ||
|
|
||
| # TODO project | ||
| def self.options(option) | ||
| if option == 1 | ||
| list_users | ||
| elsif option == 2 | ||
| list_channels | ||
| elsif option == 3 | ||
| select_user | ||
| elsif option == 4 | ||
| select_channel | ||
| elsif option == 5 | ||
| exit | ||
| end | ||
| end | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
| def self.list_users | ||
| workspace = Workspace.new | ||
| users_list = workspace.users | ||
| puts "\n\n----List of Slack Users----\n\n" | ||
| users_list.each_with_index do |user, index| | ||
| puts "User N.#{index + 1}" | ||
| puts " Name: #{user.name}" | ||
| puts " Real Name:#{user.real_name}" | ||
| puts " ID: #{user.id}\n\n" | ||
| end | ||
| puts "Press any key to continue...\n\n" | ||
|
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. This should be, "Press Enter to continue" |
||
| gets.chomp | ||
| end | ||
|
|
||
| def self.list_channels | ||
| workspace = Workspace.new | ||
| channel_list = workspace.channels | ||
| puts "\n\n----List of Slack Channels----\n\n" | ||
| channel_list.each_with_index do |channel, index| | ||
| print "Channel N.#{index + 1}" | ||
| puts channel.details + "\n\n" | ||
| end | ||
| puts "Press any key to continue...\n\n" | ||
| gets.chomp | ||
| end | ||
|
|
||
| def self.select_user | ||
| puts "\n-> Type the id or name of the user " ##### | ||
| user = gets.chomp | ||
| puts "\n\n---Recipient Options---\n\n" | ||
| puts " 1. See Details" | ||
| puts " 2. Send Message" | ||
| puts "\nPlease, choose one of the options available" | ||
| recipient_option = gets.chomp.to_i | ||
| if recipient_option == 1 | ||
| users_details(user) | ||
| elsif recipient_option == 2 | ||
| puts "\n\n-> Type away..." | ||
| text = gets.chomp | ||
| user_message(text, user) | ||
| end | ||
| end | ||
|
|
||
| def self.select_channel | ||
| puts "\n-> Type the id or name of the channel " | ||
| channel = gets.chomp | ||
| puts "\n\n---Channel Options---\n\n" | ||
| puts " 1. See Details" | ||
| puts " 2. Send Message" | ||
| puts "\nPlease, choose one of the options available" | ||
| recipient_option = gets.chomp.to_i | ||
| if recipient_option == 1 | ||
| puts Workspace.new.channel_details(channel) | ||
| elsif recipient_option == 2 | ||
| puts "\n\n-> Type away..." | ||
| text = gets.chomp | ||
| channel_message(text, channel) | ||
| end | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| def self.users_details(user) | ||
| begin | ||
| details = Workspace.users_details(user) | ||
| puts details | ||
| rescue StandardError | ||
| puts "\n\nThe user does not exist!" | ||
| end | ||
| puts "Press any key to continue...\n\n" | ||
| gets.chomp | ||
| end | ||
|
|
||
| def self.user_message(text, user) | ||
| begin | ||
| status = Workspace.user_message(text, user) | ||
| puts "\n\n---Message sucessfully sent!---" if status | ||
| rescue StandardError | ||
| puts "\n\nOops, something went wrong..." | ||
| puts "To send a message you must provide the ID (while we improve this feature)" | ||
| puts "Also, make sure you're choosing a valid user." | ||
| end | ||
| puts "Press any key to continue...\n\n" | ||
| gets.chomp | ||
| end | ||
|
|
||
| def self.channel_message(text, channel) | ||
| begin | ||
| status = Workspace.channel_message(text, channel) | ||
| puts "\n\n---Message sucessfully sent!---" if status | ||
| rescue StandardError | ||
| puts "\n\nOops, something went wrong..." | ||
| puts "Also, make sure you're choosing a valid channel." | ||
| end | ||
| puts "Press any key to continue...\n\n" | ||
| gets.chomp | ||
| end | ||
| end | ||
| end | ||
| SlackCLI::Main.main if $PROGRAM_NAME == __FILE__ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| require_relative "workspace" | ||
| require "httparty" | ||
| require "dotenv" | ||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| USER_URL_MESSAGE = "https://slack.com/api/".freeze | ||
| API_KEY = ENV["SLACK_API_TOKEN"] | ||
|
|
||
| class User | ||
| attr_reader :name, :real_name, :id | ||
|
|
||
| def initialize(name, real_name, id) | ||
| @name = name | ||
| @id = id | ||
| @real_name = real_name | ||
| end | ||
|
|
||
| def self.get(url, param) | ||
| query = { token: param } | ||
| response = HTTParty.get(url, query: query) | ||
| response | ||
| end | ||
|
|
||
| def self.list(url, param) | ||
| response = get(url, param) | ||
| members = response["members"] | ||
| formatted_list = [] | ||
| members.each do |member| | ||
| name = member["name"] | ||
| real_name = member["real_name"] | ||
| id = member["id"] | ||
| formatted_list << new(name, real_name, id) | ||
| end | ||
| formatted_list | ||
| end | ||
|
|
||
| def self.details(user, url, param) | ||
|
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. This method doesn't make sense as a class method since you want the details of a specific instance of |
||
| formatted_list = list(url, param) | ||
| details_user = "" | ||
| formatted_list.each do |member| | ||
| next unless member.name == user || member.id == user | ||
|
|
||
| details_user = "\n\nUSER'S DETAILS: | ||
| Name: #{member.name} | ||
| Real name: #{member.real_name} | ||
| Id: #{member.id}" | ||
| return details_user | ||
| end | ||
| raise ArgumentError, "The user you are trying to find does not exist" | ||
| end | ||
|
|
||
| def self.message(text, user) | ||
|
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. Why not take advantage of the |
||
| response = HTTParty.post( | ||
| "#{USER_URL_MESSAGE}chat.postMessage", | ||
| headers: { "Content-Type" => "application/x-www-form-urlencoded" }, | ||
| body: { | ||
| token: API_KEY, | ||
| channel: user, | ||
| text: text | ||
| } | ||
| ) | ||
| if response["ok"] | ||
| return true | ||
| else | ||
| raise ArgumentError, "Error when sending message to #{user}." | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| require_relative "channel" | ||
| require "httparty" | ||
| require "dotenv" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| module SlackCLI | ||
| class Workspace | ||
| attr_reader :users, :channels | ||
| USER_API = "https://slack.com/api/users.list".freeze | ||
| CHANNEL_API = "https://slack.com/api/channels.list".freeze | ||
| KEY = ENV["SLACK_API_TOKEN"] | ||
|
|
||
| def initialize | ||
| @users = SlackCLI::User.list(USER_API, KEY) | ||
| @channels = SlackCLI::Channel.list(CHANNEL_API, KEY) | ||
| end | ||
|
|
||
| def self.users_details(user) | ||
|
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. These methods don't make sense a class methods since each Workspace has a list of users it could also keep track of the currently selected user and this method as an instance method could just return |
||
| SlackCLI::User.details(user, USER_API, KEY) | ||
| end | ||
|
|
||
| def self.user_message(text, user) | ||
| SlackCLI::User.message(text, user) | ||
| end | ||
|
|
||
| def self.channel_message(text, channel) | ||
| SlackCLI::Channel.message(text, channel) | ||
| end | ||
|
|
||
| def channel_details(channel) | ||
| channel_found = @channels.find do |channel_object| | ||
| channel_object.name == channel || channel_object.id == channel | ||
| end | ||
| return "Channel not found" if !channel_found | ||
| channel_found.details | ||
| 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.
This method could be inherited from
Recipient.