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.



23
24
25
26
27
28
29
30
31
32
# File 'lib/lita/message.rb', line 23

def initialize(robot, body, source)
  @robot = robot
  @body = body
  @source = source

  name_pattern = Regexp.escape(@robot.mention_name)
  name_pattern = "#{name_pattern}|#{Regexp.escape(@robot.alias)}" if @robot.alias

  @command = !!@body.sub!(/^\s*@?(?:#{name_pattern})[:,]?\s*/i, "")
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

#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.



37
38
39
40
41
42
43
44
45
46
# File 'lib/lita/message.rb', line 37

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.



51
52
53
# File 'lib/lita/message.rb', line 51

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.



57
58
59
# File 'lib/lita/message.rb', line 57

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.



64
65
66
# File 'lib/lita/message.rb', line 64

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

#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.



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

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.



79
80
81
82
83
# File 'lib/lita/message.rb', line 79

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



90
91
92
# File 'lib/lita/message.rb', line 90

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

#userLita::User

The user who sent the message.

Returns:

See Also:



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

def_delegators :source, :user