-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.js
executable file
·193 lines (183 loc) · 6.07 KB
/
chatbot.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
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
$(document).ready(function() {
var form = document.getElementById("chat-box");
var submit = document.getElementById("chat-submit");
var input = document.getElementById("chat-user");
var container = document.getElementById("dialogue-container");
var reply;
var ids = 0;
var height=0;
var AWSconfig = {
accessKey: "",
secretKey: "",
S3Bucket: "",
region: "us-east-1",
sessionToken: "",
client_id: "", // removed on purpose
client_secret: "", // removed on purpose
user_pool_id: "", // removed on purpose
cognito_domain_url:
"https://restaurant-suggestions.auth.us-east-1.amazoncognito.com",
redirect_uri: "https://s3.amazonaws.com/chatbot-assignment-2/index.html",
identity_pool_id: "*****" // Identity pool ID
};
let apigClient = {};
var getParameterByName = function(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results[2]) return "";
if (!results) return null;
return decodeURIComponent(results[2].replace(/\+/g, " "));
};
//console.log("Code = " + getParameterByName("code"));
var exchangeAuthCodeForCredentials = function({
auth_code = getParameterByName("code"),
client_id = AWSconfig.client_id,
client_secret = AWSconfig.client_secret,
identity_pool_id = AWSconfig.identity_pool_id,
aws_region = AWSconfig.region,
user_pool_id = AWSconfig.user_pool_id,
cognito_domain_url = AWSconfig.cognito_domain_url,
redirect_uri = AWSconfig.redirect_uri
}) {
return new Promise((resolve, reject) => {
var settings = {
url: `${cognito_domain_url}/oauth2/token`,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Access-Control-Allow-Origin": "*",
Authorization: "Basic " + btoa(client_id + ":" + client_secret)
},
data: {
grant_type: "authorization_code",
client_id: client_id,
client_secret: client_secret,
redirect_uri: redirect_uri,
code: auth_code
}
};
$.ajax(settings).done(function(response) {
//console.log("OAuth2 Token Call Responded");
//console.log(response);
if (response.id_token) {
AWS.config.region = aws_region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: identity_pool_id,
Logins: {
[`cognito-idp.${aws_region}.amazonaws.com/${user_pool_id}`]: response.id_token
}
});
// console.log({
// IdentityPoolId: identity_pool_id,
// Logins: {
// [`cognito-idp.${aws_region}.amazonaws.com/${user_pool_id}`]: response.id_token
// }
// });
AWS.config.credentials.refresh(function(error) {
//console.log("Error", error);
if (error) {
reject(error);
} else {
//console.log("Successfully Logged In");
resolve(AWS.config.credentials);
}
});
} else {
reject(response);
}
});
});
};
exchangeAuthCodeForCredentials({
auth_code: getParameterByName("code"),
client_id: AWSconfig.client_id,
client_secret: AWSconfig.client_secret,
identity_pool_id: AWSconfig.identity_pool_id,
aws_region: AWSconfig.region,
user_pool_id: AWSconfig.user_pool_id,
cognito_domain_url: AWSconfig.cognito_domain_url,
redirect_uri: AWSconfig.redirect_uri
})
.then(function(response) {
//console.log("Inside Then Function", response);
apigClient = apigClientFactory.newClient({
accessKey: response.accessKeyId,
secretKey: response.secretAccessKey,
sessionToken: response.sessionToken,
region: "us-east-1",
apiKey: "***"
});
apigClient.accessKey = response.accessKeyId;
apigClient.secretKey = response.secretAccessKey;
apigClient.sessionToken = response.sessionToken;
})
.catch(function(error) {
console.log("error = " + this.error);
console.log("response = " + this.response);
});
AWS.config.region = "us-east-1"; // Region
//console.log("Access Key", apigClient.accessKey);
//console.log("secretKey ", apigClient.secretKey);
//console.log("Final Console Log");
form.addEventListener("submit", function(event) {
event.preventDefault();
create_bubble_user();
});
function create_bubble_user() {
if (input.value != "") {
var div = document.createElement("DIV");
var para = document.createElement("SPAN");
var txt = document.createTextNode(input.value);
div.setAttribute("class", "dialogue dialogue-user");
para.appendChild(txt);
div.appendChild(para);
container.appendChild(div);
get_request();
input.value = "";
checkHeight();
}
}
function got_reply() {
create_bubble_bot(reply);
}
function checkHeight(){
$('#dialogue-container').each(function(i, value){
height += parseInt($(this).height());
});
$('div').animate({scrollTop: height});
}
function create_bubble_bot(str) {
var div = document.createElement("DIV");
var para = document.createElement("SPAN");
var txt = document.createTextNode(str);
div.setAttribute("class", "dialogue dialogue-bot");
para.appendChild(txt);
div.appendChild(para);
container.appendChild(div);
checkHeight();
}
function get_request() {
var body = {
params: {
message: input.value,
userId: "12345"
}
};
// console.log("Access Key", apigClient.accessKey);
// console.log("secretKey ", apigClient.secretKey);
apigClient
.chatbotPost({}, body)
.then(function(result) {
// Add success callback code here.
reply = result.data.body["response"];
console.log(result.data);
got_reply();
})
.catch(function(result) {
// Add error callback code here.
console.log("Error:", result);
});
}
});