-
Notifications
You must be signed in to change notification settings - Fork 255
/
format-message.js
231 lines (182 loc) · 5.24 KB
/
format-message.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
'use strict';
class SkypeMessage {
constructor() {
this.template = {};
this.template.attachments = [];
}
get() {
return this.template;
}
}
class Text extends SkypeMessage {
constructor(text, format) {
super();
if (!text || typeof text !== 'string')
throw new Error('Text is required for Skype Text template');
this.template = {
type: 'message',
text: text,
textFormat: format || 'plain'
};
}
}
class Photo extends SkypeMessage {
constructor(base64Photo) {
super();
if (!base64Photo || typeof base64Photo !== 'string')
throw new Error('Photo is required for the Skype Photo template');
this.template = {
type: 'message',
attachments: [{
contentType: 'image/png',
contentUrl: base64Photo
}]
};
}
}
class Carousel extends SkypeMessage {
constructor(summary, text) {
super();
this.template = {
type: 'message',
attachmentLayout: 'carousel',
summary: summary || '',
text: text || '',
attachments: []
};
return this;
}
getCurrentAttachment() {
let current = this.template.attachments.length - 1;
if (current < 0) {
throw new Error('You need to add attachment to Carousel');
}
return current;
}
addHero(images) {
if(images && !Array.isArray(images)) {
throw new Error('Images should be sent as array for the Skype Hero template');
}
this.template.attachments.push({
contentType: 'application/vnd.microsoft.card.hero',
content: {
title: '',
subtitle: '',
text: '',
images: images ? images.map(image => ({url: image, alt: ''})) : [],
buttons: []
}
});
return this;
}
addThumbnail(images) {
if(images && !Array.isArray(images)) {
throw new Error('Images should be sent as array for the Skype Thumbnail template');
}
this.template.attachments.push({
contentType: 'application/vnd.microsoft.card.thumbnail',
content: {
title: '',
subtitle: '',
text: '',
images: images ? images.map(image => ({url: image, alt: ''})) : [],
buttons: []
}
});
return this;
}
addReceipt(total, tax, vat) {
this.template.attachments.push({
contentType: 'application/vnd.microsoft.card.receipt',
content: {
title: '',
subtitle: '',
text: '',
total: total || '',
tax: tax || '',
vat: vat || '',
items: [],
facts: [],
buttons: []
}
});
return this;
}
addFact(key, value) {
let currentAttachment = this.getCurrentAttachment();
this.template.attachments[currentAttachment].content.facts.push({
key: key || '',
value: value || ''
});
return this;
}
addItem(title, subtitle, text, price, quantity, image) {
let currentAttachment = this.getCurrentAttachment();
this.template.attachments[currentAttachment].content.items.push({
title: title || '',
subtitle: subtitle || '',
text: text || '',
price: price || '',
quantity: quantity || '',
image: {
url: image || ''
}
});
return this;
}
addTitle(title) {
let currentAttachment = this.getCurrentAttachment();
if (!title || typeof title !== 'string')
throw new Error('Title needs to be a string for Skype addTitle method');
this.template.attachments[currentAttachment].content.title = title;
return this;
}
addSubtitle(subtitle) {
let currentAttachment = this.getCurrentAttachment();
if (!subtitle || typeof subtitle !== 'string')
throw new Error('Subtitle needs to be a string for Skype addSubtitle method');
this.template.attachments[currentAttachment].content.subtitle = subtitle;
return this;
}
addText(text) {
let currentAttachment = this.getCurrentAttachment();
if (!text || typeof text !== 'string')
throw new Error('Text needs to be a string for Skype addText method');
this.template.attachments[currentAttachment].content.text = text;
return this;
}
addButton(title, value, type) {
let currentAttachment = this.getCurrentAttachment();
if (!title || typeof title !== 'string')
throw new Error('Title needs to be a string for Skype addButton method');
if (!value || typeof value !== 'string')
throw new Error('Value needs to be a string for Skype addButton method');
if (!type || typeof type !== 'string')
throw new Error('Type needs to be a string for Skype addButton method');
let validTypes = ['openUrl', 'imBack', 'postBack', 'playAudio', 'playVideo', 'showImage', 'downloadFile', 'signin'];
if (validTypes.indexOf(type) == -1)
throw new Error('Type needs to be a valid type string for Skype addButton method');
this.template.attachments[currentAttachment].content.buttons.push({
type: type,
title: title,
value: value
});
return this;
}
}
class Typing extends SkypeMessage {
constructor() {
super();
this.template = {
type: 'typing'
};
return this.template;
}
}
//TODO: investigate how to send Hero, Thumbnail and Receipt without carousel
module.exports = {
Text: Text,
Photo: Photo,
Carousel: Carousel,
Typing: Typing
};