/
index.html
246 lines (215 loc) · 7.75 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="/images/BotServices.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<title>Web Chat Upload to Azure Storage Demo</title>
<script src="https://cdn.botframework.com/botframework-webchat/4.4.1/webchat.js"></script>
<script
src="https://unpkg.com/@azure/storage-blob@10.3.0/browser/azure-storage.blob.min.js"
integrity="sha384-fsfhtLyVQo3L3Bh73qgQoRR328xEeXnRGdoi53kjo1uectCfAHFfavrBBN2Nkbdf"
crossorigin="anonymous"
></script>
<!-- This styling is for Web Chat demonstration purposes -->
<!-- It is recommended that stlye is moved to seperate file for organization in larger projects -->
<style type="text/css">
html,
body {
background-color: #F1F0F0;
height: 100%;
}
.css-eycyw2 {
background-color: #FAF9F8;
}
.main{
background-color: #F1F0F0;
margin: 15px;
}
.css-eycyw2 .main {
border-top: none;
}
.css-1vieo9r {
background-color: #F1F0F0;
}
.css-o3xlyv {
background-color: #F1F0F0;
font-family: Segoe UI;
font-size: 14px;
line-height: 20px;
}
.from-user .css-o3xlyv {
color: #F1F0F0;
background-color: #0078D4;
}
body {
background-image: url(/images/BotServices-Translucent.svg);
background-position: 100% 100%;
background-repeat: no-repeat;
background-size: auto 50%;
margin: 0;
}
#chatwindow {
border-radius: 4px;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);
height: calc(100% - 20px);
left: 10px;
overflow: hidden;
position: fixed;
top: 10px;
width: 368px;
}
#heading {
background-color: #0078D4;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
height: 48px;
left: 10px;
overflow: hidden;
position: fixed;
top: 10px;
width: 368px;
}
.css-1fyy09s{
background-color: rgb(255, 255, 255);
}
h1 {
display: table-cell;
vertical-align: middle;
padding: 11px 0px 0px 11px;
color:#F1F0F0;
font-family: Segoe UI;
font-size: 18px;
line-height: 21px;
font-weight: 500;
}
#webchat {
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
height: calc(100% - 70px);
left: 10px;
overflow: hidden;
position: fixed;
top: 60px;
width: 368px;
}
</style>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="chatwindow">
<div id="heading">
<h1>Contoso Bot</h1>
</div>
<div id="webchat">
</div>
</div>
<script>
// This is a helper function for fetching JSON resources.
var locString = "Browser does not support geolocation."
async function fetchJSON(url, options = {}) {
const res = await fetch(url, {
...options,
headers: {
...options.headers,
accept: 'application/json'
}
});
if (!res.ok) {
throw new Error(`Failed to fetch JSON due to ${res.status}`);
}
return await res.json();
}
// Loading @azure/storage-blob bundle.
const { Aborter, AnonymousCredential, BlockBlobURL, StorageURL, uploadBrowserDataToBlockBlob } = window.azblob;
function showPosition(position) {
locString = position.coords.latitude + " " + position.coords.longitude;
}
function erroring(err){
alert(err);
}
// Upload a file to Azure Storage by supplying an Object URL and a name.
async function uploadFile({ name, url }) {
// Today, Web Chat uses createObjectURL() to represent the blob.
// https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
// Because it is not trivial to GC the URL, we might change this behavior to use ArrayBuffer or Blob in later versions of Web Chat.
const res = await fetch(url);
if (!res.ok) {
throw new Error('Cannot obtain ArrayBuffer to uploading file');
}
// Fetch a SAS token from the server, think of it as a permission to upload a single file to Azure Storage.
const { sasQuery, url: uploadURL } = await fetchJSON('/api/azurestorage/uploadsastoken');
// Uploads the file to Azure Storage directly by using a fresh SAS token from the server.
const pipeline = StorageURL.newPipeline(new AnonymousCredential());
const blockBlobURL = new BlockBlobURL(`${uploadURL}?${sasQuery}`, pipeline);
// Using @azure/storage-blob bundle to upload, this function is parallelized for huge files.
const result = await uploadBrowserDataToBlockBlob(Aborter.none, await res.arrayBuffer(), blockBlobURL, {
metadata: { name }
});
// Returns the blob URL.
return uploadURL;
}
(async function main() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, erroring);
} else {
alert("Geolocation is not supported by this browser.");
}
const store = WebChat.createStore({}, ({ dispatch }) => next => action => {
const { type } = action;
// Triggers bot with initial message, in order to have greeting message render on load.
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'startConversation',
type: 'event',
value: {text: "hello"}
}
});
dispatch({
type: 'WEB_CHAT/SEND_MESSAGE_BACK',
payload: {
text : locString
}
});
return next(action);
// When Web Chat wants to send files, we intercept this action and send a message instead of an event with an attachment.
} else if (type === 'WEB_CHAT/SEND_FILES') {
(async function() {
// Tells the bot that you are uploading files. This is optional.
dispatch({ type: 'WEB_CHAT/SEND_TYPING' });
const files = await Promise.all(action.payload.files.map(({ name, url }) => uploadFile({ name, url })));
// In order for Power VA to process a file, we need to pass the blob URL as a String within a message.
// We intercept the Event and rather than sending it as an attachment we are converting the URL to a string
// and dispatching a message.
dispatch({
type: 'WEB_CHAT/SEND_MESSAGE_BACK',
payload: {
text : files[0]
}
});
})().catch(err => console.error(err));
} else {
return next(action);
}
});
// This is for obtaining Direct Line token from the bot.
const { token } = await fetchJSON('/api/directline/token');
// Makes connection to V2 Engine through direct line, with the defined actions and parameters
WebChat.renderWebChat(
{
directLine: WebChat.createDirectLine({ token }),
store,
styleOptions: {
backgroundColor: 'rgba(255, 255, 255, .8)'
}
},
document.getElementById('webchat')
);
})().catch(err => console.error(err));
</script>
<div id="root"></div>
</body>
</html>