class Lita::Message

Represents an incoming chat message.

Attributes

body[R]

The body of the message. @return [String] The message body.

extensions[R]

A hash of arbitrary data that can be populated by Lita adapters and extensions. @return [Hash] The extension data. @since 4.7.0

source[R]

The source of the message, which is a user and optional room. @return [Lita::Source] The message source.

Public Class Methods

new(robot, body, source) click to toggle source

@param robot [Lita::Robot] The currently running robot. @param body [String] The body of the message. @param source [Lita::Source] The source of the message.

# File lib/lita/message.rb, line 38
def initialize(robot, body, source)
  @robot = robot
  @body = body
  @source = source
  @extensions = {}

  name_pattern = "@?#{Regexp.escape(@robot.mention_name)}[:,]?\\s+"
  alias_pattern = "#{Regexp.escape(@robot.alias)}\\s*" if @robot.alias
  command_regex = if alias_pattern
    /^\s*(?:#{name_pattern}|#{alias_pattern})/i
  else
    /^\s*#{name_pattern}/i
  end

  @command = !!@body.sub!(command_regex, "")
end

Public Instance Methods

args() click to toggle source

An array of arguments created by shellsplitting the message body, as if it were a shell command. @return [Array<String>] The array of arguments.

# File lib/lita/message.rb, line 58
def args
  begin
    _command, *args = body.shellsplit
  rescue ArgumentError
    _command, *args =
      body.split(/\s+/).map(&:shellescape).join(" ").shellsplit
  end

  args
end
command!() click to toggle source

Marks the message as a command, meaning it was directed at the robot specifically. @return [void]

# File lib/lita/message.rb, line 72
def command!
  @command = true
end
command?() click to toggle source

A boolean representing whether or not the message was a command. @return [Boolean] true if the message was a command, false if not.

# File lib/lita/message.rb, line 78
def command?
  @command
end
match(pattern) click to toggle source

An array of matches against the message body for the given {::Regexp}. @param pattern [Regexp] A pattern to match. @return [Array<String>, Array<Array<String>>] An array of matches.

# File lib/lita/message.rb, line 85
def match(pattern)
  body.scan(pattern)
end
reply(*strings) click to toggle source

Replies by sending the given strings back to the source of the message. @param strings [String, Array<String>] The strings to send back. @return [void]

# File lib/lita/message.rb, line 92
def reply(*strings)
  @robot.send_messages(source, *strings)
end
reply_privately(*strings) click to toggle source

Replies by sending the given strings back to the user who sent the message directly, even if the message was sent in a room. @param strings [String, Array<String>] The strings to send back. @return [void]

# File lib/lita/message.rb, line 100
def reply_privately(*strings)
  private_source = source.clone
  private_source.private_message!
  @robot.send_messages(private_source, *strings)
end
reply_with_mention(*strings) click to toggle source

Replies by sending the given strings back to the source of the message. Each message is prefixed with the user's mention name. @param strings [String, Array<String>] The strings to send back. @return [void] @since 3.1.0

# File lib/lita/message.rb, line 111
def reply_with_mention(*strings)
  @robot.send_messages_with_mention(source, *strings)
end