-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
82 lines (69 loc) · 2.31 KB
/
index.ts
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
70
71
72
73
74
75
76
77
78
79
80
81
82
import { MatrixClient, SimpleFsStorageProvider } from "matrix-bot-sdk";
interface APNote {
"@context": string | string[];
id: string;
type: string;
actor: string;
to: string[];
content: string;
}
const matrixHomeserverUrl = "https://your.matrix.server";
const matrixAccessToken = "MATRIX_ACCESS_TOKEN";
const matrixUserId = "@bridgebot:your.matrix.server";
const activityPubActor = "https://activitypub.instance/actors/username";
const activityPubInbox = "https://activitypub.instance/inbox";
async function main() {
const storage = new SimpleFsStorageProvider("matrix.json");
const client = new MatrixClient(matrixHomeserverUrl, matrixAccessToken, storage);
await client.start();
// Listen for incoming Matrix messages
client.on("room.message", async (roomId: string, event: any) => {
if (!event || !event.content || event.sender === matrixUserId) {
return;
}
// Extract the plain text body
const messageType = event.content["msgtype"];
const messageBody = event.content["body"] || "";
if (messageType === "m.text") {
const note: APNote = {
"@context": "https://www.w3.org/ns/activitystreams",
id: `https://example.org/notes/${Date.now()}`,
type: "Note",
actor: activityPubActor,
to: [activityPubActor],
content: messageBody,
};
try {
const response = await fetch(activityPubInbox, {
method: "POST",
headers: {
"Content-Type": "application/activity+json"
},
body: JSON.stringify(note)
});
if (!response.ok) {
console.error("Failed to send to AP inbox:", await response.text());
}
} catch (err) {
console.error("Error sending to AP inbox:", err);
}
}
});
console.log("Matrix-ActivityPub bridge is running.");
}
async function handleIncomingActivityPubRequest(body: any) {
const note = body as APNote;
const content = note.content || "";
const targetUserId = "@targetuser:your.matrix.server";
try {
const dmRoom = await client.createRoom({
preset: "private_chat",
invite: [targetUserId],
is_direct: true
});
await client.sendText(dmRoom.room_id, content);
} catch (err) {
console.error("Error sending Matrix DM:", err);
}
}
main().catch((err) => console.error(err));