ComponentInvocation

ComponentInvocation

The Bots JS SDK exports a class that wraps an invocation to the custom component. Create it with the request payload (as an object) that was used to invoke the component. It offers a friendlier interface to reading context for the invocation as well as changing variables and sending results back to the diaog engine.

Constructor

new ComponentInvocation(requestBody)

Source:
Parameters:
Name Type Description
requestBody object The request body

Methods

(static) sdkVersion() → {string}

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

action(a)

Source:
Deprecated:
  • to be removed in favor of transition(action)
Sets the action to return from this component, which will determine the next state in the dialog.
Parameters:
Name Type Description
a string action name

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

channelType() → {string}

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

done()

Source:
Deprecated:
  • to be removed in favor of transition()
Set "done" to true when your component has completed its logic and the dialog should transition to the next state. This is only meaningful when you are sending replies (ie you have also set "exit" to true). If you are not sending replies ("exit" is false, the default) then "done" is ignored; the dialog always moves to the next state. If "exit" is true (replies are being sent), then leaving "done" as false (the default) means the dialog will stay in this state after sending the replies, 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. Setting "done" to true will transition to the next state/component after sending the replies.

error(e)

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

exit()

Source:
Deprecated:
  • to be removed in favor of keepTurn(boolean)
Set "exit" to true when your component has replies it wants to send to the client. The SDK's "reply" function automatically sets "exit" to true, but if you manually modify the response to send replies then you will need to set this explicitly.

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 attachment.
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

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

payload() → {object}

Source:
Deprecated:
  • to be removed in favor of rawPayload() and messagePayload()
Retrieves the payload of the current input message. For backward compatibility purposes. However, the payload returned may be in the new message format.
Returns:
The message payload.
Type
object

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

setCallbackToken(callbackToken)

Source:
When expecting an out of band conversation continuation, such as a user following the OAuth flow, completing a form and hitting submit, or a human support agent or other third party sending a message, issue a limited use token to allow calling back into Bots via the generic callback endpoint. The provided token should be a UUID or other unique and random number. By setting it here in the response the Bot will await a reply with that token and use it to thread the message back into the current conversation with that user.
Parameters:
Name Type Description
callbackToken string token generated by you to allow reauthentication back into this conversation. Should be unique, like userId + random. It is ok to reissue the same token for the same conversation.

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.
Parameters:
Name Type Attributes Description
t string <optional>
outcome of component 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.

userId() → {string}

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

variable(name, valueopt)

Source:
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 second form: variable(name, value); writes the value "value" to the variable called "name".
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 (optional)