class Lita::Handlers::Announce
Public Instance Methods
handle_announce(response)
click to toggle source
# File lib/lita/handlers/announce.rb, line 123 def handle_announce(response) channels = parse_channels(response.args[1]) message = response.args[2..-1].join(" ") return if channels.nil? payload = { author: response.user.name, channels: channels.uniq, text: message, timestamp: DateTime.now.to_time.to_i, } save_announcement(payload) make_announcement_to_channels(payload) end
handle_del_group(response)
click to toggle source
# File lib/lita/handlers/announce.rb, line 111 def handle_del_group(response) group = response.args[1] if redis.get("group:#{group}").nil? response.reply(":failed: Can not delete '#{group}': does not exist.") return end redis.del("group:#{group}") response.reply(":successful: Announcement group '#{group}' deleted.") end
handle_list_groups(response)
click to toggle source
# File lib/lita/handlers/announce.rb, line 71 def handle_list_groups(response) results = [] redis.scan_each do |key| if key =~ /^group:(\w+)$/ channels = MultiJson.load(redis.get(key))["channels"] results << "*#{$1}*: #{channels.map { |c| "\##{c}" }.join(", ")}" end end response.reply(results.join("\n")) end
handle_mod_group(response)
click to toggle source
# File lib/lita/handlers/announce.rb, line 82 def handle_mod_group(response) group = response.args[1] channels = response.args[2] unless group =~ /^\w+$/ response.reply(":failed: '#{group}' is not a valid name. Please use only letters, numbers, and underscores.") return end unless response.args[3].nil? response.reply(":failed: Please provide channels as a comma-separated list with no spaces.") return end channel_array = channels.split(",").map { |c| c.gsub(/^\#/, "") } all_channels_exist = true channel_array.each do |c| if Lita::Room.fuzzy_find(c).nil? response.reply(":failed: Can not create '#{group}': the \##{c} channel does not exist.") all_channels_exist = false end end return unless all_channels_exist redis.set("group:#{group}", MultiJson.dump({ channels: channel_array })) response.reply(":successful: Announcement group '#{group}' updated! Will send messages to #{channel_array.map { |c| "\##{c}" }.join(", ")}") end
make_announcement_to_channels(payload)
click to toggle source
# File lib/lita/handlers/announce.rb, line 160 def make_announcement_to_channels(payload) payload[:channels].each do |c| room = Lita::Room.fuzzy_find(c) robot.join(room) robot.send_message(Lita::Source.new(room: room), "#{payload[:text]} - #{payload[:author]}") end end
parse_channels(targets)
click to toggle source
# File lib/lita/handlers/announce.rb, line 140 def parse_channels(targets) channels = [] targets.split(",").each do |target| c = parse_target(target) if c.nil? response.reply(":failed: Could not send message: the group '#{target}' could not be found.") return nil else channels.concat(c) end end channels end
parse_target(target)
click to toggle source
# File lib/lita/handlers/announce.rb, line 168 def parse_target(target) if target =~ /^group:(\w+)$/ group = redis.get(target) return nil if group.nil? MultiJson.load(group)["channels"] else [target] end end
save_announcement(payload)
click to toggle source
# File lib/lita/handlers/announce.rb, line 155 def save_announcement(payload) redis.lpush("announcements", MultiJson.dump(payload)) redis.ltrim("announcements", 0, config.announcements_to_keep - 1) end