MessageModel

MessageModel

The Bots MessageModel is a utility class that helps creating and validating a message structure representing a bot message. This utility is used by the Bots Custom Components Conversation SDK, and can also be used independently of the SDK. This utility can be used in a server side Nodejs environment, or in the browser. When used in Nodejs, require the module 'joi' use it to initialize the class. When used in browser, require the module 'joi-browser' instead. A MessageModel class instance can be instantiated using the constructor taking the payload that represents the message. The payload is then parsed and validated. The payload can be created using the various static utility methods for creating different response types including TextConversationMessage, CardConversationMessage, AttachmentConversationMessage, etc.

Constructor

new MessageModel(payload)

Source:
To create a MessageModel object using a string or an object representing the message. The object is one of a known Common Message Model message type such as Text, Card, Attachment, Location, Postback, Agent or Raw type. This object can be created using the static methods in this class. To support older message format, the object can also be of the 'choice' type or text. The input will be parsed. If it is a valid message, messagePayload() will return the valid message object. If not, the message content can be retrieved via payload().
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 utility method to add channel extensions to a payload
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 utility method to add global actions to a payload
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) → {object}

Source:
Static utility 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.
Returns:
An AttachmentConversationMessage.
Type
object

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

Source:
Static utility 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) → {object}

Source:
Static utility 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.
Returns:
A CardConversationMessage.
Type
object

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

Source:
Static utility 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 utility 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 utility 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) → {object}

Source:
Static utility 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.
Returns:
A postbackActionObject.
Type
object

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

Source:
Static utility 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 utility method to create a RawConversationMessage
Parameters:
Name Type Description
payload object The raw (channel-specific) payload to send.
Returns:
A RawConversationMessage.
Type
object

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

Source:
Static utility 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) → {object}

Source:
Static utility 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.
Returns:
A TextConversationMessage.
Type
object

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

Source:
Static utility 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 utility 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
Returns:
if the message is valid common message model.
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 payload.
Returns:
The payload which may not comply to Conversation Message Model
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(s) encountered when converting the payload to the Conversation Message Model.
Type
object