Class: Lita::Message

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/lita/message.rb

Overview

Represents an incoming chat message.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(robot, body, source) ⇒ Message

Returns a new instance of Message.

Parameters:

  • robot (Lita::Robot)

    The currently running robot.

  • body (String)

    The body of the message.

  • source (Lita::Source)

    The source of the message.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Attribute Details

#bodyString (readonly)

The body of the message.

Returns:

  • (String)

    The message body.



8
9
10
# File 'lib/lita/message.rb', line 8

def body
  @body
end

#extensionsHash (readonly)

A hash of arbitrary data that can be populated by Lita adapters and extensions.

Returns:

  • (Hash)

    The extension data.

Since:

  • 4.7.0



17
18
19
# File 'lib/lita/message.rb', line 17

def extensions
  @extensions
end

#sourceLita::Source (readonly)

The source of the message, which is a user and optional room.

Returns:



12
13
14
# File 'lib/lita/message.rb', line 12

def source
  @source
end

Instance Method Details

#argsArray<String>

An array of arguments created by shellsplitting the message body, as if it were a shell command.

Returns:

  • (Array<String>)

    The array of arguments.



58
59
60
61
62
63
64
65
66
67
# 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!void

This method returns an undefined value.

Marks the message as a command, meaning it was directed at the robot specifically.



72
73
74
# File 'lib/lita/message.rb', line 72

def command!
  @command = true
end

#command?Boolean

A boolean representing whether or not the message was a command.

Returns:

  • (Boolean)

    true if the message was a command, false if not.



78
79
80
# File 'lib/lita/message.rb', line 78

def command?
  @command
end

#match(pattern) ⇒ Array<String>+

An array of matches against the message body for the given Regexp.

Parameters:

  • pattern (Regexp)

    A pattern to match.

Returns:

  • (Array<String>, Array<Array<String>>)

    An array of matches.



85
86
87
# File 'lib/lita/message.rb', line 85

def match(pattern)
  body.scan(pattern)
end

#private_message?Boolean

Flag indicating that the message was sent to the robot privately.

Returns:

  • (Boolean)

    The boolean flag.

See Also:

Since:

  • 4.5.0



33
# File 'lib/lita/message.rb', line 33

def_delegators :source, :user, :room_object, :private_message?

#reply(*strings) ⇒ void

This method returns an undefined value.

Replies by sending the given strings back to the source of the message.

Parameters:

  • strings (String, Array<String>)

    The strings to send back.



92
93
94
# File 'lib/lita/message.rb', line 92

def reply(*strings)
  @robot.send_messages(source, *strings)
end

#reply_privately(*strings) ⇒ void

This method returns an undefined value.

Replies by sending the given strings back to the user who sent the message directly, even if the message was sent in a room.

Parameters:

  • strings (String, Array<String>)

    The strings to send back.



100
101
102
103
104
# 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) ⇒ void

This method returns an undefined value.

Replies by sending the given strings back to the source of the message. Each message is prefixed with the user’s mention name.

Parameters:

  • strings (String, Array<String>)

    The strings to send back.

Since:

  • 3.1.0



111
112
113
# File 'lib/lita/message.rb', line 111

def reply_with_mention(*strings)
  @robot.send_messages_with_mention(source, *strings)
end

#room_objectLita::Room

The room where the message came from.

Returns:

See Also:

Since:

  • 4.5.0



33
# File 'lib/lita/message.rb', line 33

def_delegators :source, :user, :room_object, :private_message?

#userLita::User

The user who sent the message.

Returns:

See Also:



33
# File 'lib/lita/message.rb', line 33

def_delegators :source, :user, :room_object, :private_message?