let token = "womwpomwp";
let calendar_id = "wompwomp";
let timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const client = google.accounts.oauth2.initTokenClient({
client_id:
"365940140793-a924128cnq2v8kq6590mee452l3v6jug.apps.googleusercontent.com",
scope: "https://www.googleapis.com/auth/calendar",
callback: (tokenResponse) => {
if (tokenResponse && tokenResponse.access_token) {
token = tokenResponse.access_token;
fetch("/calendar/getCalendarId", {
method: "POST",
body: JSON.stringify({ token: token, timezone: timezone }),
headers: { "Content-type": "application/json" },
})
.then((response) => response.json())
.then((json) => {
calendar_id = json["calendar_id"];
})
.then(() => {
send_request();
});
}
},
});
function handle_form(event) {
event.preventDefault();
send_request();
}
function send_request() {
let input_value = document.forms["chatbox"]["msg-input"].value;
content_output = document.getElementById("content");
fetch("/calendar/execute", {
method: "POST",
body: JSON.stringify({
token: token,
calendar_id: calendar_id,
timezone: timezone,
input: input_value,
}),
headers: { "Content-type": "application/json" },
})
.then((response) => response.json())
.then((json) => {
if (json["output"] != "verify") {
content_output.innerHTML += `>>> ${input_value}
>>> ${json["output"]}
`;
document.forms["chatbox"]["msg-input"].value = "";
} else {
client.requestAccessToken();
}
});
}
document.forms["chatbox"].addEventListener("submit", handle_form);