Sha256: dc2fa304b7a4a26a6e5360b464a37a1ac3e93d30b1ca43355804879e40a3e0b8

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.1 lib/botkit/bot.rb