MessageModel

Lib~ MessageModel

The Bots MessageModel is a class for creating and validating a message structure based on the Conversation Message Model (CMM), representing a message to or from a bot. This class is used by the Bots Custom Components Conversation SDK, and can also be used independently of the SDK.

This class can be used in a server side Nodejs environment, or in the browser.

A MessageModel class instance can be instantiated using the constructor taking the payload that represents the message. The payload is then parsed and validated.

Constructor

new MessageModel(payload)

Source:
To create a MessageModel object using a javascript object representing the conversation message, or a string for plain text message.

The object is of Conversation Message Model (CMM) message type such as Text, Card, Attachment, Location, Postback, or Raw type. This message object may be created using the static methods in this class. Or the message object may be received from a sender, and a MessageModel can then be created to validate the message object.

The payload will be validated. If it is a valid message, messagePayload() will return the valid message object. If not, the message content can be retrieved via payload().

To support older message format, the object can also be of the 'choice' type.
Parameters:
Name Type Description
payload string | object The payload to be parsed into a MessageModel object

Methods

(static) addChannelExtensions(message, channel, extensions) → {object}

Source:
Static method to add channel extensions to a payload object.
Parameters:
Name Type Description
message object The message to add channel extensions to.
channel string The channel type ('facebook', 'webhook', etc) to set extensions on.
extensions object The channel-specific extensions to be added.
Returns:
A ConversationMessage with channel extensions.
Type
object

(static) addGlobalActions(message, globalActions) → {object}

Source:
Static method to add global actions to a payload object.
Parameters:
Name Type Description
message object The message to add global actions to.
globalActions object The global actions to be added.
Returns:
A ConversationMessage with global actions.
Type
object

(static) attachmentConversationMessage(type, url, actionsopt, footerTextopt) → {object}

Source:
Static method to create an AttachmentConversationMessage
Parameters:
Name Type Attributes Description
type string type of attachment - file, image, video or audio.
url string the url of the attachment.
actions Array.<object> <optional>
A list of actions for the attachmentConversationMessage.
footerText string <optional>
The footerText to be added at the bottom of the message.
Returns:
An AttachmentConversationMessage.
Type
object

(static) callActionObject(labelopt, imageUrlopt, phoneNumber) → {object}

Source:
Static method to create a call Action. A label or an imageUrl is required.
Parameters:
Name Type Attributes Description
label string <optional>
label of the action.
imageUrl string <optional>
image to show for the action.
phoneNumber string phoneNumber to call if action is taken.
Returns:
A callActionObject.
Type
object

(static) cardConversationMessage(layoutopt, cards, actionsopt, footerTextopt) → {object}

Source:
Static method to create a CardConversationMessage.
Parameters:
Name Type Attributes Description
layout string <optional>
'vertical' or 'horizontal'. Whether to display the cards horizontally or vertically. Default is vertical.
cards Array.<object> The list of cards to be rendered.
actions Array.<object> <optional>
A list of actions for the cardConversationMessage.
footerText string <optional>
The footerText to be added at the bottom of the message.
Returns:
A CardConversationMessage.
Type
object

(static) cardObject(title, descriptionopt, imageUrlopt, urlopt, actionsopt) → {object}

Source:
Static method to create a card object for CardConversationMessage.
Parameters:
Name Type Attributes Description
title string The title of the card.
description string <optional>
The description of the card.
imageUrl string <optional>
URL of the image.
url string <optional>
URL for a hyperlink of the card.
actions Array.<object> <optional>
A list of actions available for this card.
Returns:
A Card.
Type
object

(static) locationActionObject(labelopt, imageUrlopt) → {object}

Source:
Static method to create a location Action. A label or an imageUrl is required.
Parameters:
Name Type Attributes Description
label string <optional>
label of the action.
imageUrl string <optional>
image to show for the action.
Returns:
A locationActionObject.
Type
object

