class Object
Constants
- CONFIG_FILE
- MESSAGE_REGEX
Public Instance Methods
check_config()
click to toggle source
# File bin/mv-slacker, line 35 def check_config $config = {} config_changed = false config_file = File.new(CONFIG_FILE, File::CREAT|File::RDWR, 0600) raise "Permissions for #{CONFIG_FILE} are too liberal" if config_file.stat.world_readable? data = config_file.read unless data.empty? $config = JSON.parse(data).tap do |h| h.keys.each { |k| h[k.to_sym] = h.delete(k) } end end # log in to slack loop do if $config[:slack_token] begin result = SlackAPI.get("/api/auth.test") $slack_user_id = result["user_id"] puts "Logged into Slack" break rescue SlackAPI::Error => e puts "Slack login failed: #{e.message}" $config.delete(:slack_token) end end if !$config[:slack_token] config_changed = true $config[:slack_token] = ask("Paste your Slack token: ") end end # log in to motivosity loop do if $config[:mv_username] && $config[:mv_password] begin $mvclient.login! $config[:mv_username], $config[:mv_password] $company_values = $mvclient.get_values puts "Logged in to Motivosity" break rescue Motivosity::UnauthorizedError puts "Motivosity login failed" $config.delete(:mv_username) $config.delete(:mv_password) end end if !$config[:mv_username] || !$config[:mv_password] config_changed = true $config[:mv_username] = ask("Enter your Motivosity username: ") $config[:mv_password] = ask("Enter your Motivosity password: ") { |q| q.echo = false } end end # save config if config_changed config_file.rewind config_file.truncate(0) config_file.write $config.to_json end end
find_user(message, name)
click to toggle source
# File bin/mv-slacker, line 115 def find_user(message, name) users = [] begin users = $mvclient.search_for_user(name) rescue Motivosity::UnauthorizedError # in case session has expired $mvclient.login! $config[:mv_username], $config[:mv_password] users = $mvclient.search_for_user(name) end if users.size > 1 # check for an exact match among the search results matching_users = users.select { |user| user['fullName'].downcase == name.downcase } if matching_users.size != 1 puts "Ambiguous name: " + name.bold pm(message, "Multiple users match `#{name}`! Try one of #{users.map{|user| "`#{user['fullName']}`"}.join(', ')}") return [nil, nil] else users = matching_users end end if users.empty? puts "User not found: " + name.bold pm(message, "User `#{name}` not found") return [nil, nil] end [users[0]['id'], users[0]['fullName']] end
open_im_channel(user_id)
click to toggle source
# File bin/mv-slacker, line 95 def open_im_channel(user_id) json = SlackAPI.get "/api/im.open", user: user_id json["channel"]["id"] end
pm(message, text)
click to toggle source
send a direct message to the caller
# File bin/mv-slacker, line 106 def pm(message, text) send_in_channel(open_im_channel($slack_user_id), text) end
process_message(message, amount, name, company_value, note)
click to toggle source
# File bin/mv-slacker, line 143 def process_message(message, amount, name, company_value, note) # slack likes to replay old messages when you first log in # let's avoid sending redundant thanks by ignoring messages older than our slack login if message['ts'].to_f < $slack_login_time puts "ignoring old message: #{message['text']}".light_black return end # find the company value, if one was given value = $company_values.detect { |v| v['name'].downcase == company_value.downcase } if company_value value_id = value['id'] if value # look up the user user_id, user_name = find_user(message, name) return unless user_id # let's do this print "Sending $" + "#{amount}".on_green + " to " + "#{user_name}".on_red print " for " + "#{value['name']}".on_blue if value puts ": " + note.bold begin response = $mvclient.send_appreciation! user_id, amount: amount, company_value_id: value_id, note: note rescue => e puts "Failed to send appreciation: ".red + e.inspect pm(message, "Failed to send appreciation: `#{e.message}`") return end reply(message, "*Success!* #{user_name} has received your appreciation.") end
reply(message, text)
click to toggle source
reply to the message in channel
# File bin/mv-slacker, line 111 def reply(message, text) send_in_channel(message["channel"], text) end
send_in_channel(channel, text)
click to toggle source
# File bin/mv-slacker, line 100 def send_in_channel(channel, text) SlackAPI.get "/api/chat.postMessage", channel: channel, username: 'Motivosity Slacker', as_user: false, text: text, unfurl_links: false, unfurl_media: false, icon_emoji: ":motivosity:" end