Skip to content

Commit 80fe2e3

Browse files
Mihai - Alexandru ChindrișMihai - Alexandru Chindriș
authored andcommitted
Auto-invite Matrix user
1 parent 7a541dc commit 80fe2e3

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

apps/webhook-receiver/.env.example

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ MYMX_WEBHOOK_SECRET=replace_me
33
EVENT_STORE=../../data/events.jsonl
44
RULES_PATH=../../rules.json
55
NOTIFY_THRESHOLD=60
6-
MATRIX_HOMESERVER=https://matrix.beeper.com
6+
MATRIX_HOMESERVER=https://matrix-client.matrix.org
77
MATRIX_ACCESS_TOKEN=replace_me
8-
MATRIX_ROOM_ID=!roomid:beeper.local
8+
MATRIX_ROOM_ID=
99
MATRIX_ROOM_STORE=../../data/matrix-room.json
10+
MATRIX_INVITE_USER=@your-main-account:matrix.org

apps/webhook-receiver/src/server.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const matrixHomeserver = process.env.MATRIX_HOMESERVER || "https://matrix.beeper
1616
const matrixAccessToken = process.env.MATRIX_ACCESS_TOKEN || "";
1717
let matrixRoomId = process.env.MATRIX_ROOM_ID || "";
1818
const matrixRoomStore = process.env.MATRIX_ROOM_STORE || "./matrix-room.json";
19+
const matrixInviteUser = process.env.MATRIX_INVITE_USER || "";
1920

2021
if (!secret) {
2122
throw new Error("Missing MYMX_WEBHOOK_SECRET");
@@ -189,9 +190,26 @@ async function ensureMatrixRoom(): Promise<boolean> {
189190

190191
matrixRoomId = roomId;
191192
saveRoomId(roomId);
193+
194+
if (matrixInviteUser) {
195+
await inviteMatrixUser(roomId, matrixInviteUser);
196+
}
192197
return true;
193198
}
194199

200+
async function inviteMatrixUser(roomId: string, userId: string): Promise<boolean> {
201+
const url = `${matrixHomeserver}/_matrix/client/v3/rooms/${encodeURIComponent(
202+
roomId
203+
)}/invite?access_token=${encodeURIComponent(matrixAccessToken)}`;
204+
const payload = { user_id: userId };
205+
const resp = await fetch(url, {
206+
method: "POST",
207+
headers: { "Content-Type": "application/json" },
208+
body: JSON.stringify(payload),
209+
});
210+
return resp.ok;
211+
}
212+
195213
// MyMX needs raw text body to verify signatures.
196214
app.use(
197215
express.text({

0 commit comments

Comments
 (0)