This commit is contained in:
array-in-a-matrix 2022-08-13 17:22:41 -04:00
parent 938b959e83
commit b2277fbea0
7 changed files with 1178 additions and 1 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
# ignore folders
node_modules
# ignore files
config.json
storage.json

View File

@ -1,3 +1,9 @@
# media-bot
Matrix bot that displays images using their media ID.
Matrix bot that displays images using their media ID.
Bot configuration can be specified in the `config.json` file.
Format: ```<prefix>show <image ID>```
<img src="./res/demo.png">

5
example.config.json Normal file
View File

@ -0,0 +1,5 @@
{
"baseUrl": "<DOMAIN.TLD>",
"token": "<TOKEN>",
"userId": "@<USER>:<DOMAIN.TLD>"
}

30
index.js Normal file
View File

@ -0,0 +1,30 @@
// ? import bot sdk and configuration
import config from './config.json' assert {type: "json"};
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } from "matrix-bot-sdk";
// ? save state of bot between restarts
const storage = new SimpleFsStorageProvider("storage.json");
// ? create a new matrix client
const client = new MatrixClient(config.baseUrl, config.token, storage);
// ? auto join rooms invited to
AutojoinRoomsMixin.setupOnClient(client);
// ? start the client
client.start().then(() => console.log(`Client has started!`));
// ? event listener for when a message is sent
client.on("room.message", (roomId, event) => {
// ? check if the message's text is not empty and isn't sent by the bot itself
if (! event["content"] || event["sender"] === config.userId) return;
// ? if message starts with the bot's <prefix + command> then execute the command
if (event["content"]["body"].toLowerCase().startsWith(config.prefix + "show")){
// ? send image from the command's first argument, i.e. create mxc url from the argument
client.sendMessage(roomId, {
"body": "Image from Matrix",
"msgtype": "m.image",
"url": `${config.baseUrl.replace("https://matrix.", "mxc://")}/${event["content"]["body"].split(" ")[1]}`
});
}
})

6
package.json Normal file
View File

@ -0,0 +1,6 @@
{
"dependencies": {
"matrix-bot-sdk": "^0.6.1"
},
"type": "module"
}

1124
pnpm-lock.yaml Normal file

File diff suppressed because it is too large Load Diff

BIN
res/demo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB