-
Notifications
You must be signed in to change notification settings - Fork 26
Leaves-Bri & Yasmin #25
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
844f7cc
36f83be
fb95aff
afa19e5
10ba16b
f70e150
5537f71
e6741cb
6ef2979
f4ee658
9cdf5f4
ae5e8a9
c044b57
564875a
4d49679
38a00ad
18ba315
11ecd2e
f5300ff
c89eb26
bdc4aa4
84ca629
8647477
e535650
472368b
44677bd
976c2a6
652bbbb
4011d11
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_relative 'recipient' | ||
| require 'dotenv' | ||
| Dotenv.load | ||
|
|
||
| module Slack | ||
| class Channel | ||
| attr_reader :channel_name, :topic, :member_count, :slack_id | ||
| CHANNELS_LIST = "https://slack.com/api/channels.list" | ||
| CHAT_URL = "https://slack.com/api/chat.postMessage" | ||
|
|
||
| def initialize(channel_name, topic, member_count, slack_id) | ||
| @channel_name = channel_name | ||
| @topic = topic | ||
| @member_count = member_count | ||
| @slack_id = slack_id | ||
| end | ||
|
|
||
| def self.list_channels | ||
| query = { | ||
| token: ENV["SLACK_API_TOKEN"] | ||
| } | ||
| response = HTTParty.get(CHANNELS_LIST, query: query) | ||
|
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 is a key place where a parent class that has a self.get(url, params) method could help you make sure this functionality works the same in these 2 classes. |
||
| channels = response["channels"] | ||
| channels_list = [] | ||
| channels.each do |channel_hash| | ||
| channel_name = channel_hash["name"] | ||
| topic = channel_hash["topic"] | ||
| member_count = channel_hash["num_members"] | ||
| slack_id = channel_hash["id"] | ||
| channels_list << Channel.new(channel_name, topic, member_count, slack_id) | ||
| end | ||
| return channels_list | ||
| end | ||
|
|
||
| def details | ||
| "The channel name is #{channel_name} and the slack id is #{slack_id}" | ||
| end | ||
|
|
||
| # method for select channel, returns an instance of channel | ||
| # input is a string | ||
| def self.select_channel(recipient_selection) | ||
| channels = list_channels | ||
| recipient = channels.find {| channel | channel.channel_name == recipient_selection || channel.slack_id == recipient_selection } | ||
| return recipient | ||
| end | ||
|
|
||
| # method for send_message - just like in User.rb | ||
| 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. You should raise an exception here if the post request is not successful. It is best practice to raise a custom exception such as |
||
| response = HTTParty.post( | ||
| CHAT_URL, | ||
| body: { | ||
| token: ENV["SLACK_API_TOKEN"], | ||
| text: message, | ||
| channel: @slack_id # need slack_id, per Slack documentation for send_message | ||
| }, | ||
| ) | ||
|
|
||
| return response.code == 200 && response.parsed_response["ok"] | ||
| end | ||
| end | ||
| end | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #!/usr/bin/env ruby | ||
| require 'httparty' | ||
| require 'dotenv' | ||
| Dotenv.load('../.env') | ||
| require 'awesome_print' | ||
|
|
||
| CHAT_URL = "https://slack.com/api/chat.postMessage" | ||
|
|
||
| module Slack | ||
| class Recipient | ||
|
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 you mention in your comprehension questions. You could make good use of inheritance by having User and Channel inherit from Recipient. |
||
| attr_reader :slack_id, :name | ||
|
|
||
| end | ||
| end | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,81 @@ | ||
| #!/usr/bin/env ruby | ||
| require 'httparty' | ||
| require 'dotenv' | ||
| require 'awesome_print' | ||
| require_relative './user' | ||
| require_relative './channel' | ||
| Dotenv.load('../.env') | ||
|
|
||
| KEY = ENV['KEY'] | ||
|
|
||
| def main | ||
| recipient = nil | ||
| users = Slack::User.list_users | ||
| channels = Slack::Channel.list_channels | ||
|
|
||
| # Wave 1 | ||
| puts "Welcome to the Ada Slack CLI!" | ||
|
|
||
| # TODO project | ||
|
|
||
| puts "There are #{users.length} users and #{channels.length} channels." | ||
|
|
||
| # Loop | ||
| while true | ||
| puts "Would you like to view: List Users, List Channels, Select User, Select Channel, See Details, Send Message, or Quit?" | ||
| user_input = gets.chomp.split.map(&:capitalize).join(' ') | ||
| case user_input | ||
| when "List Users" | ||
| users.each do |user| | ||
| puts "User count: #{users.length}, User Name: #{user.user_name}, Real name: #{user.name}, and Slack ID: #{user.slack_id}." | ||
| end | ||
| when "List Channels" | ||
| channels.each do |channel| | ||
| puts "Channel's name: #{channel.channel_name}, Member Count: #{channel.member_count}, and Slack ID: #{channel.slack_id}." | ||
| end | ||
|
|
||
| # Wave 2: Selection Method | ||
| when "Select User" | ||
| puts "Please enter a username or slack id to continue." | ||
| recipient_selection = gets.chomp | ||
| recipient = Slack::User.select_user(recipient_selection) | ||
| if recipient == nil | ||
| puts "No username or slack id found. Returning you to main menu." | ||
| end | ||
|
|
||
| when "Select Channel" | ||
| puts "Please enter a channel name or slack id to continue." | ||
| recipient_selection = gets.chomp | ||
| recipient = Slack::Channel.select_channel(recipient_selection) | ||
| if recipient == nil | ||
| puts "No channel name or slack id found. Returning you to main menu." | ||
| end | ||
|
|
||
| when "See Details" | ||
| if recipient == nil # if no recipient is selected, no details can be shown | ||
| puts "No recipient currently selected. Returning you to main menu." | ||
| else | ||
| puts recipient.details # if recipient selected, show details | ||
| end | ||
|
|
||
| # Wave 3: Send Message to selected user. Send_message method works for User and Channel | ||
| when "Send Message" | ||
| puts "Please enter your new message now." | ||
| message = gets.chomp | ||
| if recipient == nil | ||
| puts "No recipient currently selected. Returning you to main menu." | ||
| else | ||
| recipient.send_message(message) | ||
| end | ||
|
|
||
| when "Quit" | ||
| puts "Exiting program" | ||
| quit | ||
| else | ||
| puts "Incorrect input." | ||
| end | ||
| end | ||
|
|
||
| # Program ends and exits | ||
| puts "Thank you for using the Ada Slack CLI" | ||
| end | ||
|
|
||
| main if __FILE__ == $PROGRAM_NAME | ||
| main if __FILE__ == $PROGRAM_NAME | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| require_relative 'recipient' | ||
| require 'dotenv' | ||
| Dotenv.load | ||
|
|
||
| module Slack | ||
| class User | ||
| attr_reader :user_name, :slack_id, :name | ||
|
|
||
| USERS_LIST = "https://slack.com/api/users.list" | ||
| CHAT_URL = "https://slack.com/api/chat.postMessage" | ||
|
|
||
| def initialize(slack_id, name, user_name) | ||
| @slack_id = slack_id | ||
| @name = name | ||
| @user_name = user_name | ||
| end | ||
|
|
||
| def self.list_users | ||
| query = { | ||
| token: ENV["SLACK_API_TOKEN"] | ||
| } | ||
| response = HTTParty.get(USERS_LIST, query: query) | ||
| members = response["members"] | ||
|
|
||
| users = [] | ||
| members.each do |user_hash| | ||
| slack_id = user_hash["id"] | ||
| name = user_hash["real_name"] | ||
| user_name = user_hash["name"] | ||
| users << User.new(user_name, name, slack_id) | ||
| end | ||
| return users | ||
| end | ||
|
|
||
| def details | ||
| "The name is #{@name}, the user name is #{@user_name}, and the slack id is #{@slack_id}" | ||
| end | ||
|
|
||
| def self.select_user(recipient_selection) | ||
| users = list_users | ||
| recipient = users.find { |user| user.user_name == recipient_selection || user.slack_id == recipient_selection } | ||
| return recipient | ||
| end | ||
|
|
||
| def send_message(message) | ||
| response = HTTParty.post( | ||
| CHAT_URL, | ||
| body: { | ||
| token: ENV["SLACK_API_TOKEN"], | ||
| text: message, | ||
| channel: @user_name, | ||
| as_user: true # allows you to pass in a user_name | ||
| }, | ||
| ) | ||
|
|
||
| return response.code == 200 && response.parsed_response["ok"] | ||
| end | ||
| 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 should be
coverage/