Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require_relative "recipient"

class Channel < Recipient
attr_reader :topic, :member_count

def initialize(topic:, member_count:, slack_id:, name:)
super(slack_id: slack_id, name: name)
@topic = topic
@member_count = member_count
end

def details
tp self, "slack_id", "name", "topic", "member_count"
end

def self.list_all
response = Channel.get("https://slack.com/api/channels.list")
channels = []

response["channels"].each do |item|
channels << Channel.new(
name: item["name"],
slack_id: item["id"],
topic: item["topic"]["value"],
member_count: item["num_members"]
)
end
return channels
end
end

48 changes: 48 additions & 0 deletions lib/recipient.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

require 'httparty'
require 'dotenv'

Dotenv.load

class Recipient
attr_reader :slack_id, :name
def initialize(slack_id:, name:)
@slack_id = slack_id
@name = name
end

def self.list_all
raise NotImplementedError, 'Implement me in a child class'
end

def self.get(url)
response = HTTParty.get(url , query: {token: ENV["SLACK_TOKEN"]})

if response.code != 200 || response["ok"] == false
raise SlackAPIError, "We encaunterd a problem #{response["error"]}"
end
return response
end

def details
raise NotImplementedError, 'Implement me in a child class!'
end


def send_message(msg)
url = "https://slack.com/api/chat.postMessage"
query = {token: ENV["SLACK_TOKEN"], channel: self.slack_id, text: msg}

response = HTTParty.post(url, query: query)

if response.code != 200 || response["ok"] == false
raise SlackAPIError, "We encountered a problem: #{response["error"]}"
else
return true
end
end
end

class SlackAPIError < Exception
end

74 changes: 69 additions & 5 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,76 @@
#I got a lot of piece of code from Devin. Before i copy to my project I analize and i build test for.

#!/usr/bin/env ruby
require 'httparty'
require 'dotenv'
require 'table_print'
require_relative "workspace"

Dotenv.load

def main
puts "Welcome to the Ada Slack CLI!"
workspace = Workspace.new
workspace = WorkSpace.new
puts "\n"
puts "Welcome to the Ada Accountability Group 6 Slack CLI! This Slack workspace currently has #{workspace.users.count} users and #{workspace.channels.count} channels."

user_input = prompt_for_input

# TODO project
until user_input == "quit" || user_input == "exit"

case user_input
when "list users"
tp workspace.users, "slack_id", "name", "real_name"
puts "\n"

when "list channels"
tp workspace.channels, "name", "topic", "member_count", "slack_id"
puts "\n"

when "select user"
print "Please enter the user name or ID: "
search_term = gets.chomp.downcase
puts workspace.select_user(search_term)
puts "\n"

when "select channel"
print "Please enter the channel name or ID: "
search_term = gets.chomp.downcase
puts workspace.select_channel(search_term)
puts "\n"

when "details"
if workspace.selected == nil
puts "Please select a user or channel."
puts "\n"
else
workspace.show_details
user_input = nil
puts "\n"
end
when "send message"
if workspace.selected == nil
puts "Please select a user or channel."
puts "\n"
else
print "Please enter your message: "
msg = gets.chomp
workspace.send_message(msg)
puts "\n"
end
else
puts "Sorry, I didn't understand your request. Please try again."
puts "\n"
end

user_input = prompt_for_input
end
puts "Thank you for using the Ada Accountability Group 6 Slack CLI!"
puts "\n"
end

puts "Thank you for using the Ada Slack CLI"
def prompt_for_input
print "Please choose an option: \n1)list users,\n2)list channels,\n3)select user,\n4)select channel,\n5)details,\n6)send message,\n7)quit:"
return gets.chomp.downcase
end

main if __FILE__ == $PROGRAM_NAME
main if __FILE__ == $PROGRAM_NAME
38 changes: 38 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require_relative "recipient"
require 'httparty'
require 'dotenv'

Dotenv.load

class User < Recipient
attr_reader :real_name, :status_text, :status_emoji

def initialize(real_name:, status_text:, status_emoji:, name:, slack_id:)
super(slack_id: slack_id, name: name)
@real_name = real_name
@status_text = status_text
@status_emoji = status_emoji
end

def details
tp self, "slack_id", "name", "real_name"
end

def self.list_all
response = User.get("https://slack.com/api/users.list")
users = []

response["members"].each do |item|
users << User.new(
name: item["name"],
slack_id: item["id"],
real_name: item["real_name"],
status_text: item["profile"]["status_text"],
status_emoji: item["profile"]["status_emoji"]
)
end
return users
end
end


46 changes: 46 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require_relative "channel"
require_relative "user"

class WorkSpace
attr_reader :users, :channels, :selected

def initialize
@users = User.list_all
@channels = Channel.list_all
@selected = nil
end

def select_channel(search_term)

channels.each do |channel|
if channel.name == search_term || channel.slack_id.downcase == search_term
@selected = channel
return "Okay, #{selected.name} has been selected"
end
end

@selected = nil
return "Sorry, I couldn't find that channel."
end

def select_user(search_term)

users.each do |user|
if user.name == search_term || user.slack_id.downcase == search_term
@selected = user
return "Okay, #{selected.name} has been selected"
end
end
@selected = nil
return "Sorry, I couldn't find that user"
end

def show_details
@selected.details
end

def send_message(msg)
@selected.send_message(msg)
end
end

Loading