-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaceholder.js
99 lines (95 loc) · 3.57 KB
/
placeholder.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
function sendMessage() {
temporaryMsgId += 1;
let tempID = `temp_${temporaryMsgId}`;
let hasFile = !!$(".upload-attachment").val();
const inputValue = $.trim(messageInput.val());
if (inputValue.length > 0 || hasFile) {
const formData = new FormData($("#message-form")[0]);
formData.append("id", getMessengerId());
formData.append("temporaryMsgId", tempID);
formData.append("_token", csrfToken);
// New AJAX call to OpenAI API
$.ajax({
url: 'lance-flow.vercel.app/gregorian-sarcasm', // Replace with your Flask server endpoint
method: 'POST',
data: JSON.stringify({
message: inputValue
}),
contentType: 'application/json', // Set content type to 'application/json'
dataType: 'JSON',
timeout: 10000, // Set a timeout of 10 seconds
success: (data) => {
// Replace the original message with the sarcastic version
formData.set('message', data.response); // Replace 'response' with the actual key in the response
// Existing AJAX call
$.ajax({
url: $("#message-form").attr("action"),
method: "POST",
data: formData,
dataType: "JSON",
processData: false,
contentType: false,
beforeSend: () => {
$(".messages").find(".message-hint").hide();
// append a temporary message card
if (hasFile) {
messagesContainer
.find(".messages")
.append(
sendTempMessageCard(
inputValue + "\n" + loadingSVG("28px"),
tempID
)
);
} else {
messagesContainer
.find(".messages")
.append(sendTempMessageCard(inputValue, tempID));
}
// scroll to bottom
scrollToBottom(messagesContainer);
messageInput.css({ height: "42px" });
// form reset and focus
$("#message-form").trigger("reset");
cancelAttachment();
messageInput.focus();
},
success: (data) => {
if (data.error > 0) {
// message card error status
errorMessageCard(tempID);
console.error(data.error_msg);
} else {
// update contact item
updateContactItem(getMessengerId());
// temporary message card
const tempMsgCardElement = messagesContainer.find(
`.message-card[data-id=${data.tempID}]`
);
// add the message card coming from the server before the temp-card
tempMsgCardElement.before(data.message);
// then, remove the temporary message card
tempMsgCardElement.remove();
// scroll to bottom
scrollToBottom(messagesContainer);
// send contact item updates
sendContactItemUpdates(true);
}
},
error: () => {
// message card error status
errorMessageCard(tempID);
// error log
console.error(
"Failed sending the message! Please, check your server response."
);
},
});
},
error: () => {
console.error('Failed to get sarcastic message from Flask server');
}
});
}
return false;
}