Class: Lita::Robot

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

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

Instance Method Summary collapse

Constructor Details

#initialize(registry = Lita) ⇒ Robot

Returns a new instance of Robot

Parameters:

  • registry (Lita::Registry) (defaults to: Lita)

    The registry for the robot's configuration and plugins.


40
41
42
43
44
45
46
47
48
# File 'lib/lita/robot.rb', line 40

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)
end

Instance Attribute Details

#aliasString, Nil

An alias the robot will look for in incoming messages to determine if it's being addressed.

Returns:

  • (String, Nil)

    The alias, if one was set.


26
27
28
# File 'lib/lita/robot.rb', line 26

def alias
  @alias
end

#appRack::Builder (readonly)

A Rack application used for the built-in web server.

Returns:

  • (Rack::Builder)

    The Rack app.


11
12
13
# File 'lib/lita/robot.rb', line 11

def app
  @app
end

#authLita::Authorization (readonly)

The Authorization object for the currently running robot.

Returns:

Since:

  • 4.0.0


16
17
18
# File 'lib/lita/robot.rb', line 16

def auth
  @auth
end

#mention_nameString

The name the robot will look for in incoming messages to determine if it's being addressed.

Returns:

  • (String)

    The mention name.


21
22
23
# File 'lib/lita/robot.rb', line 21

def mention_name
  @mention_name
end

#nameString

The name of the robot as it will appear in the chat.

Returns:

  • (String)

    The robot's name.


30
31
32
# File 'lib/lita/robot.rb', line 30

def name
  @name
end

#registryLita::Registry (readonly)

The Lita::Registry for the currently running robot.

Returns:

Since:

  • 4.0.0


35
36
37
# File 'lib/lita/robot.rb', line 35

def registry
  @registry
end

Instance Method Details

#join(room_id) ⇒ void

This method returns an undefined value.

Makes the robot join a room with the specified ID.

Parameters:

  • room_id (String)

    The ID of the room.

Since:

  • 3.0.0


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

def join(room_id)
  adapter.join(room_id)
end

#part(room_id) ⇒ void

This method returns an undefined value.

Makes the robot part from the room with the specified ID.

Parameters:

  • room_id (String)

    The ID of the room.

Since:

  • 3.0.0


86
87
88
# File 'lib/lita/robot.rb', line 86

def part(room_id)
  adapter.part(room_id)
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.

Parameters:


54
55
56
57
58
59
60
61
62
# File 'lib/lita/robot.rb', line 54

def receive(message)
  matched = handlers.map do |handler|
    next unless handler.respond_to?(:dispatch)

    handler.dispatch(self, message)
  end.any?

  trigger(:unhandled_message, message: message) unless matched
end

#runvoid

This method returns an undefined value.

Starts the robot, booting the web server and delegating to the adapter to connect to the chat service.


67
68
69
70
71
72
# File 'lib/lita/robot.rb', line 67

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.

Parameters:

  • target (Lita::Source)

    The user or room to send to. If the Source has a room, it will choose the room. Otherwise, it will send to the user.

  • strings (String, Array<String>)

    One or more strings to send.


96
97
98
# File 'lib/lita/robot.rb', line 96

def send_messages(target, *strings)
  adapter.send_messages(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.

Parameters:

  • target (Lita::Source)

    The user or room to send to. If the Source has a room, it will choose the room. Otherwise, it will send to the user.

  • strings (String, Array<String>)

    One or more strings to send.

Since:

  • 3.1.0


109
110
111
112
113
114
115
116
117
118
# File 'lib/lita/robot.rb', line 109

def send_messages_with_mention(target, *strings)
  return send_messages(target, *strings) if target.private_message?

  mention_name = target.user.mention_name
  prefixed_strings = strings.map do |s|
    "#{adapter.mention_format(mention_name).strip} #{s}"
  end

  send_messages(target, *prefixed_strings)
end

#set_topic(target, topic) ⇒ void

This method returns an undefined value.

Sets the topic for a chat room.

Parameters:

  • target (Lita::Source)

    A source object specifying the room.

  • topic (String)

    The new topic message to set.


125
126
127
# File 'lib/lita/robot.rb', line 125

def set_topic(target, topic)
  adapter.set_topic(target, topic)
end

#shut_downvoid

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.


133
134
135
136
137
138
139
# File 'lib/lita/robot.rb', line 133

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.

Parameters:

  • event_name (String, Symbol)

    The name of the event to trigger.

  • payload (Hash) (defaults to: {})

    An optional hash of arbitrary data.


147
148
149
150
151
152
153
# File 'lib/lita/robot.rb', line 147

def trigger(event_name, payload = {})
  handlers.each do |handler|
    next unless handler.respond_to?(:trigger)

    handler.trigger(self, event_name, payload)
  end
end