Sha256: ad686bf1d934964ca899725209e57a21dbb6e9568d287792daac7b83d107c5b6

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Lita
  module Handlers
    # Send a message to multiple channels simultaneously.
    class Broadcast < Handler
      route(/^tell\s+(?<channels>[^:]+):\s*(?<message>.+)/i,
            :broadcast,
            command: true,
            help: { t('help.broadcast_key') => t('help.broadcast_value') })

      def broadcast(response)
        channels = response.match_data['channels'].strip.split
        message = response.match_data['message']

        channels.each do |identifier|
          target = channel_for(identifier)
          return response.reply(t('broadcast.not_found', channel: identifier)) if target.nil?

          robot.send_message(target, render_template('broadcast',
                                                     user: response.user,
                                                     says: t('broadcast.says'),
                                                     message: message))
        end
      end

      private

      def channel_for(identifier)
        room = Lita::Room.fuzzy_find(identifier)
        room = Lita::Room.find_by_name(identifier.to_s.sub(/^#+/, '')) if room.nil? && robot.config.robot.adapter == :slack
        Source.new(room: room) if room
      end

      Lita.register_handler(self)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lita-broadcast-0.1.2 lib/lita/handlers/broadcast.rb