-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (51 loc) · 1.62 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
const writeNewMessage = (text, isUser = false) => {
let newMessage = document.createElement("p");
newMessage.textContent = `${isUser ? "Tu" : "Nunzio"}: ${text}`;
document.querySelector(".chat").appendChild(newMessage);
};
let state = null;
fetch("https://engine.memori.ai/memori/v2/session", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
memoriId: "1afe57c6-1b69-4a61-96ea-52bf7b8d158e",
}),
})
.then((response) => response.json())
.then((result) => {
state = result;
console.log("state", state);
if (state.currentState.emission)
writeNewMessage(state.currentState.emission);
document.getElementById("question").focus();
})
.catch((error) => console.log("error", error));
document.getElementById("go").addEventListener("click", (event) => {
const submittedText = document.getElementById("question").value ?? "";
writeNewMessage(submittedText, true);
console.log("session", state, state.sessionID);
fetch(
`https://engine.memori.ai/memori/v2/TextEnteredEvent/${state.sessionID}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
text: submittedText,
}),
}
)
.then((response) => response.json())
.then((result) => {
state = result;
console.log("state", state);
if (state.currentState.emission)
writeNewMessage(state.currentState.emission);
document.getElementById("question").value = "";
document.getElementById("question").focus();
})
.catch((error) => console.log("error", error));
});