-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
161 lines (151 loc) · 5.13 KB
/
index.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
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
const defaultLocale = 'en-US';
function requestChatBot(loc) {
const params = new URLSearchParams(location.search);
const oReq = new XMLHttpRequest();
oReq.addEventListener("load", initBotConversation);
var path = "/chatBot?locale=" + extractLocale(params.get('locale'));
if (loc) {
path += "&lat=" + loc.lat + "&long=" + loc.long;
}
if (params.has('userId')) {
path += "&userId=" + params.get('userId');
}
if (params.has('userName')) {
path += "&userName=" + params.get('userName');
}
oReq.open("POST", path);
oReq.send();
}
function extractLocale(localeParam) {
if (!localeParam) {
return defaultLocale;
}
else if (localeParam === 'autodetect') {
return navigator.language;
}
else {
return localeParam;
}
}
function chatRequested() {
const params = new URLSearchParams(location.search);
if (params.has('shareLocation')) {
getUserLocation(requestChatBot);
}
else {
requestChatBot();
}
}
function getUserLocation(callback) {
navigator.geolocation.getCurrentPosition(
function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var location = {
lat: latitude,
long: longitude
}
callback(location);
},
function(error) {
// user declined to share location
console.log("location error:" + error.message);
callback();
});
}
function initBotConversation() {
if (this.status >= 400) {
alert(this.statusText);
return;
}
// extract the data from the JWT
const jsonWebToken = this.response;
const tokenPayload = JSON.parse(atob(jsonWebToken.split('.')[1]));
const user = {
id: tokenPayload.userId,
name: tokenPayload.userName,
locale: tokenPayload.locale
};
let domain = undefined;
if (tokenPayload.directLineURI) {
domain = "https://" + tokenPayload.directLineURI + "/v3/directline";
}
let location = undefined;
if (tokenPayload.location) {
location = tokenPayload.location;
} else {
// set default location if desired
location = {
lat: 44.86448450671394,
long: -93.32597021107624
}
}
var botConnection = window.WebChat.createDirectLine({
token: tokenPayload.connectorToken,
domain: domain
});
const styleOptions = {
botAvatarImage: 'https://docs.microsoft.com/en-us/azure/bot-service/v4sdk/media/logo_bot.svg?view=azure-bot-service-4.0',
// botAvatarInitials: '',
// userAvatarImage: '',
hideSendBox: false, /* set to true to hide the send box from the view */
botAvatarInitials: 'Bot',
userAvatarInitials: 'You',
backgroundColor: '#F8F8F8'
};
const store = window.WebChat.createStore({}, function(store) { return function(next) { return function(action) {
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
store.dispatch({
type: 'DIRECT_LINE/POST_ACTIVITY',
meta: {method: 'keyboard'},
payload: {
activity: {
type: "invoke",
name: "InitConversation",
locale: user.locale,
value: {
// must use for authenticated conversation.
jsonWebToken: jsonWebToken,
// Use the following activity to proactively invoke a bot scenario
/*
triggeredScenario: {
trigger: "{scenario_id}",
args: {
location: location,
myVar1: "{custom_arg_1}",
myVar2: "{custom_arg_2}"
}
}
*/
}
}
}
});
}
else if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
if (action.payload && action.payload.activity && action.payload.activity.type === "event" && action.payload.activity.name === "ShareLocationEvent") {
// share
getUserLocation(function (location) {
store.dispatch({
type: 'WEB_CHAT/SEND_POST_BACK',
payload: { value: JSON.stringify(location) }
});
});
}
}
return next(action);
}}});
const webchatOptions = {
directLine: botConnection,
styleOptions: styleOptions,
store: store,
userID: user.id,
username: user.name,
locale: user.locale
};
startChat(user, webchatOptions);
}
function startChat(user, webchatOptions) {
const botContainer = document.getElementById('webchat');
window.WebChat.renderWebChat(webchatOptions, botContainer);
}