(static) locationConversationMessage(latitude, longitude, titleopt, urlopt, actionsopt) → {object}

Source:
Static method to create a LocationConversationMessage.
Parameters:
Name Type Attributes Description
latitude number The latitude.
longitude number The longitude.
title string <optional>
The title for the location.
url string <optional>
A url for displaying a map of the location.
actions Array.<object> <optional>
A list of actions for the locationConversationMessage.
Returns:
A LocationConversationMessage.
Type
object

(static) postbackActionObject(labelopt, imageUrlopt, postback, keywordsopt, skipAutoNumberopt) → {object}

Source:
Static method to create a postback Action. A label or an imageUrl is required.
Parameters:
Name Type Attributes Description
label string <optional>
label of the action.
imageUrl string <optional>
image to show for the action.
postback object | string object or string to send as postback if action is taken.
keywords Array.<string> <optional>
array of keywords that can be used to trigger the postback action.
skipAutoNumber boolean <optional>
Boolean flag that can be used to exclude a postback action from auto-numbering. Only applicable when 'autoNumberPostbackActions' context variable or 'autoNumberPostbackActions' component property is set to true.
Returns:
A postbackActionObject.
Type
object

(static) postbackConversationMessage(postback, labelopt, actionsopt) → {object}

Source:
Static method to create a postackConversationMessage
Parameters:
Name Type Attributes Description
postback object | string object or string to send as postback.
label string <optional>
The label associated with the postback.
actions Array.<object> <optional>
A list of actions for the postbackConversationMessage.
Returns:
A PostbackConversationMessage.
Type
object

(static) rawConversationMessage(payload) → {object}

Source:
Static method to create a RawConversationMessage.
Parameters:
Name Type Description
payload object The raw (channel-specific) payload,
Returns:
A RawConversationMessage.
Type
object

(static) shareActionObject(labelopt, imageUrlopt) → {object}

Source:
Static method to create a share Action. A label or an imageUrl is required.
Parameters:
Name Type Attributes Description
label string <optional>
label of the action.
imageUrl string <optional>
image to show for the action.
Returns:
A shareActionObject.
Type
object

(static) textConversationMessage(text, actionsopt, footerTextopt) → {object}

Source:
Static method to create a TextConversationMessage.
Parameters:
Name Type Attributes Description
text string The text of the message payload.
actions Array.<object> <optional>
A list of actions related to the text.
footerText string <optional>
The footerText to be added at the bottom of the message.
Returns:
A TextConversationMessage.
Type
object

(static) urlActionObject(labelopt, imageUrlopt, url) → {object}

Source:
Static method to create a url Action. A label or an imageUrl is required.
Parameters:
Name Type Attributes Description
label string <optional>
label of the action.
imageUrl string <optional>
image to show for the action.
url string url to open if action is taken.
Returns:
A urlActionObject.
Type
object

(static) validateConversationMessage(payload) → {boolean|object}

Source:
Static method to validate a common ConversationMessage
Parameters:
Name Type Description
payload object The payload object to be verified
Returns:
true if valid; return Validation Error object (error & value) if invalid
Type
boolean | object

isValid() → {boolean}

Source:
returns if the instance contains a valid message according to Conversation Message Model (CMM)
Returns:
if the message conforms to Conversation Message Model (CMM)
Type
boolean

messagePayload() → {object}

Source:
Retrieves the validated common message model payload.
Returns:
The common message model payload
Type
object

rawPayload() → {object}

Source:
If messagePayload() returns null or if isValid() is false, this method can be used to retrieve the payload that could not be converted to a Conversation Message Model (CMM) payload.
Returns:
The payload which may not comply to Conversation Message Model (CMM)
Type
object

validationError() → {object}

Source:
Retrieves the validation error messages, if any. Use if messagePayload() returns null or isValid() is false, signifying validation errors.
Returns:
The validation error encountered when converting the payload to the Conversation Message Model (CMM). The validation error object is produced by joi.
Type
object