-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
151 lines (139 loc) · 5.13 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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>10 Pound hammer</title>
<meta name="viewport" content="width=device-width">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script>
let params = {};
/**
* Following the v2 message API Response
* at https://cloud.ibm.com/apidocs/assistant/assistant-v2#send-user-input-to-assistant,
* we add some items to context.
*/
function getJsonFromUrl(url) {
if(!url) url = location.search;
var query = url.substr(1);
var result = {};
query.split("&").forEach(function(part) {
var item = part.split("=");
result[item[0]] = decodeURIComponent(item[1]);
});
console.log(result);
return result;
}
function preSendhandler(event) {
const { phone_number } = params;
// When fetching the Welcome Node on startup, the context won't be defined, yet. If you want to add to
// context when fetching welcome node, you will need to define the context.
console.log('PresendHandler!', phone_number);
// NOTE: If you are using an actions skill, replace 'main skill' with 'actions skill'.
event.data.context.skills['actions skill'] = event.data.context.skills['actions skill'] || { user_defined: {} };
event.data.context.skills['actions skill'].user_defined.user_phone_number = phone_number;
console.log('preSend', event);
// You can OPTIONALLY return a promise and we will wait to continue processing until the promise is resolved. If
// you return nothing we will immediately continue processing the event. If you fail the Promise we will cancel
// sending the message.
/*
return new Promise(function(resolve) {
myAsyncThing.then(function(moreData) {
// Do some other manipulation of event.data...
resolve();
});
});
*/
}
function customResponseHandler(event) {
const type = event.type;
const location = `37.7925,-122.4008`
const locationSquare = `37.751691%2C-122.466768,37.810709%2C-122.409761`
const message = JSON.stringify(event.data.message);
const element = event.data.element;
const responseType = event.data.message?.user_defined?.command
const activity=event.data.message?.user_defined?.activity
console.log(`CustomResponse SCOTT activity: ${activity}`, event);
if (/running/i.test(activity)) {
var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer 5316852709bd9a34e6fafe5cd1b5aa7422a92ad1");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
console.log('RECEIVED DATA', event.data);
return fetch(`https://www.strava.com/api/v3/segments/explore?bounds=${locationSquare}&activity_type=running`, requestOptions)
.then(response => response.json())
.then(result => {
console.log("Segments...", result);
element.innerHTML = result.segments.filter((segment, index) => index < 5).reduce((final, current) => {
return final + `<div class="ibm-web-chat--card">
<h4>${current.name}</h4>
<p> Distance: ${current.distance} meters</p>
<p> Average Grade: ${current.avg_grade}%</div>
`
},'');
})
.catch(error => console.log('error', error));
element.innerHTML = `<div>${error}</div>`
}
/* if (/eating/i.test(activity)) {
const token = 'AIzaSyA9ENiujZzoQC8jXmTpESYjrGyIPD8lFbc'
const URL=`https://www.google.com/maps/embed/v1/search?key=${token}¢er=${location}&q=restaurants`
element.innerHTML = `<iframe
width="450"
height="250"
frameborder="0" style="border:0"
src=${URL}>
</iframe>`
}
*/
}
function preReceive(event) {
console.log('pre:receive', event);
}
window.watsonAssistantChatOptions = {
integrationID: "8583606b-4f88-4943-bbc0-3b7a67516b08", // The ID of this integration.
region: "us-south", // The region your integration is hosted in.
serviceInstanceID: "852ba3ea-4f22-4377-83f2-3c41b3c7424f", // The ID of your service instance.
pageLinkConfig: {
linkIDs: {
'channel_transfer': {
text: 'channel_transfer'
},
'flight_status': {
text: 'flight status'
},
}
},
onLoad: function(instance) {
params = getJsonFromUrl();
if (params.phone_number) {
instance.updateUserID(params.phone_number);
}
instance.on({ type: "pre:send", handler: preSendhandler });
instance.on({ type: "pre:receive", handler: preReceive });
instance.on({ type: "customResponse", handler: customResponseHandler });
instance.render();
},
openChatByDefault: true,
debug: true
};
setTimeout(function(){
const t=document.createElement('script');
t.src="https://web-chat.global.assistant.watson.appdomain.cloud/loadWatsonAssistantChat.js";
document.head.appendChild(t);
});
</script>
</head>
<body>
<div class="wrapper">
<header>
<h1>10 Pound Hammer</h1>
<p></p>
</div>
</body>
</html>