MessageFactory class
A set of utility functions to assist with the formatting of the various message types a bot can return.
Remarks
The following example shows sending a message containing a single hero card:
const { MessageFactory, CardFactory } = require('botbuilder');
const card = CardFactory.heroCard(
'White T-Shirt',
['https://example.com/whiteShirt.jpg'],
['buy']
);
const message = MessageFactory.attachment(card);
await context.sendActivity(message);
Methods
attachment(Attachment, string, string, Input |
Returns a single message activity containing an attachment. |
carousel(Attachment[], string, string, Input |
Returns a message that will display a set of attachments using a carousel layout. |
content |
Returns a message that will display a single image or video to a user. |
list(Attachment[], string, string, Input |
Returns a message that will display a set of attachments in list form. |
suggested |
Returns a message that includes a set of suggested actions and optional text. |
text(string, string, Input |
Returns a simple text message. |
Method Details
attachment(Attachment, string, string, InputHints | string)
Returns a single message activity containing an attachment.
static function attachment(attachment: Attachment, text?: string, speak?: string, inputHint?: InputHints | string): Partial<Activity>
Parameters
- attachment
-
Attachment
Adaptive card to include in the message.
- text
-
string
(Optional) text of the message.
- speak
-
string
(Optional) SSML to include with the message.
- inputHint
-
InputHints | string
(Optional) input hint for the message. Defaults to acceptingInput
.
Returns
Partial<Activity>
A message activity containing the attachment.
Remarks
This example shows creating a message with a hero card attachment:
const message = MessageFactory.attachment(
CardFactory.heroCard(
'White T-Shirt',
['https://example.com/whiteShirt.jpg'],
['buy']
)
);
carousel(Attachment[], string, string, InputHints | string)
Returns a message that will display a set of attachments using a carousel layout.
static function carousel(attachments: Attachment[], text?: string, speak?: string, inputHint?: InputHints | string): Partial<Activity>
Parameters
- attachments
-
Attachment[]
Array of attachments to include in the message.
- text
-
string
(Optional) text of the message.
- speak
-
string
(Optional) SSML to include with the message.
- inputHint
-
InputHints | string
(Optional) input hint for the message.
Returns
Partial<Activity>
A message activity that will display a set of attachments using a carousel layout.
Remarks
This example shows creating a message with a carousel of hero cards:
const message = MessageFactory.carousel([
CardFactory.heroCard('title1', ['imageUrl1'], ['button1']),
CardFactory.heroCard('title2', ['imageUrl2'], ['button2']),
CardFactory.heroCard('title3', ['imageUrl3'], ['button3'])
]);
contentUrl(string, string, string, string, string, InputHints | string)
Returns a message that will display a single image or video to a user.
static function contentUrl(url: string, contentType: string, name?: string, text?: string, speak?: string, inputHint?: InputHints | string): Partial<Activity>
Parameters
- url
-
string
Url of the image/video to send.
- contentType
-
string
The MIME type of the image/video.
- name
-
string
(Optional) Name of the image/video file.
- text
-
string
(Optional) text of the message.
- speak
-
string
(Optional) SSML to include with the message.
- inputHint
-
InputHints | string
(Optional) input hint for the message.
Returns
Partial<Activity>
A message activity that will display a single image or video to a user.
Remarks
This example shows sending an image to the user:
const message = MessageFactory.contentUrl('https://example.com/hawaii.jpg', 'image/jpeg', 'Hawaii Trip', 'A photo from our family vacation.');
list(Attachment[], string, string, InputHints | string)
Returns a message that will display a set of attachments in list form.
static function list(attachments: Attachment[], text?: string, speak?: string, inputHint?: InputHints | string): Partial<Activity>
Parameters
- attachments
-
Attachment[]
Array of attachments to include in the message.
- text
-
string
(Optional) text of the message.
- speak
-
string
(Optional) SSML to include with the message.
- inputHint
-
InputHints | string
(Optional) input hint for the message.
Returns
Partial<Activity>
A message activity that will display a set of attachments in list form.
Remarks
This example shows creating a message with a list of hero cards:
const message = MessageFactory.list([
CardFactory.heroCard('title1', ['imageUrl1'], ['button1']),
CardFactory.heroCard('title2', ['imageUrl2'], ['button2']),
CardFactory.heroCard('title3', ['imageUrl3'], ['button3'])
]);
suggestedActions(string | CardAction[], string, string, InputHints | string)
Returns a message that includes a set of suggested actions and optional text.
static function suggestedActions(actions: string | CardAction[], text?: string, speak?: string, inputHint?: InputHints | string): Partial<Activity>
Parameters
- actions
-
string | CardAction[]
Array of card actions or strings to include. Strings will be converted to messageBack
actions.
- text
-
string
(Optional) text of the message.
- speak
-
string
(Optional) SSML to include with the message.
- inputHint
-
InputHints | string
(Optional) input hint for the message. Defaults to acceptingInput
.
Returns
Partial<Activity>
A message activity that contains the suggested actions.
Remarks
This example shows creating a message with suggested actions:
const message = MessageFactory.suggestedActions(['red', 'green', 'blue'], `Choose a color`);
text(string, string, InputHints | string)
Returns a simple text message.
static function text(text: string, speak?: string, inputHint?: InputHints | string): Partial<Activity>
Parameters
- text
-
string
Text to include in the message.
- speak
-
string
(Optional) SSML to include in the message.
- inputHint
-
InputHints | string
(Optional) input hint for the message. Defaults to acceptingInput
.
Returns
Partial<Activity>
A message activity containing the text.
Remarks
This example shows sending a simple text message:
const message = MessageFactory.text('Greetings from example message');