Compare commits

..

7 Commits

Author SHA1 Message Date
1a151b197b this is so nigger rigged dude I really should just use a class or
something
2025-07-03 11:39:57 -04:00
70180c5d5f muh dik 2025-07-03 10:42:39 -04:00
dac037809c loggign 2025-07-02 06:42:22 -04:00
6088a2cbd3 add keepalive ping, onclose event reason 2025-07-02 06:41:43 -04:00
ed8d148d0a update README 2025-07-01 17:18:53 -04:00
379099dc7a remove unused code, add try/catch block 2025-07-01 15:30:33 -04:00
c0ed38ac1a update README 2025-07-01 15:26:38 -04:00
2 changed files with 50 additions and 50 deletions

View File

@ -1,15 +1,15 @@
## Pleroma -> Ollama Bot Setup
1. Clone project
2. Install npm 22.11.0 if you don't have it already
2. Install Node `v22.11.0` if you don't have it already
* If using `nvm`, just `nvm install 22.11.0` and then `nvm use 22.11.0` if necessary
3. `cd` into the project directory
4. Run `npm install`
6. Run `npx prisma migrate dev --name init`
7. To run the software on a cronjob, use `npm run once`
8. To run continuously, use `npm run ws`
7. To start, run `npm run start`
I recommend using `screen` to run this in the background until a `systemd` service can be created. I just haven't bothered to do it yet.
### Database Migrations
If you add stuff to the schema, follow the [Prisma development workflow](https://www.prisma.io/docs/orm/prisma-migrate/workflows/development-and-production). This will apply the new schema to the database and generate a new Prisma client with type safety.
Setting as a system service will come at some point, or someone could contribute if they wanted.

View File

@ -8,31 +8,10 @@ import {
import striptags from "striptags";
import { PrismaClient } from "../generated/prisma/client.js";
import { createWebsocket } from "./websocket.js";
import { WebSocket } from "ws";
const prisma = new PrismaClient();
// const getNotifications = async () => {
// try {
// const request = await fetch(
// `${process.env.PLEROMA_INSTANCE_URL}/api/v1/notifications?types[]=mention`,
// {
// method: "GET",
// headers: {
// Authorization: `Bearer ${process.env.INSTANCE_BEARER_TOKEN}`,
// },
// }
// );
// const notifications: Notification[] = await request.json();
// return notifications;
// } catch (error: any) {
// throw new Error(error.message);
// }
// };
// const notifications = await getNotifications();
const storeUserData = async (notification: Notification): Promise<void> => {
try {
await prisma.user.upsert({
@ -173,7 +152,24 @@ const postReplyToStatus = async (
}
};
const ws = createWebsocket();
let ws = createWebsocket();
const reconnect = (ws: WebSocket) => {
if (ws) {
ws.close();
}
return createWebsocket();
};
ws.on("close", () => {
for (let i = 0; i < 5; i++) {
if (ws.readyState !== WebSocket.OPEN) {
setTimeout(() => {
ws = reconnect(ws);
}, 5000);
}
}
});
ws.on("upgrade", () => {
console.log(
@ -181,27 +177,31 @@ ws.on("upgrade", () => {
);
});
ws.on("message", async (data) => {
const message: WSEvent = JSON.parse(data.toString("utf-8"));
if (message.event !== "notification") {
// only watch for notification events
return;
}
console.log("Websocket message received.");
const payload = JSON.parse(message.payload) as Notification;
const ollamaResponse = await generateOllamaRequest(payload);
if (ollamaResponse) {
await postReplyToStatus(payload, ollamaResponse);
}
ws.on("open", () => {
setInterval(() => {
ws.send(JSON.stringify({ type: "ping" }));
console.log("Sending ping to keep session alive...");
}, 20000);
});
// if (notifications) {
// await Promise.all(
// notifications.map(async (notification) => {
// const ollamaResponse = await generateOllamaRequest(notification);
// if (ollamaResponse) {
// postReplyToStatus(notification, ollamaResponse);
// }
// })
// );
// }
ws.on("pong", (data) => {
console.log(`Pong received: ${JSON.stringify(data.toString("utf-8"))}`);
});
ws.on("message", async (data) => {
try {
const message: WSEvent = JSON.parse(data.toString("utf-8"));
if (message.event !== "notification") {
// only watch for notification events
return;
}
console.log("Websocket message received.");
const payload = JSON.parse(message.payload) as Notification;
const ollamaResponse = await generateOllamaRequest(payload);
if (ollamaResponse) {
await postReplyToStatus(payload, ollamaResponse);
}
} catch (error: any) {
console.error(error.message);
}
});