Spaces:
Sleeping
Sleeping
Update server.ts
Browse files
server.ts
CHANGED
|
@@ -2,13 +2,14 @@ import * as express from "express";
|
|
| 2 |
|
| 3 |
const PORT = 7860;
|
| 4 |
const BOT_USERNAME = "@smolSWE";
|
|
|
|
| 5 |
|
| 6 |
if (!process.env.WEBHOOK_SECRET || !process.env.HF_TOKEN) {
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
}
|
| 13 |
|
| 14 |
const app = express();
|
|
@@ -17,51 +18,52 @@ const app = express();
|
|
| 17 |
app.use(express.json());
|
| 18 |
|
| 19 |
app.get("/", (req, res) => {
|
| 20 |
-
|
| 21 |
});
|
| 22 |
|
| 23 |
app.post("/", async (req, res) => {
|
| 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 |
app.listen(PORT, () => {
|
| 66 |
-
|
| 67 |
-
});
|
|
|
|
| 2 |
|
| 3 |
const PORT = 7860;
|
| 4 |
const BOT_USERNAME = "@smolSWE";
|
| 5 |
+
const BOT_USER_ID = "6799fc43229849dba9fb3bfa"; // Added BOT_USER_ID
|
| 6 |
|
| 7 |
if (!process.env.WEBHOOK_SECRET || !process.env.HF_TOKEN) {
|
| 8 |
+
console.error(
|
| 9 |
+
"This app needs the following env variables to be defined:",
|
| 10 |
+
"WEBHOOK_SECRET, HF_TOKEN"
|
| 11 |
+
);
|
| 12 |
+
process.exit();
|
| 13 |
}
|
| 14 |
|
| 15 |
const app = express();
|
|
|
|
| 18 |
app.use(express.json());
|
| 19 |
|
| 20 |
app.get("/", (req, res) => {
|
| 21 |
+
res.json({ hello: "world" });
|
| 22 |
});
|
| 23 |
|
| 24 |
app.post("/", async (req, res) => {
|
| 25 |
+
if (req.header("X-Webhook-Secret") !== process.env.WEBHOOK_SECRET) {
|
| 26 |
+
console.error("Incorrect secret");
|
| 27 |
+
return res.status(400).json({ error: "Incorrect secret" });
|
| 28 |
+
}
|
| 29 |
|
| 30 |
+
console.log(req.body);
|
| 31 |
|
| 32 |
+
const event = req.body.event;
|
| 33 |
|
| 34 |
+
if (
|
| 35 |
+
event.action === "create" &&
|
| 36 |
+
event.scope === "discussion.comment" &&
|
| 37 |
+
req.body.comment.content.includes(BOT_USERNAME) &&
|
| 38 |
+
req.body.comment.author.id !== BOT_USER_ID // Added check for author
|
| 39 |
+
) {
|
| 40 |
+
// Simple static response instead of AI-generated text
|
| 41 |
+
const continuationText = `Hey, you commented: ${req.body.comment.content}`;
|
| 42 |
|
| 43 |
+
console.log(continuationText);
|
| 44 |
|
| 45 |
+
const commentUrl = req.body.discussion.url.api + "/comment";
|
| 46 |
|
| 47 |
+
try {
|
| 48 |
+
const commentApiResponse = await fetch(commentUrl, {
|
| 49 |
+
method: "POST",
|
| 50 |
+
headers: {
|
| 51 |
+
Authorization: `Bearer ${process.env.HF_TOKEN}`,
|
| 52 |
+
"Content-Type": "application/json",
|
| 53 |
+
},
|
| 54 |
+
body: JSON.stringify({ comment: continuationText }),
|
| 55 |
+
});
|
| 56 |
|
| 57 |
+
const apiOutput = await commentApiResponse.json();
|
| 58 |
+
console.log(apiOutput);
|
| 59 |
+
} catch (error) {
|
| 60 |
+
console.error("Failed to post comment:", error);
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
|
| 64 |
+
res.json({ success: true });
|
| 65 |
});
|
| 66 |
|
| 67 |
app.listen(PORT, () => {
|
| 68 |
+
console.debug(`Server started at http://localhost:${PORT}`);
|
| 69 |
+
});
|