Doorbell works, added user friendliness for setup
This commit is contained in:
parent
0cef4671c5
commit
a25190d742
|
@ -118,4 +118,5 @@ dist
|
||||||
.pnp.*
|
.pnp.*
|
||||||
|
|
||||||
bot-storage.json
|
bot-storage.json
|
||||||
config.json
|
config.json
|
||||||
|
logAuthToken.json
|
27
README.md
27
README.md
|
@ -1,6 +1,27 @@
|
||||||
# doorbell-matrix-bot
|
# 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
|
### Step 1.2: Getting the token
|
||||||
https://git.pvv.ntnu.no/henrkgr/doorbell-matrix-bot
|
- 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
|
|
@ -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/"
|
||||||
|
}
|
26
index.mjs
26
index.mjs
|
@ -13,6 +13,17 @@ const homeserverUrl = config.homeserver;
|
||||||
const token = config.token;
|
const token = config.token;
|
||||||
const prefix = config.prefix;
|
const prefix = config.prefix;
|
||||||
const rooms = config.rooms;
|
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
|
// 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
|
// 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);
|
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', {
|
|
||||||
method: 'POST',
|
fetch(doorbellWebhook, { method: 'POST' }).then(response => {
|
||||||
headers: {
|
if (response.ok) {
|
||||||
'Content-Type' : 'application/json'
|
console.log("DING DONG!");
|
||||||
},
|
} else {
|
||||||
})
|
console.log("No ding dong :(");
|
||||||
|
console.log(response);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var tags = [];
|
var tags = [];
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
{
|
||||||
|
"username": "USERNAME",
|
||||||
|
"password": "PASSWORD"
|
||||||
|
}
|
|
@ -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);
|
Loading…
Reference in New Issue