-
Notifications
You must be signed in to change notification settings - Fork 275
/
cardFactory.js
244 lines (244 loc) · 8.67 KB
/
cardFactory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @module botbuilder
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
const botframework_schema_1 = require("botframework-schema");
/**
* A set of utility functions designed to assist with the formatting of the various card types a
* bot can return.
*
* @remarks
* All of these functions return an `Attachment` which can be added to an `Activity` directly or
* passed as input to a `MessageFactory` method.
*
* ```javascript
* const card = CardFactory.heroCard(
* 'White T-Shirt',
* ['https://example.com/whiteShirt.jpg'],
* ['buy']
* );
* ```
*/
class CardFactory {
/**
* Returns an attachment for an adaptive card. The attachment will contain the card and the
* appropriate `contentType`.
*
* @remarks
* Adaptive Cards are a new way for bots to send interactive and immersive card content to
* users. For channels that don't yet support Adaptive Cards natively, the Bot Framework will
* down render the card to an image that's been styled to look good on the target channel. For
* channels that support [hero cards](#herocards) you can continue to include Adaptive Card
* actions and they will be sent as buttons along with the rendered version of the card.
*
* For more information about Adaptive Cards and to download the latest SDK, visit
* [adaptivecards.io](http://adaptivecards.io/).
*
* ```JavaScript
* const card = CardFactory.adaptiveCard({
* "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
* "type": "AdaptiveCard",
* "version": "1.0",
* "body": [
* {
* "type": "TextBlock",
* "text": "Default text input"
* }
* ],
* "actions": [
* {
* "type": "Action.Submit",
* "title": "OK"
* }
* ]
* });
* ```
* @param card The adaptive card to return as an attachment.
*/
static adaptiveCard(card) {
return { contentType: CardFactory.contentTypes.adaptiveCard, content: card };
}
/**
* Returns an attachment for an animation card.
*
* @param title The cards title.
* @param media Media URL's for the card.
* @param buttons (Optional) set of buttons to include on the card.
* @param other (Optional) additional properties to include on the card.
*/
static animationCard(title, media, buttons, other) {
return mediaCard(CardFactory.contentTypes.animationCard, title, media, buttons, other);
}
/**
* Returns an attachment for an audio card.
*
* @param title The cards title.
* @param media Media URL's for the card.
* @param buttons (Optional) set of buttons to include on the card.
* @param other (Optional) additional properties to include on the card.
*/
static audioCard(title, media, buttons, other) {
return mediaCard(CardFactory.contentTypes.audioCard, title, media, buttons, other);
}
static heroCard(title, text, images, buttons, other) {
const a = CardFactory.thumbnailCard(title, text, images, buttons, other);
a.contentType = CardFactory.contentTypes.heroCard;
return a;
}
/**
* Returns an attachment for an OAuth card used by the Bot Frameworks Single Sign On (SSO) service.
*
* @param connectionName The name of the OAuth connection to use.
* @param title Title of the cards signin button.
* @param text (Optional) additional text to include on the card.
*/
static oauthCard(connectionName, title, text) {
const card = { buttons: [{ type: botframework_schema_1.ActionTypes.Signin, title: title, value: undefined }], connectionName: connectionName };
if (text) {
card.text = text;
}
return { contentType: CardFactory.contentTypes.oauthCard, content: card };
}
/**
* Returns an attachment for a receipt card. The attachment will contain the card and the
* appropriate `contentType`.
*
* @param card The adaptive card to return as an attachment.
*/
static receiptCard(card) {
return { contentType: CardFactory.contentTypes.receiptCard, content: card };
}
/**
* Returns an attachment for a signin card. For channels that don't natively support signin
* cards an alternative message will be rendered.
*
* @param title Title of the cards signin button.
* @param url The link to the signin page the user needs to visit.
* @param text (Optional) additional text to include on the card.
*/
static signinCard(title, url, text) {
const card = { buttons: [{ type: botframework_schema_1.ActionTypes.Signin, title: title, value: url }] };
if (text) {
card.text = text;
}
return { contentType: CardFactory.contentTypes.signinCard, content: card };
}
static thumbnailCard(title, text, images, buttons, other) {
if (typeof text !== 'string') {
other = buttons;
buttons = images;
images = text;
text = undefined;
}
const card = Object.assign({}, other);
if (title) {
card.title = title;
}
if (text) {
card.text = text;
}
if (images) {
card.images = CardFactory.images(images);
}
if (buttons) {
card.buttons = CardFactory.actions(buttons);
}
return { contentType: CardFactory.contentTypes.thumbnailCard, content: card };
}
/**
* Returns an attachment for a video card.
*
* @param title The cards title.
* @param media Media URLs for the card.
* @param buttons (Optional) set of buttons to include on the card.
* @param other (Optional) additional properties to include on the card.
*/
static videoCard(title, media, buttons, other) {
return mediaCard(CardFactory.contentTypes.videoCard, title, media, buttons, other);
}
/**
* Returns a properly formatted array of actions. Supports converting strings to `messageBack`
* actions (note: using 'imBack' for now as 'messageBack' doesn't work properly in emulator.)
*
* @param actions Array of card actions or strings. Strings will be converted to `messageBack` actions.
*/
static actions(actions) {
const list = [];
(actions || []).forEach((a) => {
if (typeof a === 'object') {
list.push(a);
}
else {
list.push({ type: botframework_schema_1.ActionTypes.ImBack, value: a.toString(), title: a.toString() });
}
});
return list;
}
/**
* Returns a properly formatted array of card images.
*
* @param images Array of card images or strings. Strings will be converted to card images.
*/
static images(images) {
const list = [];
(images || []).forEach((img) => {
if (typeof img === 'object') {
list.push(img);
}
else {
list.push({ url: img });
}
});
return list;
}
/**
* Returns a properly formatted array of media url objects.
*
* @param links Array of media url objects or strings. Strings will be converted to a media url object.
*/
static media(links) {
const list = [];
(links || []).forEach((lnk) => {
if (typeof lnk === 'object') {
list.push(lnk);
}
else {
list.push({ url: lnk });
}
});
return list;
}
}
/** List of content types for each card style. */
CardFactory.contentTypes = {
adaptiveCard: 'application/vnd.microsoft.card.adaptive',
animationCard: 'application/vnd.microsoft.card.animation',
audioCard: 'application/vnd.microsoft.card.audio',
heroCard: 'application/vnd.microsoft.card.hero',
receiptCard: 'application/vnd.microsoft.card.receipt',
oauthCard: 'application/vnd.microsoft.card.oauth',
signinCard: 'application/vnd.microsoft.card.signin',
thumbnailCard: 'application/vnd.microsoft.card.thumbnail',
videoCard: 'application/vnd.microsoft.card.video'
};
exports.CardFactory = CardFactory;
/**
* @private
*/
function mediaCard(contentType, title, media, buttons, other) {
const card = Object.assign({}, other);
if (title) {
card.title = title;
}
card.media = CardFactory.media(media);
if (buttons) {
card.buttons = CardFactory.actions(buttons);
}
return { contentType: contentType, content: card };
}
//# sourceMappingURL=cardFactory.js.map