Compare commits
5 Commits
add-websoc
...
dac037809c
Author | SHA1 | Date | |
---|---|---|---|
dac037809c | |||
6088a2cbd3 | |||
ed8d148d0a | |||
379099dc7a | |||
c0ed38ac1a |
10
README.md
10
README.md
@ -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.
|
70
src/main.ts
70
src/main.ts
@ -11,28 +11,6 @@ import { createWebsocket } from "./websocket.js";
|
||||
|
||||
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({
|
||||
@ -181,27 +159,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("close", (event: CloseEvent) => {
|
||||
console.log(`Connection closed: ${event.reason}`);
|
||||
});
|
||||
|
||||
// if (notifications) {
|
||||
// await Promise.all(
|
||||
// notifications.map(async (notification) => {
|
||||
// const ollamaResponse = await generateOllamaRequest(notification);
|
||||
// if (ollamaResponse) {
|
||||
// postReplyToStatus(notification, ollamaResponse);
|
||||
// }
|
||||
// })
|
||||
// );
|
||||
// }
|
||||
ws.on("open", () => {
|
||||
setInterval(() => {
|
||||
ws.send(JSON.stringify({ type: "ping" }));
|
||||
console.log("Sending ping to keep session alive...");
|
||||
}, 20000);
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user