async-matrixSourceAsyncMatrixApplicationServiceBot

class Bot

DSL wrapper that pairs a Client with event handlers.

A Bot produces handler objects that conform to the Dispatcher's duck-type contract (#event_types, #call). Register a bot on a Server (or Dispatcher) the same way you would register a plain handler.

bot = Bot.new(client) do on "m.room.member" do |event| join_room(event.room_id) if event.content.membership == "invite" end

on "m.room.message", msgtype: "m.text", not_from: :self do |event|
  send_notice event.room_id, "Echo: #{event.content.body}"
end

end

server.register(bot)

Nested

Definitions

def on(*event_types, msgtype: nil, not_from: nil, &block)

Register a handler block for one or more event types.

Filters (all optional): msgtype: — only dispatch when content.msgtype matches (e.g. "m.text") not_from: — :self skips events sent by this bot's own MXID

Implementation

def on(*event_types, msgtype: nil, not_from: nil, &block)
  raise ArgumentError, "on requires at least one event type" if event_types.empty?
  raise ArgumentError, "on requires a block" unless block

  handler = Handler.new(
    bot:         self,
    event_types: event_types.flatten,
    msgtype:     msgtype,
    not_from:    not_from,
    block:       block
  )

  @handlers << handler
  handler
end