smolSWE commited on
Commit
66d66d2
·
verified ·
1 Parent(s): c804aa5

Update server.ts

Browse files
Files changed (1) hide show
  1. server.ts +41 -39
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
- console.error(
8
- "This app needs the following env variables to be defined:",
9
- "WEBHOOK_SECRET, HF_TOKEN"
10
- );
11
- process.exit();
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
- res.json({ hello: "world" });
21
  });
22
 
23
  app.post("/", async (req, res) => {
24
- if (req.header("X-Webhook-Secret") !== process.env.WEBHOOK_SECRET) {
25
- console.error("Incorrect secret");
26
- return res.status(400).json({ error: "Incorrect secret" });
27
- }
28
 
29
- console.log(req.body);
30
 
31
- const event = req.body.event;
32
 
33
- if (
34
- event.action === "create" &&
35
- event.scope === "discussion.comment" &&
36
- req.body.comment.content.includes(BOT_USERNAME)
37
- ) {
38
- // Simple static response instead of AI-generated text
39
- const continuationText = `You mentioned ${BOT_USERNAME}. Here's a helpful response! ${req.body.comment.content}`;
 
40
 
41
- console.log(continuationText);
42
 
43
- const commentUrl = req.body.discussion.url.api + "/comment";
44
 
45
- try {
46
- const commentApiResponse = await fetch(commentUrl, {
47
- method: "POST",
48
- headers: {
49
- Authorization: `Bearer ${process.env.HF_TOKEN}`,
50
- "Content-Type": "application/json",
51
- },
52
- body: JSON.stringify({ comment: continuationText }),
53
- });
54
 
55
- const apiOutput = await commentApiResponse.json();
56
- console.log(apiOutput);
57
- } catch (error) {
58
- console.error("Failed to post comment:", error);
59
- }
60
- }
61
 
62
- res.json({ success: true });
63
  });
64
 
65
  app.listen(PORT, () => {
66
- console.debug(`Server started at http://localhost:${PORT}`);
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
+ });