File size: 1,847 Bytes
2e22a88
 
cf5db11
c3558ea
66d66d2
cf5db11
13bcdcd
66d66d2
 
 
 
 
13bcdcd
 
b865405
c804aa5
 
b865405
2e22a88
b865405
66d66d2
2e22a88
 
cf5db11
66d66d2
 
 
 
c804aa5
66d66d2
c804aa5
66d66d2
c804aa5
66d66d2
 
 
 
 
 
 
 
2e22a88
66d66d2
b851a37
66d66d2
c804aa5
66d66d2
 
 
 
 
 
 
 
 
b851a37
66d66d2
 
 
 
 
 
c804aa5
66d66d2
2e22a88
 
b865405
66d66d2
 
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
import * as express from "express";

const PORT = 7860;
const BOT_USERNAME = "@smolSWE";
const BOT_USER_ID = "6799fc43229849dba9fb3bfa"; // Added BOT_USER_ID

if (!process.env.WEBHOOK_SECRET || !process.env.HF_TOKEN) {
  console.error(
    "This app needs the following env variables to be defined:",
    "WEBHOOK_SECRET, HF_TOKEN"
  );
  process.exit();
}

const app = express();

// Parse HTTP request bodies as JSON
app.use(express.json());

app.get("/", (req, res) => {
  res.json({ hello: "world" });
});

app.post("/", async (req, res) => {
  if (req.header("X-Webhook-Secret") !== process.env.WEBHOOK_SECRET) {
    console.error("Incorrect secret");
    return res.status(400).json({ error: "Incorrect secret" });
  }

  console.log(req.body);

  const event = req.body.event;

  if (
    event.action === "create" &&
    event.scope === "discussion.comment" &&
    req.body.comment.content.includes(BOT_USERNAME) &&
    req.body.comment.author.id !== BOT_USER_ID // Added check for author
  ) {
    // Simple static response instead of AI-generated text
    const continuationText = `Hey, you commented: ${req.body.comment.content}`;

    console.log(continuationText);

    const commentUrl = req.body.discussion.url.api + "/comment";

    try {
      const commentApiResponse = await fetch(commentUrl, {
        method: "POST",
        headers: {
          Authorization: `Bearer ${process.env.HF_TOKEN}`,
          "Content-Type": "application/json",
        },
        body: JSON.stringify({ comment: continuationText }),
      });

      const apiOutput = await commentApiResponse.json();
      console.log(apiOutput);
    } catch (error) {
      console.error("Failed to post comment:", error);
    }
  }

  res.json({ success: true });
});

app.listen(PORT, () => {
  console.debug(`Server started at http://localhost:${PORT}`);
});