Bot runs (epic)

This commit is contained in:
Henrik Kjernmoen Gran 2024-08-15 21:57:13 +02:00
parent 8c77451317
commit 68736384a8
4 changed files with 213 additions and 4 deletions

7
config.json Normal file
View File

@ -0,0 +1,7 @@
{
"homeserver": "https://matrix.pvv.ntnu.no/",
"username": "bot_doorbell",
"password": "Abc@123@Abc",
"prefix": "!",
"activeRooms": []
}

View File

@ -1,8 +1,101 @@
import {
MatrixClient,
SimpleFsStorageProvider,
MatrixAuth,
AutojoinUpgradedRoomsMixin,
RichRepliesPreprocessor
} from "matrix-bot-sdk";
console.log("owo");
import axios from "axios";
import config from "./config.json" assert {type: "json"};
const homeserverUrl = config.homeserver;
const username = config.username;
const password = config.password;
const prefix = config.prefix;
// Now we can create the client and set it up to automatically join rooms.
const client = await new MatrixAuth(homeserverUrl).passwordLogin(username, password);
AutojoinUpgradedRoomsMixin.setupOnClient(client);
client.addPreprocessor(new RichRepliesPreprocessor(false));
// We also want to make sure we can receive events - this is where we will
// handle our command.
client.on("room.message", handleCommand);
client.on("room.invite", (roomId, inviteEvent) => {
console.log(inviteEvent);
if (inviteEvent.sender !== "@henrkgr:pvv.ntnu.no") return;
return client.joinRoom(roomId);
});
// Now that the client is all set up and the event handler is registered, start the
// client up. This will start it syncing.
client.start().then(() => console.log("Client started!"));
// This is our event handler for dealing with the `!hello` command.
async function handleCommand(roomId, event) {
// Don't handle events that don't have contents (they were probably redacted)
if (!event["content"]) return;
// Don't handle non-text events
if (event["content"]["msgtype"] !== "m.text") return;
// We never send `m.text` messages so this isn't required, however this is
// how you would filter out events sent by the bot itself.
if (event["sender"] === await client.getUserId()) return;
// Make sure that the event looks like a command we're expecting
const text = event["content"]["body"];
if (!text) return;
console.log(text);
var tags = [];
var re = /`([a-z1-9:. æøå]*)`/g;
do {
var m = re.exec(text);
if (m) {
tags.push(m[1]);
}
} while (m);
if (tags == false) return;
var encoded_tags = encodeURI(JSON.stringify(tags));
var file_ids = (await axios.get(hydrusURI + "/get_files/search_files?system_inbox=true&tags=" + encoded_tags, {headers: {'Hydrus-Client-API-Access-Key': hydrusToken}})).data.file_ids;
if (file_ids == false) return;
console.log(file_ids);
var random_id = [file_ids[Math.floor(Math.random() * file_ids.length)]];
var encoded_id = encodeURI(JSON.stringify(random_id));
var metadata = (await axios.get(hydrusURI + "/get_files/file_metadata?&file_ids=" + encoded_id, {headers: {'Hydrus-Client-API-Access-Key': hydrusToken}})).data.metadata[0];
console.log(metadata);
var hash = metadata.hash;
var width = metadata.width;
var height = metadata.height;
var mime = metadata.mime;
var ext = metadata.ext;
var size = metadata.size;
var mxc = "mxc://" + fake_hs + "/" + prefix + hash;
var event = {
"body": hash + ext,
"info": {
"h": height,
"w": width,
"mimetype": mime,
"size": size,
},
"msgtype": "m.image",
"url": mxc
};
client.sendMessage(roomId, event);
}

106
package-lock.json generated Normal file
View File

@ -0,0 +1,106 @@
{
"name": "doorbell-matrix-bot",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "doorbell-matrix-bot",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"axios": "^1.7.4"
}
},
"node_modules/asynckit": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
},
"node_modules/axios": {
"version": "1.7.4",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.7.4.tgz",
"integrity": "sha512-DukmaFRnY6AzAALSH4J2M3k6PkaC+MfaAGdEERRWcC9q3/TWQwLpHR8ZRLKTdQ3aBDL64EdluRDjJqKw+BPZEw==",
"dependencies": {
"follow-redirects": "^1.15.6",
"form-data": "^4.0.0",
"proxy-from-env": "^1.1.0"
}
},
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
"dependencies": {
"delayed-stream": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/delayed-stream": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
"engines": {
"node": ">=0.4.0"
}
},
"node_modules/follow-redirects": {
"version": "1.15.6",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
"integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime-types": {
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"dependencies": {
"mime-db": "1.52.0"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
}
}
}

View File

@ -11,5 +11,8 @@
"url": "https://git.pvv.ntnu.no/henrkgr/doorbell-matrix-bot"
},
"author": "henrkgr",
"license": "ISC"
"license": "ISC",
"dependencies": {
"axios": "^1.7.4"
}
}