-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathAdaptiveCardBuilder.ts
135 lines (105 loc) · 3.13 KB
/
AdaptiveCardBuilder.ts
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
import {
AdaptiveCard,
CardElement,
Column,
ColumnSet,
Container,
Image,
OpenUrlAction,
Size,
SizeAndUnit,
SubmitAction,
TextBlock,
TextSize,
TextWeight
} from 'adaptivecards';
import { CardAction } from 'botframework-directlinejs';
export interface BotFrameworkCardAction {
__isBotFrameworkCardAction: boolean;
cardAction: CardAction;
}
function addCardAction(cardAction: CardAction, includesOAuthButtons?: boolean) {
const { type } = cardAction;
let action;
if (
type === 'imBack' ||
type === 'messageBack' ||
type === 'postBack' ||
(type === 'signin' && includesOAuthButtons)
) {
action = new SubmitAction();
action.data = {
__isBotFrameworkCardAction: true,
cardAction
};
action.title = cardAction.title;
} else {
action = new OpenUrlAction();
action.title = cardAction.title;
action.url = cardAction.type === 'call' ? `tel:${cardAction.value}` : cardAction.value;
}
return action;
}
export default class AdaptiveCardBuilder {
card: AdaptiveCard;
container: Container;
constructor(adaptiveCards) {
this.card = new adaptiveCards.AdaptiveCard();
this.container = new Container();
this.card.addItem(this.container);
}
addColumnSet(sizes: number[], container: Container = this.container) {
const columnSet = new ColumnSet();
container.addItem(columnSet);
return sizes.map(size => {
const column = new Column();
column.width = SizeAndUnit.parse(size);
columnSet.addColumn(column);
return column;
});
}
addItems(cardElements: CardElement[], container: Container = this.container) {
cardElements.forEach(cardElement => container.addItem(cardElement));
}
addTextBlock(text: string, template: Partial<TextBlock>, container: Container = this.container) {
if (typeof text !== 'undefined') {
const textblock = new TextBlock();
// tslint:disable-next-line:forin
for (const prop in template) {
textblock[prop] = template[prop];
}
textblock.speak = text;
textblock.text = text;
container.addItem(textblock);
}
}
addButtons(cardActions: CardAction[], includesOAuthButtons?: boolean) {
cardActions &&
cardActions.forEach(cardAction => {
this.card.addAction(addCardAction(cardAction, includesOAuthButtons));
});
}
addCommonHeaders(content: ICommonContent) {
this.addTextBlock(content.title, { size: TextSize.Medium, weight: TextWeight.Bolder });
this.addTextBlock(content.subtitle, { isSubtle: true, wrap: true });
this.addTextBlock(content.text, { wrap: true });
}
addCommon(content: ICommonContent) {
this.addCommonHeaders(content);
this.addButtons(content.buttons);
}
addImage(url: string, container?: Container, selectAction?: CardAction) {
container = container || this.container;
const image = new Image();
image.url = url;
image.selectAction = selectAction && addCardAction(selectAction);
image.size = Size.Stretch;
container.addItem(image);
}
}
export interface ICommonContent {
buttons?: CardAction[];
subtitle?: string;
text?: string;
title?: string;
}