-
Notifications
You must be signed in to change notification settings - Fork 476
/
parent.html
339 lines (302 loc) · 12.2 KB
/
parent.html
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=">
<title>Chatbot UI iFrame Example</title>
<!-- bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid"><!-- main container -->
<h1>Lex Chatbot UI Sample Parent Page</h1>
<p class="lead">
Use the chatbot UI to drive your bot. This page will dynamically
update using the associated Lex variables as the bot dialog progresses.
</p>
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">Send "Buy Flowers" utterance to iframe</div>
<div class="panel-body">
<div>
<button id="send-intent" type="button" class="btn btn-default" disabled>
Send
</button>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">Lex Dialog State</div>
<div class="panel-body">
<div>
<strong>dialogState:</strong>
<span id="dialog-state"></span>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">Lex Slots</div>
<div class="panel-body">
<div id="slots"></div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">Lex Intent Name</div>
<div class="panel-body">
<div>
<strong>Intent Name:</strong>
<span id="intent-name"></span>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">Lex Session Attributes</div>
<div class="panel-body">
<div>
<pre id="session-attributes"></pre>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">Lex Response Card</div>
<div class="panel-body">
<div>
<pre id="response-card"></pre>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-warning">
<div class="panel-heading">NOTE</div>
<div class="panel-body">
<p>
This application was created using the
<a href="https://github.com/awslabs/aws-lex-web-ui">aws-lex-web-ui</a>
project.
</p>
</div>
</div>
</div>
</div>
</div><!-- main container -->
<!-- jquery used as part on this example - not required by lex-web-ui -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<!-- bootstrap used as part of thie example - not required by lex-web-ui -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!--
Loader script tag
Add a script tag pointing to the loader library towards the bottom
of the html BODY section
The library and its dependencies can be hosted on another site,
S3 bucket or a CDN (e.g. CloudFront)
This script creates a global variable named ChatbotUiLoader which
provides the loader library functionality
Webpack injects the loader script tag here.
if you want to include it manually in your app,
replace the webpack tag with:
<script src="./lex-web-ui-loader.min.js"></script>
-->
<%= htmlWebpackPlugin.tags.bodyTags %>
<!--
After the loader script tag has been included, you can use its global
variable to load the chatbot UI in an iframe
-->
<script>
// The loader constructor supports various configurable options used to
// control how the component configuration and dependencies are retrieved.
var loaderOpts = {
// Point the baseUrl option to the the base URL used to download
// the chatbot UI files and dependencies.
// In this example, the base URL is relative to the root path
// You can also use full URLs:
// baseUrl: 'https://iframe-host.example.org/lex-web-ui/'
baseUrl: '/',
// enable receiving chatbot UI config from event
// the event is used as an alternative to passing the chatbot
// UI via JSON file or via the parameter in the load() function
shouldLoadConfigFromEvent: true,
// Controls if it should load minimized production dependecies
// defaults to true for production builds and false in development
shouldLoadMinDeps: true,
}
// ChatBotUiLoader contains the IframeLoader constructor
// Instantiate the loader and pass the loader options
var iframeLoader = new ChatBotUiLoader.IframeLoader(loaderOpts);
// You can override the chatbot UI config by passing a parameter
var chatbotUiconfig = {
// if the iframe page is hosted on a different site,
// you should configure the parentOrigin and iframeOrigin
/*
ui: {
// origin of the parent site where you are including the chatbot UI
parentOrigin: 'https://mysite.example.org'
},
iframe: {
// origin hosting the HTML file that will be embedded in the iframe
iframeOrigin: 'https://iframe-host.example.org'
},
lex: {
sessionAttributes: {
userAgent: navigator.userAgent,
QNAClientFilter: ''
}
}
*/
};
// load the iframe
iframeLoader.load(chatbotUiconfig)
.then(function () {
// at this point the API is available
// so I can send a ping to the iframe
iframeLoader.api.ping();
// perform actions on the parent dependent on the chatbot
// loading. For example, enable the send button once the
// chatbot UI is loaded
$('#send-intent').prop('disabled', false);
})
.catch(function (error) {
console.error('chatbot UI failed to load', error);
});
// Sample function illustrating dynamic messaging with the iframe using
// the API. This function is called from the event handler of the "Send"
// button.
function sendUtterance(utterance) {
// function to determine if the iframe is minimized
function isBotMinimized() {
// look at the options of the loader to determine the iframe
// element ID
var elementId = iframeLoader.options.elementId;
// this class is present in the iframe when minimized
var minimizedClass = 'lex-web-ui-iframe--minimize';
return $('#' + elementId).hasClass(minimizedClass);
}
return Promise.resolve()
.then(function () {
// use the toggleMinimizeUi API if the iframe is minimized
return !isBotMinimized() || iframeLoader.api.toggleMinimizeUi();
})
.then(function () {
return iframeLoader.api.postText(utterance);
})
.then(function () { console.log('message succesfully sent'); })
.catch(function (err) { console.error('error sending message ', err); });
}
</script>
<!--
This script illustrates how to handle the various events supported by the
chatbot UI.
-->
<script>
$(document).ready(function chatbotHandler() {
// When the chatbot ui iframe is ready to receive the
// dynamic config it sends the 'receivelexconfig' event to the parent
// For example, you can send dynamic config/parameters
// (e.g. username, geolocation) to the chatbot ui from here
$(document).one('receivelexconfig', function onReceiveLexConfig() {
var localTimeZone;
try {
localTimeZone = JSON.stringify(
Intl.DateTimeFormat().resolvedOptions().timeZone
);
} catch (err) {
localTimeZone = JSON.stringify(
new Date().getTimezoneOffset()
)
}
// sample config passing the local timezone in a sessionAttribute
var config = {
lex: {
sessionAttributes: {
localTimeZone: JSON.stringify(
Intl.DateTimeFormat().resolvedOptions().timeZone
)
}
}
};
// emit bot config event to send the dynamic config to the
// chatbot UI
// jquery can't trigger native events so use vanilla JS CustomEvent
var event = new CustomEvent('loadlexconfig', { detail: { config: config } });
document.dispatchEvent(event);
});
// Once the chatbot UI is ready, it sends a 'lexWebUiReady' event
$(document).on('lexWebUiReady', function onUpdateLexState(evt) {
// We are just sending a ping request here as an example
// This example uses an event instead of calling
// iframeLoader.api.ping() to show the asynchronous
// event API alternative
var event = new CustomEvent(
'lexWebUiMessage',
{ detail: {message: {event: 'ping'} } }
);
document.dispatchEvent(event);
});
// setup Send button handler
$('#send-intent').on('click', function(event) {
event.preventDefault();
sendUtterance('Buy Flowers');
});
// bot update event handler
// In this example, the parent page is updated based on the state
$(document).on('updatelexstate', function onUpdateLexState(evt) {
var slots = {};
var dialogState = {};
var intentName = '';
var sessionAttributes = {};
var responseCard = {};
if (evt && ('detail' in evt) && evt.detail && ('state' in evt.detail)) {
slots = evt.detail.state.slots;
dialogState = evt.detail.state.dialogState;
intentName = evt.detail.state.intentName || intentName;
sessionAttributes = evt.detail.state.sessionAttributes || sessionAttributes;
responseCard = evt.detail.state.responseCard || responseCard;
}
if (!slots || !dialogState) {
console.warn('updatelexstate event is missing slot or dialogState field');
return;
}
$('#dialog-state').text(dialogState);
$('#intent-name').text(intentName);
$('#session-attributes').text(JSON.stringify(sessionAttributes, null, 2));
$('#response-card').text(JSON.stringify(responseCard, null, 2));
var $slotsContainerReplacement = $('<div>', { id: 'slots' });
Object.keys(slots).forEach(function updateOrder(slotName, index) {
var slotValue = JSON.stringify(slots[slotName]);
var $slotDiv = $('<div>', { id: 'slot-' + index });
var $slotName = $('<strong>').text(slotName + ': ');
var $slotValue = $('<span>').text(slotValue);
$slotDiv.append($slotName);
$slotDiv.append($slotValue);
$slotsContainerReplacement.append($slotDiv);
});
$('#slots').replaceWith($slotsContainerReplacement);
});
});
</script>
</body>
</html>