mirror of
https://github.com/jesperh1/nodebb-matrix-bridge
synced 2025-06-07 20:08:40 +01:00
Initil commit
This commit is contained in:
parent
d5c792c394
commit
d55ac7dcfb
8
config.ts.example
Normal file
8
config.ts.example
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
export const config = {
|
||||||
|
MATRIX_ACCESS_TOKEN: "",
|
||||||
|
HOMESERVER_URL: "https://matrix.org",
|
||||||
|
ROOM_ID: "",
|
||||||
|
USER_ID: "@forum-bot:matrix.org",
|
||||||
|
DB_PATH: "./topics.db",
|
||||||
|
FORUM_BASE_URL: "https://nodebbforum.com",
|
||||||
|
}
|
65
main.ts
Normal file
65
main.ts
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
import { createClient, MatrixClient, MsgType } from "npm:matrix-js-sdk";
|
||||||
|
import * as sqlite from "jsr:@db/sqlite";
|
||||||
|
import { config } from "./config.ts";
|
||||||
|
|
||||||
|
const client: MatrixClient = createClient({
|
||||||
|
baseUrl: config.HOMESERVER_URL,
|
||||||
|
accessToken: config.MATRIX_ACCESS_TOKEN,
|
||||||
|
userId: config.USER_ID,
|
||||||
|
});
|
||||||
|
|
||||||
|
await client.startClient();
|
||||||
|
console.log("Client Started");
|
||||||
|
|
||||||
|
async function sendMessage(msg: string) {
|
||||||
|
await client.sendMessage(config.ROOM_ID, {
|
||||||
|
msgtype: MsgType.Text,
|
||||||
|
body: msg,
|
||||||
|
format: "org.matrix.custom.html",
|
||||||
|
formatted_body: msg,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const db = new sqlite.Database(config.DB_PATH);
|
||||||
|
|
||||||
|
await db.exec(`
|
||||||
|
CREATE TABLE IF NOT EXISTS topics (
|
||||||
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||||
|
title TEXT NOT NULL,
|
||||||
|
slug TEXT NOT NULL UNIQUE,
|
||||||
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
|
||||||
|
const API_ENDPOINT = `${config.FORUM_BASE_URL}/api/recent`;
|
||||||
|
|
||||||
|
interface ForumTopic {
|
||||||
|
title: string;
|
||||||
|
slug: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function checkForNewTopics() {
|
||||||
|
const response = await fetch(API_ENDPOINT);
|
||||||
|
const data = await response.json();
|
||||||
|
const topics = data.topics as ForumTopic[];
|
||||||
|
|
||||||
|
const stmt = db.prepare("SELECT * FROM topics WHERE slug = ?");
|
||||||
|
|
||||||
|
for (const topic of topics) {
|
||||||
|
const result = await stmt.get(topic.slug);
|
||||||
|
|
||||||
|
if (!result) {
|
||||||
|
await db.prepare("INSERT INTO topics (title, slug) VALUES (?, ?)").run(topic.title, topic.slug);
|
||||||
|
|
||||||
|
const topicUrl = `${config.FORUM_BASE_URL}/topic/${topic.slug}`;
|
||||||
|
const message = `New forum post: <a href="${topicUrl}">${topic.title}</a>`;
|
||||||
|
await sendMessage(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run every 5 minutes
|
||||||
|
setInterval(checkForNewTopics, 5 * 60 * 1000);
|
||||||
|
await checkForNewTopics(); // Run once immediately
|
||||||
|
|
||||||
|
await db.close();
|
Loading…
Reference in New Issue
Block a user