Spaces:
Running
Running
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}`); | |
}); |