forked from microsoft/BotFramework-WebChat
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
270 lines (226 loc) · 7.63 KB
/
index.html
File metadata and controls
270 lines (226 loc) · 7.63 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
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
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Web Chat: Automated test harness</title>
<script>
!(function () {
'use strict';
const { console } = window;
const log = (window.__console__ = []);
const push = (type, ...args) => {
log.push([type, args.join(' ')]);
console[type].apply(console, args);
};
window.console = {
...console,
debug: push.bind(null, 'debug'),
error: push.bind(null, 'error'),
info: push.bind(null, 'info'),
log: push.bind(null, 'log'),
warn: push.bind(null, 'warn')
};
window.addEventListener('error', ({ colno, error, filename, lineno, message }) => {
push(
'error',
JSON.stringify({
colno,
error,
filename,
lineno,
message
})
);
});
})();
</script>
<!--
For demonstration purposes, we are using the latest official release of Web Chat at "/latest/webchat.js". When you are using Web Chat for production, you may lock down on a specific version with the following format: "/4.1.0/webchat.js".
-->
<style>
html,
body {
height: 100%;
}
body {
margin: 0;
}
input,
textarea,
[contenteditable] {
caret-color: Transparent;
}
#manualStart {
background-color: Transparent;
border: 0;
height: 100%;
position: absolute;
top: 0;
width: 100%;
}
#webchat {
height: 100%;
width: 100%;
}
</style>
<script crossorigin="anonymous" src="https://corinagum.github.io/BotFramework-Offline-MockBot/index.js"></script>
<script
crossorigin="anonymous"
src="https://unpkg.com/event-target-shim@5.0.1/dist/event-target-shim.umd.js"
></script>
<script crossorigin="anonymous" src="https://unpkg.com/simple-update-in?browser"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.development.js"></script>
<script crossorigin="anonymous" src="/createProduceConsumeBroker.js"></script>
<script crossorigin="anonymous" src="/mockWebSpeech.js"></script>
</head>
<body>
<button id="manualStart">Tap to load Web Chat</button>
<div id="webchat" role="main"></div>
<script>
function unmarshal({ __evalKeys, ...obj } = {}) {
__evalKeys &&
__evalKeys.forEach(key => {
obj[key] = eval(obj[key])();
});
return obj;
}
async function runHook(name, args) {
return new Promise((resolve, reject) => {
window.WebChatTest.store.dispatch({
type: 'DIRECT_LINE/INCOMING_ACTIVITY',
payload: {
activity: {
from: { id: 'bot' },
id: Math.random().toString(36).substr(2, 10),
name: 'hook',
type: 'event',
value: { args, name, reject, resolve }
}
}
});
});
}
function loadScript(src) {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.setAttribute('async', '');
script.setAttribute('src', src);
script.addEventListener('load', resolve);
script.addEventListener('error', ({ error }) => reject(error));
document.body.appendChild(script);
});
}
async function retry(fn, retries) {
let lastErr;
for (; retries >= 0; retries--) {
try {
return await fn();
} catch (err) {
lastErr = err;
}
}
throw lastErr;
}
const PASSTHRU_MIDDLEWARE = store => next => action => next(action);
async function main(options) {
document.querySelector('#manualStart').remove();
let {
createDirectLine,
createStyleSet,
disableNoMagicCode,
props,
setup,
storeInitialState = {},
storeMiddleware = PASSTHRU_MIDDLEWARE,
useProductionBot
} = unmarshal(options);
props = unmarshal(props);
if (setup) {
await setup();
}
await loadScript('/webchat.js');
let directLine;
if (!useProductionBot && !createDirectLine) {
directLine = window.MockBotAdapter.createDirectLine({});
} else {
const { token } = await retry(async () => {
try {
const res = await fetch('https://hawo-mockbot4-token-app.blueriver-ce85e8f0.westus.azurecontainerapps.io/api/token/directline', {
method: 'POST',
timeout: 2000
});
return await res.json();
} catch (err) {
console.error('Failed to fetch Direct Line token from Mockbot.');
console.error(err);
throw err;
}
}, 3);
createDirectLine || (createDirectLine = window.WebChat.createDirectLine);
directLine = (createDirectLine || window.WebChat.createDirectLine)({ token });
}
if (disableNoMagicCode) {
directLine.getSessionId = undefined;
}
const store = (window.WebChatTest.store = window.WebChat.createStore(storeInitialState, store => {
const setupMiddleware = storeMiddleware(store);
return next => {
const processAction = setupMiddleware(next);
return action => {
window.WebChatTest.actions.push(action);
return processAction(action);
};
};
}));
const activityMiddleware = () => next => card => {
const { activity } = card;
if (activity.type === 'event' && activity.name === 'hook') {
return children =>
React.createElement(() => {
const { args, name, reject, resolve } = activity.value;
try {
const hookFn = window.WebChat.hooks[name];
if (!hookFn) {
console.log(
`No hooks named "${name}" were found. Valid hooks are:`,
Object.keys(window.WebChat.hooks)
.sort()
.map(name => `- ${name}`)
.join('\n')
);
throw new Error(`No hooks named "${name}" were found.`);
}
resolve(hookFn(args));
} catch (err) {
reject(err);
}
return false;
});
}
return next(card);
};
props = {
activityMiddleware,
directLine,
store,
styleSet: createStyleSet && createStyleSet(props.styleOptions),
username: 'Happy Web Chat user',
...props
};
renderWebChat = props => window.WebChat.renderWebChat(props, document.getElementById('webchat'));
renderWebChat(props);
document.querySelector('#webchat > *').focus();
window.WebChatTest.updateProps = mergeProps => {
props = { ...props, ...mergeProps };
renderWebChat(props);
};
}
document.querySelector('#manualStart').addEventListener('click', () => main());
window.WebChatTest = {
actions: [],
loadScript,
runHook
};
</script>
</body>
</html>