-
Notifications
You must be signed in to change notification settings - Fork 18
/
characterai_server.js
56 lines (49 loc) · 1.51 KB
/
characterai_server.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
const express = require("express");
const app = express();
const port = 3000;
const CharacterAI = require("node_characterai");
const characterAI = new CharacterAI();
chat = null;
app.get("/", (req, res) => {
res.send("Hello World!");
});
app.post("/authenticate", async (req, res) => {
try {
await characterAI.authenticateAsGuest();
res.status(200).send("Authentication successful");
} catch (error) {
console.log(error);
res.status(500).send("Error occurred during authentication");
}
});
app.post("/authenticateToken", async (req, res) => {
try {
if (characterAI.isAuthenticated()) characterAI.unauthenticate();
await characterAI.authenticateWithToken(req.query.token);
res.status(200).send("Authentication successful");
} catch (error) {
console.log(error);
res.status(500).send("Error occurred during authentication");
}
});
app.post("/setCharacter", async (req, res) => {
try {
chat = await characterAI.createOrContinueChat(req.query.characterId);
res.status(200).send("Chat creation successful");
} catch (error) {
console.log(error);
res.status(500).send("Error occurred during chat creation");
}
});
app.post("/sendChat", async (req, res) => {
try {
const response = await chat.sendAndAwaitResponse(req.query.text, true);
res.status(200).send(response);
} catch (error) {
console.log(error);
res.status(500).send("Error sending chat");
}
});
app.listen(port, () => {
console.log(`character_ai wrapper listening on port ${port}`);
});