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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

# Ignore environemnt variables
.env
cassete/*.yml
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
39 changes: 39 additions & 0 deletions lib/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative 'recipient'
require 'httparty'
require 'dotenv'

Dotenv.load

module SlackCli

class Channel < Recipient

attr_reader :slack_id, :name, :topic, :member_count

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

def details
return [{"slack_id" => slack_id,"name" => name,"topic" => topic, "member_count" => member_count }]
end

def self.create_a_channel(channel_name)
url = "https://slack.com/api/channels.create"
param = {token: ENV["SLACK_BOT_TOKEN"],name: channel_name}
new_channel = HTTParty.post(url, query: param)
return new_channel.code
end

def self.list_all
url = "https://slack.com/api/conversations.list"
param = {token: ENV["SLACK_BOT_TOKEN"]}
result = self.get(url, param)
channel_list = result["channels"].map { |channel| SlackCli::Channel.new(slack_id: channel["id"], name: channel["name"], topic: channel["topic"]["value"], member_count: channel["num_members"]) }
return channel_list
end

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

module SlackCli

class Recipient

attr_accessor :slack_id, :name

def initialize (slack_id, name)
@slack_id = slack_id
@name = name
end

def send_message(message)
url = "https://slack.com/api/chat.postMessage"
recipient = self.slack_id
token = ENV["SLACK_BOT_TOKEN"]
message = HTTParty.post(url, query: {token: token, channel: recipient, text: message})
return message["ok"] && message.code
end

def self.get(url, params)
return HTTParty.get(url, query: params)
end

def self.list_all
raise SlackAPIError, "implement in user or channel"
end

def details
raise SlackAPIError, "implement in user or channel"
end

end


class SlackAPIError < StandardError
end


end

74 changes: 70 additions & 4 deletions lib/slack.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,78 @@
#!/usr/bin/env ruby
require 'httparty'
require 'dotenv'
require_relative 'workspace'
require 'table_print'

Dotenv.load

def main
workspace = SlackCli::Workspace.new(users: SlackCli::User.list_all,channels: SlackCli::Channel.list_all,selected: nil)
puts "Welcome to the Ada Slack CLI!"
workspace = Workspace.new

# TODO project
option = ' '
while option != "quit"
puts "What would you like to do?"
puts "options: list channels? list users? select user? select channel? list details? send message? quit?"
option = gets.chomp
unless option == "list channels" || option == "list users" || option == "select user" || option == "select channel" || option == "list details" || option == "send message" || option == "quit"
puts "wrong input! try again!"
puts "What would you like to do next?"
puts "options: list channels? list users? select user? select channel? list details? send message? quit?"
option = gets.chomp
end

puts "Thank you for using the Ada Slack CLI"
case option
when "list channels"
puts ''
tp workspace.channels.map{|channel| channel.details}, "slack_id", "name", "topic", "member_count"
puts ''
when "list users"
tp workspace.users.map{|user| user.details}, "slack_id", "name", "real_name", "status_text" ,"status_emoji"
when "select user"
workspace.select_user
when "select channel"
workspace.select_channel
when "send message"
workspace.send_message
when "list details"
if workspace.selected == nil || workspace.selected == ''
puts "no previously selected user or channel!"
puts "would you like to select a user or a channel? q to return to previous menu!"
selection = gets.chomp
if
selection == "q"
main
elsif
selection == "user"
workspace.select_user
puts "here are the details of user #{workspace.selected.name}"
workspace.show_details
elsif selection == "channel"
workspace.select_channel
puts "here are the details of channel #{workspace.selected.name}"
workspace.show_details
else
puts "wrong input! try again"
main
end
else
if
selection == "user"
puts "here are the details of user #{workspace.selected.name}"
workspace.show_details
elsif selection == "channel"
puts "here are the details of channel #{workspace.selected.name}"
workspace.show_details
else
puts "wrong input! try again"
main
end
end
when "quit"
puts "Thank you for using the Ada Slack CLI"
end
end
end


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

module SlackCli

class User < Recipient

attr_reader :slack_id, :name, :real_name, :status_text, :status_emoji

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

def details
return [{"slack_id" => slack_id,"name" => name ,"real_name" => real_name, "status_text" => status_text, "status_emoji" => status_emoji }]
end

def self.list_all
url = "https://slack.com/api/users.list"
param = {token: ENV["SLACK_BOT_TOKEN"]}
result = self.get(url, param)
user_list = result["members"].map { |user| SlackCli::User.new(slack_id: user["id"], name: user["name"], real_name: user["real_name"], status_text: user["status_text"], status_emoji: user["status_emoji"]) }
return user_list
end

end
end
108 changes: 108 additions & 0 deletions lib/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
require_relative 'channel'
require_relative 'user'
require 'table_print'
require 'json'


module SlackCli

class Workspace

attr_reader :users, :channels
attr_accessor :selected

def initialize(users:,channels:,selected:)
@users = SlackCli::User.list_all
@channels = SlackCli::Channel.list_all
@selected = nil
end

def select_channel
puts "here's a list of all channels available!"
tp self.channels.map{|channel| channel}, "slack_id","name"
puts "which channel would you like to select? or q to return to the previous menu!"
selected_cli = gets.chomp
if selected_cli == "q"
main
else
selection = find_id_or_name(channels,selected_cli)
if selection == true
puts "you selected #{selected.name}"
else
puts "#{selected_cli} is not a valid user! try again!"
select_channel
end
end
end

def select_user
puts "here's a list of all users available!"
tp self.users.map{|user| user}, "slack_id","name"
puts "which user would you like to select? or q to return to the previous menu!"
selected_cli = gets.chomp
if selected_cli == "q"
main
else
selection = find_id_or_name(users,selected_cli)
if selection == true
puts "you selected #{selected.name}"
else
puts "#{selected_cli} is not a valid user! try again!"
select_user
end
end
end

def find_id_or_name(items,string)
items.each do |item|
if item.slack_id.include?(string.upcase) || item.name.include?(string.downcase)
@selected = item
return true
end
end
return false
end

def show_details
tp selected.details
return selected.details
end

def send_message
if @selected != nil
puts "would you like to send a message to the previously selected user/channel? type previous to continue or type q to return to the previous menu"
puts "or would you like to select a new user/channel?"
selection = gets.chomp
if selection == "q"
return
elsif selection == "user"
select_user
elsif selection == "channel"
select_channel
elsif selection == "previous"
puts "you'll be sending a message to #{selected.name}"
else
puts "wrong input! try again!"
end
else
puts "would you like to send a message to a user or a channel?"
selection = gets.chomp
if selection == "user"
select_user
elsif selection == "channel"
select_channel
else
puts "wrong input! try again!"
end
end

puts "Type a message you'd like to send to #{selected.name}? or type q to return to the previous menu"
message = gets.chomp
if message == "q" || message == " "
return
else
selected.send_message(message)
end
end
end
end
Loading