diff --git a/.gitignore b/.gitignore index 92cb982..0f2d29e 100644 --- a/.gitignore +++ b/.gitignore @@ -118,4 +118,5 @@ dist .pnp.* bot-storage.json -config.json \ No newline at end of file +config.json +logAuthToken.json \ No newline at end of file diff --git a/README.md b/README.md index 003ca6a..2a659cf 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,27 @@ # doorbell-matrix-bot +

Doorbell bot for matrix

+

The bot will only respond & join in authorized rooms (add authorized room IDs in `config.json` => `rooms`). +NOTE: The bot needs to have the room authorized before being invited. If the bot is invited, then has the room authorized in the config, it will not join!

+

The commands to trigger the doorbell is: "doorbell", "open", "ring", "knock", "ding", "dong" & "dingdong"

-Doorbell bot for matrix +# How to run: +## Step 1: Getting an access token +### Step 1.1: Inserting required info to generate token +- Fill in: `homeserver` (`config.json`) +- Fill in: `username` (`logAuthToken.json`) +- Fill in: `password` (`logAuthToken.json`) -# Library -https://git.pvv.ntnu.no/henrkgr/doorbell-matrix-bot \ No newline at end of file +### Step 1.2: Getting the token +- Run: `logAuthToken.mjs` (Command: `node logAuthToken.mjs`) +- If all is done correctly, an access token should be logged in the console + +## Step 2: Running the bot +### Step 2.1: Inserting more required information +- Fill in: `homeserver` (`config.json`) (This should already be filled if step `1.1` was done) +- Fill in: `token` (`config.json`) (This is the token you got in step `1.2`) +- Fill in: `prefix` (`config.json`) +- Fill in: `rooms` (`config.json`) +- Fill in: `doorbellWebhook` (`config.json`) +### Step 2.2: Actually running the bot +- Run: `index.mjs` (Command: `node index.mjs`) +- If all is done correctly, the bot should be running \ No newline at end of file diff --git a/config.json.template b/config.json.template new file mode 100644 index 0000000..6f9837e --- /dev/null +++ b/config.json.template @@ -0,0 +1,7 @@ +{ + "homeserver": "HOME_SERVER", + "token": "ACCESS_TOKEN", + "prefix": "BOT_PREFIX", + "rooms": ["AUTHORIZED_ROOM_ID", "ANOTHER_AUTHORIZED_ID"], + "doorbellWebhook": "https://GOOGLE.ASSISTANT.WEBHOOK.URL/" +} \ No newline at end of file diff --git a/index.mjs b/index.mjs index 382f778..0cc6219 100644 --- a/index.mjs +++ b/index.mjs @@ -13,6 +13,17 @@ const homeserverUrl = config.homeserver; const token = config.token; const prefix = config.prefix; const rooms = config.rooms; +const doorbellWebhook = config.doorbellWebhook; + +function configNotFound(name) { + throw new Error(`ERROR: Config option "${name}" not found`); +} + +if (!homeserverUrl) configNotFound("homeserver"); +if (!token) configNotFound("token"); +if (!prefix) configNotFound("prefix"); +if (!rooms) configNotFound("rooms"); +if (!doorbellWebhook) configNotFound("doorbellWebhook"); // We'll want to make sure the bot doesn't have to do an initial sync every // time it restarts, so we need to prepare a storage provider. Here we use @@ -59,12 +70,15 @@ async function handleCommand(roomId, event) { const text = rawText.substring(prefix.length); if (["doorbell", "open", "ring", "knock", "ding", "dong", "dingdong"].includes(text)) { - fetch('https://homeassistant.pvv.ntnu.no:8123/api/webhook/doorbell-oRkXU_ZUFzkc4wTtc6_m9PFR', { - method: 'POST', - headers: { - 'Content-Type' : 'application/json' - }, - }) + + fetch(doorbellWebhook, { method: 'POST' }).then(response => { + if (response.ok) { + console.log("DING DONG!"); + } else { + console.log("No ding dong :("); + console.log(response); + } + }); } var tags = []; diff --git a/logAuthToken.json.template b/logAuthToken.json.template new file mode 100644 index 0000000..af89a54 --- /dev/null +++ b/logAuthToken.json.template @@ -0,0 +1,4 @@ +{ + "username": "USERNAME", + "password": "PASSWORD" +} \ No newline at end of file diff --git a/logAuthToken.mjs b/logAuthToken.mjs new file mode 100644 index 0000000..048abf0 --- /dev/null +++ b/logAuthToken.mjs @@ -0,0 +1,18 @@ +import { MatrixAuth } from "matrix-bot-sdk"; + +import configMain from "./config.json" assert {type: "json"}; +import config from "./logAuthToken.json" assert {type: "json"}; + +const homeserverUrl = configMain.homeserver; +const username = config.username; +const password = config.password; + +function configNotFound(name) { + throw new Error(`ERROR: Config option "${name}" not found`); +} + +if (!homeserverUrl) configNotFound("config.json => homeserver"); +if (!username) configNotFound("logAuthToken.json => username"); +if (!password) configNotFound("logAuthToken.json => password"); + +console.log("Access token: " + (await new MatrixAuth(homeserverUrl).passwordLogin(username, password)).accessToken); \ No newline at end of file