-
Notifications
You must be signed in to change notification settings - Fork 31
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
Earth - Ringo & Jasmine #17
base: master
Are you sure you want to change the base?
Changes from all commits
2d084a1
08445a7
2cf3d20
f9b04a3
95694ac
ed7061a
02a716c
19549eb
e159690
7acca4b
16d636d
01c1a70
2a3d65c
951e680
cc601cf
a5aebc0
be4abee
af20912
a1d4f4a
b180d69
4f1f9ab
b522158
edf8037
51fb266
cde04a0
ba4e7ff
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 |
---|---|---|
|
@@ -4,3 +4,5 @@ | |
|
||
# Ignore environemnt variables | ||
.env | ||
.floo | ||
.flooignore |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'httparty' | ||
require 'dotenv' | ||
Dotenv.load | ||
|
||
require_relative 'recipient' | ||
|
||
class Channel < Recipient | ||
|
||
attr_reader :name, :topic, :member_count | ||
|
||
BASE_URL = "https://slack.com/api/conversations.list" | ||
|
||
def initialize(slack_id, name, topic, member_count) | ||
|
||
raise SlackApiError if [slack_id, name, topic, member_count].include? nil | ||
|
||
super(slack_id) | ||
@name = name | ||
@topic = topic | ||
@member_count = member_count | ||
end | ||
|
||
def self.list | ||
response = self.get(BASE_URL) | ||
all_our_pretty_channels = [] | ||
response["channels"].each_with_index do |channel| | ||
slack_id = channel["id"] | ||
name = channel["name"] | ||
topic = channel["purpose"]["value"] | ||
member_count = channel["num_members"] | ||
all_our_pretty_channels << Channel.new(slack_id, name, topic, member_count) | ||
end | ||
return all_our_pretty_channels | ||
end | ||
|
||
def details | ||
output = "Name: #{name}\nSlack ID: #{slack_id}\nMember Count: #{member_count}\nTopic: #{topic}" | ||
return output | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'httparty' | ||
require 'dotenv' | ||
Dotenv.load | ||
|
||
require_relative 'slack_api_error' | ||
|
||
class Recipient | ||
|
||
attr_reader :slack_id | ||
|
||
KEY = ENV["SLACK_TOKEN"] | ||
|
||
def initialize(slack_id) | ||
@slack_id = slack_id | ||
end | ||
|
||
def self.get(url, parameters: { token: KEY } ) | ||
response = HTTParty.get(url, query: parameters ) | ||
raise SlackApiError, "Error when getting info from #{url}" unless response['ok'] | ||
return response | ||
end | ||
|
||
def post(message) | ||
post_url = "https://slack.com/api/chat.postMessage" | ||
body = { | ||
token: KEY, | ||
text: message, | ||
channel: slack_id | ||
} | ||
|
||
begin | ||
bot_attributes = JSON.parse(File.read("bot-settings.json")) | ||
body.merge!(bot_attributes) | ||
rescue JSON::ParserError | ||
end | ||
|
||
response = HTTParty.post(post_url, body: body) | ||
|
||
raise SlackApiError, "Error when posting #{message}" unless response['ok'] | ||
return "Your message was delivered!" | ||
end | ||
|
||
def self.list | ||
raise NotImplementedError, "Call me in a child class" | ||
end | ||
|
||
def details | ||
raise NotImplementedError, "Call me in a child class" | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,83 @@ | ||
#!/usr/bin/env ruby | ||
# require "dotenv" | ||
require "httparty" | ||
require "pry" | ||
require "colorize" | ||
require_relative 'workspace.rb' | ||
# Dotenv.load | ||
|
||
def main | ||
puts "Welcome to the Ada Slack CLI!" | ||
puts "Welcome to the Ada Slack CLI!".colorize(:light_yellow) | ||
workspace = Workspace.new | ||
tp.set :max_width, 200 | ||
|
||
# TODO project | ||
puts "There are #{workspace.users.length} users and #{workspace.channels.length} channels".colorize(:light_yellow) | ||
|
||
options = "\nWhat would you like to do?\n— list users\n— list channels\n— select user\n— select channel\n— details\n- send message\n- customize bot\n— quit\n".colorize(:light_green) | ||
|
||
while true | ||
puts options | ||
print "=> " | ||
user_input = gets.chomp.downcase | ||
puts | ||
|
||
case user_input | ||
|
||
when "list users" | ||
tp *workspace.list("users") | ||
|
||
when "list channels" | ||
tp *workspace.list("channels") | ||
|
||
when "select user" | ||
puts "please enter user slack ID or username" | ||
input = gets.chomp | ||
looked_for_user = workspace.select("user", input: input) | ||
if looked_for_user | ||
puts "you have selected #{looked_for_user.username}" | ||
else | ||
puts "we could not find that user" | ||
end | ||
|
||
when "select channel" | ||
puts "please enter channel slack ID or name" | ||
input = gets.chomp | ||
looked_for_channel = workspace.select("channel", input: input) | ||
if looked_for_channel | ||
puts "you have selected #{looked_for_channel.name}" | ||
else | ||
puts "we could not find that channel" | ||
end | ||
|
||
when "details" | ||
puts workspace.details | ||
|
||
when "send message" | ||
if workspace.is_selected? | ||
puts "what message would you like to send?" | ||
text = gets.chomp | ||
puts workspace.send_message(text) | ||
else | ||
puts "Please select user or channel, first." | ||
end | ||
|
||
when "customize bot" | ||
puts "what would you like to name the bot?" | ||
username = gets.chomp | ||
puts "what emoji would you like to give the bot?" | ||
emoji = gets.chomp | ||
workspace.customize_bot(username, emoji) | ||
puts "enjoy your power to change the names and faces of others" | ||
|
||
when "quit" | ||
break | ||
|
||
else | ||
puts "I told you your options, buddy... try again.".colorize(:light_magenta) | ||
end | ||
end | ||
|
||
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,2 @@ | ||
class SlackApiError < Exception | ||
end |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,42 @@ | ||||||
require 'httparty' | ||||||
require 'dotenv' | ||||||
Dotenv.load | ||||||
|
||||||
require_relative 'recipient' | ||||||
|
||||||
class User < Recipient | ||||||
|
||||||
attr_reader :username, :real_name, :status_text, :status_emoji | ||||||
|
||||||
BASE_URL = "https://slack.com/api/users.list" | ||||||
|
||||||
def initialize(slack_id, username, real_name, status_text, status_emoji) | ||||||
|
||||||
raise SlackApiError if [slack_id, username, real_name, status_text, status_emoji].include? nil | ||||||
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. When I try to actually run the program by calling the In the midst of TDD, it's important to still manually run your code to make sure it all works together for the user, rather than relying only on the tests. 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.
Suggested change
|
||||||
|
||||||
super(slack_id) | ||||||
@username = username | ||||||
@real_name = real_name | ||||||
@status_text = status_text | ||||||
@status_emoji = status_emoji | ||||||
end | ||||||
|
||||||
def self.list | ||||||
response = self.get(BASE_URL) | ||||||
all_our_pretty_users = [] | ||||||
response["members"].each_with_index do |user| | ||||||
slack_id = user["id"] | ||||||
username = user["name"] | ||||||
real_name = user["real_name"] | ||||||
status_text = user["profile"]["status_text"] | ||||||
status_emoji = user['profile']['status_emoji'] | ||||||
all_our_pretty_users << User.new(slack_id, username, real_name, status_text, status_emoji) | ||||||
end | ||||||
return all_our_pretty_users | ||||||
end | ||||||
|
||||||
def details | ||||||
output = "Username: #{username}\nReal Name: #{real_name}\nSlack ID: #{slack_id}\nStatus: #{status_text}\nStatus Emoji: #{status_emoji}" | ||||||
return output | ||||||
end | ||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'table_print' | ||
require 'json' | ||
require_relative 'user' | ||
require_relative 'channel' | ||
|
||
|
||
class Workspace | ||
|
||
attr_reader :users, :channels, :selected | ||
|
||
def initialize | ||
@users = User.list | ||
@channels = Channel.list | ||
@selected = nil | ||
end | ||
|
||
def list(thing_to_print) | ||
if thing_to_print == "users" | ||
return [@users, :slack_id, :real_name, :username] | ||
elsif thing_to_print == "channels" | ||
return [@channels, :name, :member_count, :slack_id, :topic] | ||
end | ||
end | ||
|
||
def select(recipient_type, input: "") | ||
if recipient_type == "user" | ||
@selected = @users.find {|user| user.username.downcase == input.downcase || user.slack_id.downcase == input.downcase } | ||
elsif recipient_type == "channel" | ||
@selected = @channels.find {|channel| channel.name.downcase == input.downcase || channel.slack_id.downcase == input.downcase } | ||
end | ||
return @selected | ||
end | ||
|
||
def is_selected? | ||
return !!@selected | ||
end | ||
|
||
def details | ||
return "Please select user or channel, first." unless is_selected? | ||
return @selected.details | ||
end | ||
|
||
def send_message(text) | ||
return "Please select user or channel, first." unless is_selected? | ||
return @selected.post(text) | ||
end | ||
|
||
def customize_bot(username, emoji) | ||
# we format in hash | ||
body = { | ||
icon_emoji: emoji, | ||
username: username | ||
} | ||
# save hash to json file | ||
File.open("bot-settings.json","w") do |f| | ||
f.write(body.to_json) | ||
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.
Fun addition!