Class: Lita::Robot
Overview
The main object representing a running instance of Lita. Provides a high level API for the underlying adapter. Dispatches incoming messages to registered handlers. Can send outgoing chat messages and set the topic of chat rooms.
Instance Attribute Summary collapse
-
#alias ⇒ String, Nil
An alias the robot will look for in incoming messages to determine if it’s being addressed.
-
#app ⇒ Rack::Builder
readonly
A
Rack
application used for the built-in web server. -
#auth ⇒ Lita::Authorization
readonly
The Authorization object for the currently running robot.
-
#mention_name ⇒ String
The name the robot will look for in incoming messages to determine if it’s being addressed.
-
#name ⇒ String
The name of the robot as it will appear in the chat.
-
#registry ⇒ Lita::Registry
readonly
The Registry for the currently running robot.
Instance Method Summary collapse
- #chat_service ⇒ Object
-
#initialize(registry = Lita) ⇒ Robot
constructor
A new instance of Robot.
-
#join(room) ⇒ void
Makes the robot join a room with the specified ID.
- #mention_format(name) ⇒ Object
-
#part(room) ⇒ void
Makes the robot part from the room with the specified ID.
-
#persisted_rooms ⇒ Array<String>
A list of room IDs the robot should join on boot.
-
#receive(message) ⇒ void
The primary entry point from the adapter for an incoming message.
- #roster(room) ⇒ Object
-
#run ⇒ void
Starts the robot, booting the web server and delegating to the adapter to connect to the chat service.
-
#send_messages(target, *strings) ⇒ void
(also: #send_message)
Sends one or more messages to a user or room.
-
#send_messages_with_mention(target, *strings) ⇒ void
(also: #send_message_with_mention)
Sends one or more messages to a user or room.
-
#set_topic(target, topic) ⇒ void
Sets the topic for a chat room.
-
#shut_down ⇒ void
Gracefully shuts the robot down, stopping the web server and delegating to the adapter to perform any shut down tasks necessary for the chat service.
-
#trigger(event_name, payload = {}) ⇒ void
Triggers an event, instructing all registered handlers to invoke any methods subscribed to the event, and passing them a payload hash of arbitrary data.
Constructor Details
#initialize(registry = Lita) ⇒ Robot
Returns a new instance of Robot.
51 52 53 54 55 56 57 58 59 |
# File 'lib/lita/robot.rb', line 51 def initialize(registry = Lita) @registry = registry @name = config.robot.name @mention_name = config.robot.mention_name || @name @alias = config.robot.alias @app = RackApp.build(self) @auth = Authorization.new(config) trigger(:loaded, room_ids: persisted_rooms) end |
Instance Attribute Details
#alias ⇒ String, Nil
An alias the robot will look for in incoming messages to determine if it’s being addressed.
26 27 28 |
# File 'lib/lita/robot.rb', line 26 def alias @alias end |
#app ⇒ Rack::Builder (readonly)
A Rack
application used for the built-in web server.
11 12 13 |
# File 'lib/lita/robot.rb', line 11 def app @app end |
#auth ⇒ Lita::Authorization (readonly)
The Authorization object for the currently running robot.
16 17 18 |
# File 'lib/lita/robot.rb', line 16 def auth @auth end |
#mention_name ⇒ String
The name the robot will look for in incoming messages to determine if it’s being addressed.
21 22 23 |
# File 'lib/lita/robot.rb', line 21 def mention_name @mention_name end |
#name ⇒ String
The name of the robot as it will appear in the chat.
30 31 32 |
# File 'lib/lita/robot.rb', line 30 def name @name end |
#registry ⇒ Lita::Registry (readonly)
The Lita::Registry for the currently running robot.
35 36 37 |
# File 'lib/lita/robot.rb', line 35 def registry @registry end |
Instance Method Details
#chat_service ⇒ Object
48 |
# File 'lib/lita/robot.rb', line 48 def_delegators :adapter, :chat_service, :mention_format, :roster |
#join(room) ⇒ void
This method returns an undefined value.
Makes the robot join a room with the specified ID.
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/lita/robot.rb', line 89 def join(room) room_object = find_room(room) if room_object Lita.redis.sadd("persisted_rooms", room_object.id) adapter.join(room_object.id) else adapter.join(room) end end |
#mention_format(name) ⇒ Object
48 |
# File 'lib/lita/robot.rb', line 48 def_delegators :adapter, :chat_service, :mention_format, :roster |
#part(room) ⇒ void
This method returns an undefined value.
Makes the robot part from the room with the specified ID.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/lita/robot.rb', line 104 def part(room) room_object = find_room(room) if room_object Lita.redis.srem("persisted_rooms", room_object.id) adapter.part(room_object.id) else adapter.part(room) end end |
#persisted_rooms ⇒ Array<String>
A list of room IDs the robot should join on boot.
118 119 120 |
# File 'lib/lita/robot.rb', line 118 def persisted_rooms Lita.redis.smembers("persisted_rooms").sort end |
#receive(message) ⇒ void
This method returns an undefined value.
The primary entry point from the adapter for an incoming message. Dispatches the message to all registered handlers.
65 66 67 68 69 70 71 72 73 |
# File 'lib/lita/robot.rb', line 65 def receive() matched = handlers.map do |handler| next unless handler.respond_to?(:dispatch) handler.dispatch(self, ) end.any? trigger(:unhandled_message, message: ) unless matched end |
#roster(room) ⇒ Object
48 |
# File 'lib/lita/robot.rb', line 48 def_delegators :adapter, :chat_service, :mention_format, :roster |
#run ⇒ void
This method returns an undefined value.
Starts the robot, booting the web server and delegating to the adapter to connect to the chat service.
78 79 80 81 82 83 |
# File 'lib/lita/robot.rb', line 78 def run run_app adapter.run rescue Interrupt shut_down end |
#send_messages(target, *strings) ⇒ void Also known as: send_message
This method returns an undefined value.
Sends one or more messages to a user or room.
128 129 130 |
# File 'lib/lita/robot.rb', line 128 def (target, *strings) adapter.(target, strings.flatten) end |
#send_messages_with_mention(target, *strings) ⇒ void Also known as: send_message_with_mention
This method returns an undefined value.
Sends one or more messages to a user or room. If sending to a room, prefixes each message with the user’s mention name.
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/lita/robot.rb', line 141 def (target, *strings) return (target, *strings) if target. mention_name = target.user.mention_name prefixed_strings = strings.map do |s| "#{adapter.mention_format(mention_name).strip} #{s}" end (target, *prefixed_strings) end |
#set_topic(target, topic) ⇒ void
This method returns an undefined value.
Sets the topic for a chat room.
157 158 159 |
# File 'lib/lita/robot.rb', line 157 def set_topic(target, topic) adapter.set_topic(target, topic) end |
#shut_down ⇒ void
This method returns an undefined value.
Gracefully shuts the robot down, stopping the web server and delegating to the adapter to perform any shut down tasks necessary for the chat service.
165 166 167 168 169 170 171 |
# File 'lib/lita/robot.rb', line 165 def shut_down trigger(:shut_down_started) @server.stop(true) if @server @server_thread.join if @server_thread adapter.shut_down trigger(:shut_down_complete) end |
#trigger(event_name, payload = {}) ⇒ void
This method returns an undefined value.
Triggers an event, instructing all registered handlers to invoke any methods subscribed to the event, and passing them a payload hash of arbitrary data.
179 180 181 182 183 184 185 |
# File 'lib/lita/robot.rb', line 179 def trigger(event_name, payload = {}) handlers.each do |handler| next unless handler.respond_to?(:trigger) handler.trigger(self, event_name, payload) end end |