-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgoogle.js
124 lines (102 loc) · 4.2 KB
/
google.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
const DefaultResponseBuilder = require('./default');
const richResponseAppenderBySource = {
'apiai': (googleApp, finalResponse, richResponse, respItem) => {
if ((typeof respItem.response === 'string' || respItem.response instanceof String)) {
richResponse.addSimpleResponse(respItem.response);
} else if ((respItem.response.type === 'simple_response') || (respItem.response.speech)) {
richResponse.addSimpleResponse(respItem.response.textToSpeech || respItem.response.speech);
} else if (respItem.response.type === 'suggestion_chips') {
richResponse.suggestions = respItem.response.suggestions;
} else if (respItem.response.type === 'basic_card') {
var cardBuild = googleApp.buildBasicCard(respItem.response.formattedText)
.setTitle(respItem.response.title)
.setImage(respItem.response.image.url, 'Image');
if ((respItem.response.buttons) && (respItem.response.buttons.length > 0)) {
for (var i = 0; i < respItem.response.buttons.length; i++) {
var buttonItem = respItem.response.buttons[i];
cardBuild = cardBuild.addButton(buttonItem.title, ((buttonItem.openUrlAction) && (buttonItem.openUrlAction.url)) ? buttonItem.openUrlAction.url : null);
}
}
richResponse.addBasicCard(cardBuild);
}
},
'abbott': (googleApp, finalResponse, richResponse, respItem) => {
if ((typeof respItem.response === 'string' || respItem.response instanceof String)) {
richResponse.addSimpleResponse(respItem.response);
} else {
if (respItem.response.richResponse) {
if (respItem.response.richResponse.items) {
richResponse.items = respItem.response.richResponse.items;
}
}
if (((respItem.response.carousel) && (respItem.response.carousel.items)) ||
((respItem.response.list) && (respItem.response.list.items))) {
var buildObj = null;
var arrayItems = [];
if (respItem.response.carousel) {
buildObj = googleApp.buildCarousel();
arrayItems = respItem.response.carousel.items;
} else if (respItem.response.list) {
buildObj = googleApp.buildList(respItem.response.list.text || null);
arrayItems = respItem.response.list.items;
}
for (let i = 0; i < arrayItems.length; i++) {
let item = arrayItems[i];
let optionItem = googleApp.buildOptionItem(item.id || 'ITEM_' + i, []);
if (item.title) {
optionItem.setTitle(item.title);
}
if (item.description) {
optionItem.setDescription(item.description);
}
if (item.image) {
optionItem.setImage(item.image.url, item.image.accessibilityText || 'Image');
}
buildObj.addItems(optionItem);
}
if (respItem.response.carousel) {
finalResponse.carousel = buildObj;
} else if (respItem.response.list) {
finalResponse.list = buildObj;
}
}
}
}
};
module.exports = class extends DefaultResponseBuilder {
get googleApp() {
return this.intentFlowHandler.bot.googleApp;
}
get responseFormattersBySource() {
return richResponseAppenderBySource;
}
constructor(intentFlowHandler) {
super(intentFlowHandler);
}
_appendItemToRichResponse(finalResponse, richResponse, respItem) {
this.responseFormattersBySource[respItem.source](this.googleApp, finalResponse, richResponse, respItem);
}
build(responseMessage) {
var finalResponse = null;
var responseConvo = null;
var richResponse = this.googleApp.buildRichResponse();
finalResponse = {
};
if (responseMessage instanceof Array) {
responseMessage.forEach((respItem) => {
this._appendItemToRichResponse(finalResponse, richResponse, respItem);
responseConvo = respItem.convo || null;
});
} else {
this._appendItemToRichResponse(finalResponse, richResponse, responseMessage);
responseConvo = responseMessage.convo || null;
}
if ((richResponse.items) && (richResponse.items.length > 0)) {
finalResponse.richResponse = richResponse;
}
return {
convo: responseConvo,
response: finalResponse
};
}
};