Doorbell works, added user friendliness for setup

This commit is contained in:
Henrik Kjernmoen Gran 2024-08-16 00:31:24 +02:00
parent 0cef4671c5
commit a25190d742
6 changed files with 75 additions and 10 deletions

1
.gitignore vendored
View File

@ -119,3 +119,4 @@ dist
bot-storage.json
config.json
logAuthToken.json

View File

@ -1,6 +1,27 @@
# doorbell-matrix-bot
<p>Doorbell bot for matrix</p>
<p>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 <b>will not</b> join!</p>
<p>The commands to trigger the doorbell is: "doorbell", "open", "ring", "knock", "ding", "dong" & "dingdong"</p>
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
### 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

7
config.json.template Normal file
View File

@ -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/"
}

View File

@ -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 = [];

View File

@ -0,0 +1,4 @@
{
"username": "USERNAME",
"password": "PASSWORD"
}

18
logAuthToken.mjs Normal file
View File

@ -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);