-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshow.js
More file actions
150 lines (142 loc) · 5.83 KB
/
show.js
File metadata and controls
150 lines (142 loc) · 5.83 KB
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
'use strict';
const BODY_TEMPLATE_3 = 'bodyTemplate3';
const LIST_TEMPLATE_1 = 'listTemplate1';
const BUCKET = '';
module.exports = {
supportsDisplay: function() {
var hasDisplay =
this.event.context &&
this.event.context.System &&
this.event.context.System.device &&
this.event.context.System.device.supportedInterfaces &&
this.event.context.System.device.supportedInterfaces.Display
return hasDisplay;
},
hasPicture: function(data) {
return data.picture;
},
getImageLink: function(data) {
var link = 'https://s3.amazonaws.com/' + BUCKET + '/recipe_pictures/' + data.key + '.jpg';
return link;
},
renderTemplate: function(templateType, data) {
switch(templateType) {
case LIST_TEMPLATE_1:
var listItems = [];
var speechOutput = '';
data.forEach(function(recipe) {
var imageData = {};
if (module.exports.hasPicture(recipe)) {
var imageLink = module.exports.getImageLink(recipe);
imageData = {
'sources': [
{
'url': imageLink
}
]
};
}
if (speechOutput != '') {
speechOutput += ', ';
}
listItems.push({
'image': imageData,
'token': recipe.title,
'textContent': {
'primaryText': {
'text': '<font size="3">' + recipe.title.replace('&', '&') + '</font>',
'type': 'RichText'
},
'secondaryText': {
'text': '<font size="2">' + recipe.caption.replace('&', '&') + '</font>',
'type': 'RichText'
}
}
});
speechOutput += recipe.title.replace('&', ' and ');
});
var response = {
'version': '1.0',
'response': {
'directives': [
{
'type': 'Display.RenderTemplate',
'template': {
'type': 'ListTemplate1',
'token': 'string',
'backButton': 'HIDDEN',
'title': 'List Recipes',
'listItems': listItems,
}
}
],
'outputSpeech': {
'type': 'SSML',
'ssml': '<speak> ' + speechOutput + ' </speak>'
},
'shouldEndSession': true,
'card': {
'type': 'Simple',
'title': 'List Recipes',
'content': speechOutput
}
},
'sessionAttributes': {}
}
this.context.succeed(response);
break;
case BODY_TEMPLATE_3:
var imageData = {};
if (module.exports.hasPicture(data)) {
var imageLink = module.exports.getImageLink(data);
imageData = {
'sources': [
{
'url': imageLink
}
]
};
}
var textContent = '<font size="2">';
data.ingredients.forEach(ingredient => {
textContent += ' - ' + ingredient + '<br/>';
});
textContent += '<br/>' + data.directions + '</font>';
var response = {
'version': '1.0',
'response': {
'directives': [
{
'type': 'Display.RenderTemplate',
'template': {
'type': 'BodyTemplate3',
'token': 'string',
'backButton': 'HIDDEN',
'title': data.title.replace('&', '&'),
'image': imageData,
'textContent': {
'primaryText': {
'type': 'RichText',
'text': textContent
}
}
}
}
],
'outputSpeech': {
'type': 'SSML',
'ssml': '<speak>Here are the details for ' + data.title.replace('&', ' and ') + '</speak>'
},
'shouldEndSession': true,
'card': {
'type': 'Simple',
'title': 'Recipe Description',
'content': textContent
}
},
'sessionAttributes': {}
}
this.context.succeed(response);
}
}
}