Class: Lita::Message
- Inherits:
-
Object
- Object
- Lita::Message
- Extended by:
- Forwardable
- Defined in:
- lib/lita/message.rb
Overview
Represents an incoming chat message.
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
The body of the message.
-
#extensions ⇒ Hash
readonly
A hash of arbitrary data that can be populated by Lita adapters and extensions.
-
#source ⇒ Lita::Source
readonly
The source of the message, which is a user and optional room.
Instance Method Summary collapse
-
#args ⇒ Array<String>
An array of arguments created by shellsplitting the message body, as if it were a shell command.
-
#command! ⇒ void
Marks the message as a command, meaning it was directed at the robot specifically.
-
#command? ⇒ Boolean
A boolean representing whether or not the message was a command.
-
#initialize(robot, body, source) ⇒ Message
constructor
A new instance of Message.
-
#match(pattern) ⇒ Array<String>+
An array of matches against the message body for the given Regexp.
-
#private_message? ⇒ Boolean
Flag indicating that the message was sent to the robot privately.
-
#reply(*strings) ⇒ void
Replies by sending the given strings back to the source of the message.
-
#reply_privately(*strings) ⇒ void
Replies by sending the given strings back to the user who sent the message directly, even if the message was sent in a room.
-
#reply_with_mention(*strings) ⇒ void
Replies by sending the given strings back to the source of the message.
-
#room_object ⇒ Lita::Room
The room where the message came from.
-
#user ⇒ Lita::User
The user who sent the message.
Constructor Details
#initialize(robot, body, source) ⇒ Message
Returns a new instance of 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
#body ⇒ String (readonly)
The body of the message.
8 9 10 |
# File 'lib/lita/message.rb', line 8 def body @body end |
#extensions ⇒ Hash (readonly)
A hash of arbitrary data that can be populated by Lita adapters and extensions.
17 18 19 |
# File 'lib/lita/message.rb', line 17 def extensions @extensions end |
#source ⇒ Lita::Source (readonly)
The source of the message, which is a user and optional room.
12 13 14 |
# File 'lib/lita/message.rb', line 12 def source @source end |
Instance Method Details
#args ⇒ Array<String>
An array of arguments created by shellsplitting the message body, as if it were a shell command.
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.
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.
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.
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.
92 93 94 |
# File 'lib/lita/message.rb', line 92 def reply(*strings) @robot.(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.
100 101 102 103 104 |
# File 'lib/lita/message.rb', line 100 def reply_privately(*strings) private_source = source.clone private_source. @robot.(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.
111 112 113 |
# File 'lib/lita/message.rb', line 111 def reply_with_mention(*strings) @robot.(source, *strings) end |
#room_object ⇒ Lita::Room
The room where the message came from.
33 |
# File 'lib/lita/message.rb', line 33 def_delegators :source, :user, :room_object, :private_message? |
#user ⇒ Lita::User
The user who sent the message.
33 |
# File 'lib/lita/message.rb', line 33 def_delegators :source, :user, :room_object, :private_message? |