Moogsoft Docs

Events

The Events Moobot module allows you to make a Moobot driven by the occurrence of events by defining the type of event that interests the Moobot.

The Events module is available to load into any standard Moobot.

To use, at the top of a Moobot js file, define a new global object events to load the Events module:

var events = MooBot.loadModule('Events');
Method
  • events.onEvent

The Events module has only one method, onEvent. This method points the Moobot to a supplied JavaScript function, which is called when a specified event type occurs.

The parameters to the called function depend on the type of event that you are listening for.

In a Moobot, this method is typically the last line in a script.

The type of event adaptor chosen is specific to the type of Moobot you are building.

Reference Guide
events.onEvent()

Takes the name of a valid JavaScript function in a Moobot and also event code (from the Constants module eventType), and returns an event adaptor object

Request Arguments

Name

Type

Description

functionName

String

The name of a valid JavaScript function in the Moobot that is called when the event arrives.

type

CEvent

eventType event code that specifies what type of event the Moobot is listening for. It is typically from the Constants module.

Return Parameter

Name

Type

Description

CEventAdaptor

Object

An event adaptor object. Made active with the listen () function in-line to listen for the event type.

Example

For the AlertBuilder MooBot:

events.onEvent("newEvent",constants.eventType("Event")).listen();
  • Call the newEvent JavaScript function.

  • Define the Event type Event (from the Constants module), which responds to events put on the Message Bus by a LAM.

  • Call the listen function in-line to listen for the event type.

When the Moolet starts and loads this events Moobot, its JavaScript file executes, initializing the Moobot to respond in an event-driven way to events arriving.

newEvent Javascript function

The format of the function newEvent (which is called when you get an event), is as follows:

newEvent()

Request Argument

Name

Type

Description

event

CEvent object

An object that encapsulates all the data for the event from the Message Bus, and allows you to forward the event to the bus, using the CEvent forward method detailed below.

CEvent forward methods

You can emulate MoogDb behavior by running the MoogDb.V2 Moobots.For example, the alert.forward(this) line will send an alert onto the Moolets specified in the appropriate process_output_of block within moog_farmd.conf.

You could instead remove the process_output_of lines from the AlertRulesEngine / Sigaliser / Cookbook / Speedbird Moolets and explicitly send events / alerts / Situations on within the Moobot code using (as an example):

alert.forward("Cookbook");

The advantage of this approach is that alerts / Situations can be forwarded to different AlertRulesEngines / Sigalisers dynamically in the Moobots (for example based on the value of the source file).

CEventAdaptor auxiliary object

This object is a utility class used by the Events module to allow for the programmatic activation of event listening. It has one method:

listen()

Starts the event adaptor listening, which then calls the specified function when an event occurs.

Request Argument

None.

Return Parameter

Void - no value returned.

CEvent auxiliary object

This object encapsulates a generic Message Bus event object, and the contents of it are specific to the event type it represents. You can however access the key-value pairs contained in the object, and also set the values. Its methods include:

contains()

Returns true if the Event contains a value stored at the key name.

Request Argument

Name

Type

Description

name

String

The name of the key being queried.

Return Parameter

Type

Description

Boolean

True if the event has a field called name, otherwise false.

set()

Associates the specified value with the specified name in the event.

Previous key mapping has the old value replaced.

Request Argument

Name

Type

Description

name

String

The key with which the specified value is to be associated.

value

Object

The value associated with the key.

Return Parameter

Type

Description

Boolean

Indicates if the operation was successful: true = success, false = fail

value()

Returns the object stored at the key name.

Request Argument

Name

Type

Description

name

String

The name of the key to return the object from.

Return Parameter

Type

Description

Object

A Javascript object containing what is at the key name.

Note

Compatibility with MoogDb and MoogDb V2 Methods and auxiliary objects listed here are compatible with the MoogDb V2 module.