Sha256: 35497eac1663f145caf82bddb37e74ea4b74f8aced0d97bdce45a42ecfd2fb40

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module Botkit
  class Bot
    include Signal

    # When setting `bot.halt = true`, the bot will be halted
    # after the current loop.
    attr_writer :halt

    # Detect if bot should halt himself from the loop.
    def halt?
      @halt
    end

    def call
    end

    def send_message(message)
    end

    def reply_message(_message, reply)
      send_message(reply)
    end

    def handle_incoming_message(message)
      if message.command?
        emit("command:#{message.command}", message)
      else
        emit("internal:message", message)
      end
    end

    def parse_message(input, matcher = nil)
      input = input.to_s
      _, command, text = *input.match(%r{\A/([^ ]+)(?:\s+(.*?))?\z})
      matches = text.to_s.match(matcher) if matcher

      {
        command: command,
        text: text || input,
        matches: matches&.named_captures
      }
    end

    def command(name, &block)
      on("command:#{name}", &block)
    end

    def message(&block)
      on("internal:message", &block)
    end

    def exception(&block)
      on("internal:exception", &block)
    end

    def report_exception(exception)
      emit("internal:exception", exception)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
botkit-0.0.2 lib/botkit/bot.rb