-
Notifications
You must be signed in to change notification settings - Fork 26
Leaves - Elizabeth & Nicky #5
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
3ef7e84
90a85d1
0431360
dc18ee0
2729dff
9170784
9fed5d3
6cc2355
00b51ce
0122cc4
dde4c3a
f48cc00
323fbd8
eabeada
723089a
3ad2e39
86b047e
e76db3c
79ae3eb
9ee31a6
364b6aa
dbcb504
3d019f4
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 |
|---|---|---|
|
|
@@ -54,3 +54,5 @@ build-iPhoneSimulator/ | |
|
|
||
| # Ignore cassette files | ||
| /specs/cassettes/ | ||
|
|
||
| .DS_Store | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| require "httparty" | ||
| require "dotenv" | ||
| require "awesome_print" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| require_relative "recipient" | ||
|
|
||
| module Slack | ||
| class Channel < Recipient | ||
| attr_reader :topic, :member_count | ||
|
|
||
| def initialize(name:, slack_id:, topic:, member_count:) | ||
| super(name: name, slack_id: slack_id) | ||
| @topic = topic | ||
| @member_count = member_count | ||
| end | ||
|
|
||
| def self.list | ||
| url = "https://slack.com/api/channels.list" | ||
| query_parameters = { | ||
| token: ENV["SLACK_TOKEN"], | ||
| } | ||
| response = Channel.get(url, query: query_parameters) | ||
|
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. We do not have the tools yet to test a bad get request in this instance, but it is good to think about and build in functionality to deal with bad requests. |
||
| channels_array = [] | ||
|
|
||
| response["channels"].each do |channel| | ||
| channel_hash = { "name" => channel["name"], "slack id" => channel["id"], "topic" => channel["topic"]["value"], "member count" => channel["members"].length } | ||
| channels_array << channel_hash | ||
| end | ||
| return channels_array | ||
| end | ||
|
|
||
| def details | ||
| return "\nname: #{name}, slack_id: #{slack_id}, topic: #{topic}, member count: #{member_count}" | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| require "httparty" | ||
| require "dotenv" | ||
| MESSAGE_URL = "https://slack.com/api/chat.postMessage" | ||
|
|
||
| module Slack | ||
| class Recipient | ||
| attr_reader :name, :slack_id | ||
|
|
||
| def initialize(name:, slack_id:) | ||
| @name = name | ||
| @slack_id = slack_id | ||
| end | ||
|
|
||
| def self.get(url, params) | ||
| return HTTParty.get(url, params) | ||
| end | ||
|
|
||
| def send_message(message) | ||
| body = { | ||
| token: ENV["SLACK_TOKEN"], | ||
| channel: slack_id, | ||
| text: message, | ||
| } | ||
|
|
||
| response = HTTParty.post(MESSAGE_URL, body: body) | ||
|
|
||
| if response["ok"] != true | ||
| raise SlackApiError.new("Invalid request. Error is #{response.code}: #{response.message}") | ||
| end | ||
| return response | ||
| end | ||
|
|
||
| def self.list | ||
| raise NotImplementedError, "Implement me in a child class!" | ||
| end | ||
|
|
||
| def details | ||
| raise NotImplementedError, "Implement me in a child class!" | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,75 @@ | ||
| #!/usr/bin/env ruby | ||
| require "httparty" | ||
| require "dotenv" | ||
| require "awesome_print" | ||
| require "table_print" | ||
| require_relative "channel" | ||
| require_relative "user" | ||
| require_relative "workspace" | ||
| require_relative "recipient" | ||
|
|
||
| # For specific SlackApiError messages (https://stackoverflow.com/questions/3382866/rubys-exception-error-classes) | ||
|
|
||
| def main | ||
| puts "Welcome to the Ada Slack CLI!" | ||
|
|
||
| # TODO project | ||
| workspace = Slack::Workspace.new | ||
|
|
||
| input = "" | ||
| until input == "quit" | ||
| puts "\nPlease select an option: | ||
|
|
||
| -list users | ||
| -list channels | ||
| -select user | ||
| -select channel | ||
| -show details | ||
| -send message | ||
| -quit\n" | ||
| input = gets.chomp.downcase | ||
|
|
||
| puts "Thank you for using the Ada Slack CLI" | ||
| case input | ||
| when "list users" | ||
| user_array = Slack::User.list | ||
| tp user_array | ||
| when "list channels" | ||
| channel_array = Slack::Channel.list | ||
| tp channel_array | ||
| when "select user" | ||
| puts "\n[Enter a username or slack id]" | ||
| identifier = gets.chomp | ||
| begin | ||
| workspace.select_user(identifier) | ||
| rescue SlackApiError => e | ||
| puts "\n#{e.message}" | ||
| end | ||
| when "select channel" | ||
| puts "\n[Enter a name or slack id]" | ||
| identifier = gets.chomp | ||
| begin | ||
| workspace.select_channel(identifier) | ||
| rescue SlackApiError => e | ||
| puts "\n#{e.message}" | ||
| end | ||
| when "show details" | ||
| if workspace.show_details == nil | ||
| puts "\n[No user or channel has been selected]" | ||
| else | ||
| puts "#{workspace.show_details}" | ||
| end | ||
| when "send message" | ||
| if workspace.selected == nil | ||
| puts "\n[No user or channel has been selected]" | ||
| else | ||
| puts "What do you want to send?" | ||
| message = gets.chomp | ||
| workspace.send_message(message) | ||
| end | ||
| when "quit" | ||
| puts "[Thank you for using the Ada Slack CLI]" | ||
| exit | ||
| end | ||
| end | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| main if __FILE__ == $PROGRAM_NAME |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| require "httparty" | ||
| require "dotenv" | ||
| require "awesome_print" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| require_relative "recipient" | ||
|
|
||
| module Slack | ||
| class User < Recipient | ||
| attr_reader :name, :real_name, :slack_id | ||
|
|
||
| def initialize(name:, slack_id:, real_name:) | ||
| super(name: name, slack_id: slack_id) | ||
| @real_name = real_name | ||
| end | ||
|
|
||
| def self.list | ||
| url = "https://slack.com/api/users.list" | ||
| query_parameters = { | ||
| token: ENV["SLACK_TOKEN"], | ||
| } | ||
| response = User.get(url, query: query_parameters) | ||
| users_array = [] | ||
| response["members"].each do |member| | ||
| user_hash = { "real name" => member["real_name"], "user name" => member["name"], "slack id" => member["id"] } | ||
| users_array << user_hash | ||
| end | ||
| return users_array | ||
| end | ||
|
|
||
| def details | ||
| return "\nreal name: #{real_name}, user name: #{name}, slack_id: #{slack_id}" | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| require "httparty" | ||
| require "dotenv" | ||
| require "awesome_print" | ||
|
|
||
| Dotenv.load | ||
|
|
||
| url = "https://slack.com/api/channels.list" | ||
| query_parameters = { | ||
| token: ENV["SLACK_TOKEN"], | ||
| } | ||
|
|
||
| # channel_names = | ||
| # puts query_parameters | ||
| response = HTTParty.get(url, query: query_parameters) | ||
|
|
||
| # Loops through the results and prints name of each channel | ||
| # response["channels"].each do |channel| | ||
| # ap channel["name"] | ||
| # end | ||
| # ap response["channels"][1]["name"] | ||
|
|
||
| # response["channels"].each do |channel| | ||
| # ap channel["members"].length | ||
| # end | ||
|
|
||
| response["channels"].each do |channel| | ||
| ap channel | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| require_relative "user" | ||
| require_relative "channel" | ||
| require_relative "recipient" | ||
|
|
||
| class SlackApiError < StandardError; end | ||
|
|
||
| module Slack | ||
| class Workspace | ||
| attr_reader :users, :channels, :selected | ||
|
|
||
| def initialize | ||
| @users = Slack::User.list.map do |user_obj| | ||
| Slack::User.new(name: user_obj["user name"], slack_id: user_obj["slack id"], real_name: user_obj["real name"]) | ||
| end | ||
| @channels = Slack::Channel.list.map do |chan_obj| | ||
| Slack::Channel.new(name: chan_obj["name"], slack_id: chan_obj["slack id"], topic: chan_obj["topic"], member_count: chan_obj["member count"]) | ||
| end | ||
| @selected = nil | ||
| end | ||
|
|
||
| def select_channel(identifier) | ||
|
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 code is very similar to the code for |
||
| @channels.each do |channel| | ||
| if [channel.name, channel.slack_id].include?(identifier) | ||
| @selected = channel | ||
| return @selected | ||
| end | ||
| end | ||
| raise SlackApiError.new("[We couldn't find this channel!]") | ||
| end | ||
|
|
||
| def select_user(identifier) | ||
| @users.each do |user| | ||
| if [user.name, user.slack_id].include?(identifier) | ||
| @selected = user | ||
| return @selected | ||
| end | ||
| end | ||
| raise SlackApiError.new("[We couldn't find this user!]") | ||
| end | ||
|
|
||
| def show_details | ||
| if @selected | ||
| @selected.details | ||
| end | ||
| end | ||
|
|
||
| def send_message(message) | ||
| if @selected | ||
| @selected.send_message(message) | ||
| else | ||
| return nil | ||
| end | ||
| 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.
.envshould be in.gitignore.