created
This commit is contained in:
parent
938b959e83
commit
b2277fbea0
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
# ignore folders
|
||||||
|
node_modules
|
||||||
|
|
||||||
|
# ignore files
|
||||||
|
config.json
|
||||||
|
storage.json
|
|
@ -1,3 +1,9 @@
|
||||||
# media-bot
|
# 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
5
example.config.json
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"baseUrl": "<DOMAIN.TLD>",
|
||||||
|
"token": "<TOKEN>",
|
||||||
|
"userId": "@<USER>:<DOMAIN.TLD>"
|
||||||
|
}
|
30
index.js
Normal file
30
index.js
Normal 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
6
package.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"matrix-bot-sdk": "^0.6.1"
|
||||||
|
},
|
||||||
|
"type": "module"
|
||||||
|
}
|
1124
pnpm-lock.yaml
Normal file
1124
pnpm-lock.yaml
Normal file
File diff suppressed because it is too large
Load diff
BIN
res/demo.png
Normal file
BIN
res/demo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 103 KiB |
Loading…
Reference in a new issue