This commit is contained in:
Kristian Iver Berget 2024-08-15 23:17:07 +02:00
commit 0cef4671c5
1 changed files with 6 additions and 7 deletions

View File

@ -27,10 +27,7 @@ client.addPreprocessor(new RichRepliesPreprocessor(false));
// We also want to make sure we can receive events - this is where we will // We also want to make sure we can receive events - this is where we will
// handle our command. // handle our command.
client.on("room.message", handleCommand); client.on("room.message", handleCommand);
client.on("room.invite", (roomId, inviteEvent) => { client.on("room.invite", (roomId) => {
console.log(inviteEvent);
console.log(roomId);
console.log(rooms[0]);
if (rooms.includes(roomId)) { if (rooms.includes(roomId)) {
return client.joinRoom(roomId); return client.joinRoom(roomId);
} }
@ -40,7 +37,7 @@ client.on("room.invite", (roomId, inviteEvent) => {
// client up. This will start it syncing. // client up. This will start it syncing.
client.start().then(() => console.log("Client started!")); client.start().then(() => console.log("Client started!"));
// This is our event handler for dealing with the `!hello` command. // This is our event handler for dealing with commands.
async function handleCommand(roomId, event) { async function handleCommand(roomId, event) {
// Don't handle events that don't have contents (they were probably redacted) // Don't handle events that don't have contents (they were probably redacted)
if (!event["content"]) return; if (!event["content"]) return;
@ -56,8 +53,10 @@ async function handleCommand(roomId, event) {
// If not in any authorized rooms, ignore // If not in any authorized rooms, ignore
if (!rooms.includes(roomId)) return; if (!rooms.includes(roomId)) return;
const text = event["content"]["body"]; // Check that the message starts with the prefix, and get the prefix-less message
if (!text) return; const rawText = event["content"]["body"];
if (!rawText || !rawText.startsWith(prefix)) return;
const text = rawText.substring(prefix.length);
if (["doorbell", "open", "ring", "knock", "ding", "dong", "dingdong"].includes(text)) { if (["doorbell", "open", "ring", "knock", "ding", "dong", "dingdong"].includes(text)) {
fetch('https://homeassistant.pvv.ntnu.no:8123/api/webhook/doorbell-oRkXU_ZUFzkc4wTtc6_m9PFR', { fetch('https://homeassistant.pvv.ntnu.no:8123/api/webhook/doorbell-oRkXU_ZUFzkc4wTtc6_m9PFR', {