CustomComponentContext

Lib~ CustomComponentContext

The Bots Node SDK uses this class to receive bot requests, which is provided to the custom component invocation.

It offers a comprehensive interface to reading context for the invocation as well as changing variables and sending results back to the dialog engine.

Constructor

new CustomComponentContext(request)

Source:
Example

Conversation object used to invoke Custom Component

const MyCustomComponent = {
  metadata: () => ({name: 'hello'}),
  invoke: (conversation, done) => {
    // use conversation instance methods to respond, set variables, etc.
    conversation.reply('Hello!');
    conversation.transition();
    done();
  }
}
Parameters:
Name Type Description
request object The request body

Extends

Methods

(static) sdkVersion() → {string}

Source:
Retrieves the sdk version.
Returns:
The sdk version.
Type
string

attachment() → {object}

Source:
Retrieves the attachment of the current input message. If the input message is not an attachment, this will return null.
Returns:
The attachment.
Type
object

botId() → {string}

Source:
Retrieves the bot id.
Returns:
The bot id.
Type
string

channelId() → {string}

Source:
Retrieves the channel Id of the current input message.
Returns:
The channel id.
Type
string

channelType() → {string}

Source:
Retrieves the channel type of the current input message.
Returns:
The channel type - facebook, webhook, test, etc.
Type
string

constructMessagePayload(payload)

Source:
Overrides:
Creates a message payload object
Parameters:
Name Type Description
payload object can take a string payload, an object payload or a MessageModel payload. A string or object payload will be parsed into a MessageModel payload. If the MessageModel payload has a valid common message format, then reply will use it as messagePayload, else it will use the payload to create a rawConversationMessage (see MessageModel) as messagePayload.

error(e)

Source:
Sets the error flag on the response.
Parameters:
Name Type Description
e boolean sets error if true

getLogger() → {object}

Source:
Overrides:
Retrieves the response object.
Returns:
The response object.
Type
object

getMessageModel() → {MessageModel}

Source:
Overrides:
See:
  • MessageModel.js
Returns the MessageModel class for creating or validating messages to or from bots.
Returns:
The MessageModel class
Type
MessageModel

getRequest() → {object}

Source:
Overrides:
Retrieves the request object.
Returns:
The request object.
Type
object

getResponse() → {object}

Source:
Overrides:
Retrieves the response object.
Returns:
The response object.
Type
object

getVariable(name) → {object}

Source:
Overrides:
Returns the value of a context or user variable
Parameters:
Name Type Description
name string name of the variable
Returns:
variable value
Type
object

invalidUserInput(ropt)

Source:
Call this method if the input is not understood, and this would allow the bots runtime to handle the issue. The bots runtime may just display the message to the user and execute the same component again, or it may try to interpret the input and process differently.
Parameters:
Name Type Attributes Description
r object | string | MessageModel <optional>
optional payload to be sent to user. payload could also be a string for text response

keepTurn(kopt)

Source:
"keepTurn" is used to indicate if the Bot/component should send the next replies, or or if the Bot/component should wait for user input (keepTurn = false).

The SDK's "reply" function automatically sets "keepTurn" to false.

Parameters:
Name Type Attributes Description
k boolean <optional>
whether to keep the turn for sending more replies

location() → {object}

Source:
Retrieves the location of the current input message. If the input message does not contain a location, this will return null.
Returns:
The location.
Type
object

logger() → {object}

Source:
Retrieves the logger so the component can use the shared logger for logging. The shared logger should support the methods log, info, warn, error and trace.
Returns:
The logger.
Type
object

MessageModel() → {MessageModel}

Source:
Returns the MessageModel class for creating or validating messages to or from bots.
Returns:
The MessageModel class
Type
MessageModel

messagePayload() → {object}

Source:
Retrieves the payload of the current input message in the common message format.
Returns:
The common message payload.
Type
object

nlpResult(nlpVariableNameopt) → {NLPResult}

