/
api2.html
124 lines (102 loc) · 4.13 KB
/
api2.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Voicebot api test</title>
<link rel="icon" href="i/favicon.png" type="image/x-icon" />
<script type="text/javascript" src="https://apis.google.com/js/api.js"></script>
<script type="text/javascript">
/*
urls:
https://cloud.google.com/dialogflow-enterprise/docs/reference/rest/v2/projects.agent.sessions/detectIntent
https://developers.google.com/identity/protocols/OAuth2UserAgent
https://console.cloud.google.com/apis/credentials?project=jaanusbot-v2
todo: fulfillment fix (math calculator)
*/
var GoogleAuth;
var SCOPE = 'https://www.googleapis.com/auth/dialogflow';
document.addEventListener("DOMContentLoaded", function(event) {
console.log("loaded");
// init client begin
gapi.load('client:auth2', function() {
gapi.client.init({
'apiKey': 'AIzaSyDb7zyMT5m3SYnXAGpK6ni4Jryfzhcp5OU',
'clientId': '150540862694-engp2c2vvd23c6mbeot0ivg8k15utr87.apps.googleusercontent.com',
'scope': SCOPE
}).then(function() {
GoogleAuth = gapi.auth2.getAuthInstance();
// Listen for sign-in state changes.
GoogleAuth.isSignedIn.listen(updateSigninStatus);
// Handle initial sign-in state. (Determine if user is already signed in.)
var user = GoogleAuth.currentUser.get();
setSigninStatus();
});
});
// init client end
document.querySelector("#sign-in-or-out-button").addEventListener("click", handleAuthClick);
document.querySelector("#revoke-access-button").addEventListener("click", revokeAccess);
document.querySelector("#perform-request-button").addEventListener("click", function(ev) {
console.log("clicked request button");
gapi.client.request({
path: 'https://dialogflow.googleapis.com/v2/projects/jaanusbot-v2/agent/sessions/deadbeef:detectIntent',
method: "POST",
body: {
"queryInput": {
"text": {
"text": "why did the chicken cross the road",
"languageCode": "en"
}
}
}
}).then(function(response) {
console.log("onFulfilled", response);
}, function(error) {
console.log("onRejected", error);
});
});
function handleAuthClick() {
if (GoogleAuth.isSignedIn.get()) {
// User is authorized and has clicked 'Sign out' button.
console.log("signed in. signing out now.");
GoogleAuth.signOut();
} else {
// User is not signed in. Start Google auth flow.
console.log("not signed in. signing in.");
GoogleAuth.signIn();
}
}
function revokeAccess() {
GoogleAuth.disconnect();
}
function setSigninStatus(isSignedIn) {
var user = GoogleAuth.currentUser.get();
var isAuthorized = user.hasGrantedScopes(SCOPE);
if (isAuthorized) {
console.log("user authorized, updating UI to reflect that");
document.querySelector('#sign-in-or-out-button').innerHTML = 'Sign out';
document.querySelector('#revoke-access-button').style.display = 'inline-block';
document.querySelector('#perform-request-button').style.display = 'inline-block';
document.querySelector('#auth-status').innerHTML = ('You are currently signed in and have granted access to this app.');
} else {
console.log("user not authorized, updating UI to reflect that");
document.querySelector('#sign-in-or-out-button').innerHTML = ('Sign In/Authorize');
document.querySelector('#revoke-access-button').style.display = 'none';
document.querySelector('#perform-request-button').style.display = 'none';
document.querySelector('#auth-status').innerHTML = ('You have not authorized this app or you are signed out.');
}
}
function updateSigninStatus(isSignedIn) {
setSigninStatus();
}
});
</script>
<meta name="viewport" content="width=device-width" />
</head>
<body>
<p class="main">wat.</p>
<button id="sign-in-or-out-button">Sign In/Authorize</button>
<button id="revoke-access-button" style="display: none">Revoke access</button>
<button id="perform-request-button" style="display: none">Perform Dialogflow request</button>
<div id="auth-status" style="display: inline"></div>
</body>
</html>