Source:
Returns an NLPResult helper object for working with nlpresult variables. See the NLPResult documentation for more information.

You may specify a particular nlpresult by name (if you have multiple nlpresult variables defined in the flow), or omit the name if you only have 1 nlpresult.

Parameters:
Name Type Attributes Description
nlpVariableName string <optional>
variable to be given the nlpResult
Returns:
The nlp resolution result.
Type
NLPResult

platformVersion() → {string}

Source:
Retrieves the platform version of the request.
Returns:
The platform version.
Type
string

postback() → {object}

Source:
Retrieves the postback of the current input message. If the input message is not a postback, this will return null.
Returns:
The postback payload.
Type
object

properties() → {object}

Source:
Retrieves the properties defined for the current state.
Returns:
The properties
Type
object

rawPayload() → {object}

Source:
Retrieves the raw payload of the current input message.
Returns:
The raw payload.
Type
object

releaseTurn(kopt)

Source:
"releaseTurn" is the shorthand for keepTurn(false)
Parameters:
Name Type Attributes Description
k boolean <optional>
whether to keep the turn for sending more replies

reply(payload, channelConversationopt)

Source:
Adds a reply to be sent back to the user. May be called multiple times to send multiple replies in a given response. Automatically sets the keepTurn as false.

reply can take a string payload, an object payload or a MessageModel payload. A string or object payload will be parsed into a MessageModel payload. If the MessageModel payload has a valid common message format, then reply will use it as messagePayload, else it will use the payload to create a rawConversationMessage (see MessageModel) as messagePayload.

Parameters:
Name Type Attributes Description
payload object | string | MessageModel payload to be sent back. payload could also be a string for text response
channelConversation object <optional>
to override the default channelConversation from request

request() → {object}

Source:
Retrieves the request body.
Returns:
The request body.
Type
object

sessionId() → {string}

Source:
Retrieves the sessionId for the current input message.
Returns:
The sessionId.
Type
string

setVariable(name, value)

Source:
Overrides:
Sets the value of a context or user variable
Parameters:
Name Type Description
name string name of the variable
value object value of the variable

text() → {string}

Source:
Retrieves the text of the current input message. Eventually not all messages will have a text value, in which case this will return null.
Returns:
The text of the input message.
Type
string

transition(topt)

Source:
Call transition() when your component has completed its logic and the dialog should transition to the next state, after replies (if any) are sent.

If transition() is not called, the dialog will stay in this state after sending the replies (if any), and subsequent user input will come back to this component. This allows a component to handle a series of interactions within itself, however the component is responsible for keeping track of its own state in such situations.

transition() will cause the dialog to transition to the next state. transition(outcome) will set te outcome of the component that would be used to determine the next state to transition to.
Parameters:
Name Type Attributes Description
t string <optional>
outcome of component

userId() → {string}

Source:
Retrieves the userId for the current input message.
Returns:
The userId.
Type
string

variable(name, valueopt)

Source:
Overrides:
Read or write variables defined in the current flow. It is not possible to change the type of an existing variable through this method. It is the caller's responsibility to ensure that the value being set on a variable is of the correct type. (e.g. entity, string or other primitive, etc).

A new variable can be created. However, since the variable is not defined in the flow, using it in the flow subsequently may be flagged for validation warnings.

This function takes a variable number of arguments.

The first form: variable(name); reads the variable called "name", returning its value. The name could be in the form of <scope>.<variableName>. For example, a variable firstName in the profile scope needs to be retrieved as variable("profile.firstName").

The second form: variable(name, value); writes the value "value" to the variable called "name".
Example
let firstName = conversation.variable("profile.firstName");
let lastName = conversation.variable("profile.lastName");
conversation.variable("fullName", firstName + ' ' + lastName);
Parameters:
Name Type Attributes Description
name string The name of variable to be set or read
value string <optional>
value to be set for variable