inital commit
This commit is contained in:
commit
d99d132093
7 changed files with 1905 additions and 0 deletions
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
/node_modules/
|
||||
/crypto-storage/matrix-sdk-crypto/conf
|
||||
/crypto-storage/matrix-sdk-crypto/db
|
||||
/crypto-storage/matrix-sdk-crypto/snap.0000000001B6F8BB
|
||||
/crypto-storage/bot.json
|
||||
/crypto-storage/bot-sdk.json
|
15
Dockerfile
Normal file
15
Dockerfile
Normal file
|
@ -0,0 +1,15 @@
|
|||
FROM node:20-alpine
|
||||
|
||||
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
|
||||
USER root
|
||||
WORKDIR /home/node/app
|
||||
|
||||
COPY --chown=node:node package*.json ./
|
||||
|
||||
RUN npm install
|
||||
|
||||
USER node
|
||||
COPY --chown=node:node . .
|
||||
RUN ["chmod", "755", "index.mjs"]
|
||||
|
||||
CMD [ "node", "indec.mjs" ]
|
5
config.mjs
Normal file
5
config.mjs
Normal file
|
@ -0,0 +1,5 @@
|
|||
export var MATRIX_TOKEN=process.env.MATRIX_TOKEN ||"sys_xxxx"
|
||||
export var MATRIX_URL=process.env.MATRIX_URL || "https://matrix.org";
|
||||
export var JITSI_ADMIN_URL =process.env.JITSI_ADMIN_URL || 'https://jitsi-admin.de';
|
||||
export var MATRIX_USERNAME =process.env.MATRIX_USERNAME || 'h2inventbot';
|
||||
export var MATRIX_PASSWORD =process.env.MATRIX_PASSWORD || 'xxxxxxxx';
|
75
index.mjs
Normal file
75
index.mjs
Normal file
|
@ -0,0 +1,75 @@
|
|||
|
||||
|
||||
import {
|
||||
MatrixClient,
|
||||
SimpleFsStorageProvider,
|
||||
AutojoinRoomsMixin,
|
||||
LogService,
|
||||
LogLevel,
|
||||
RustSdkCryptoStorageProvider
|
||||
} from "matrix-bot-sdk";
|
||||
import { md5 } from 'js-md5';
|
||||
|
||||
import {JITSI_ADMIN_URL, MATRIX_TOKEN, MATRIX_URL} from './config.mjs'
|
||||
|
||||
const cryptoProvider = new RustSdkCryptoStorageProvider("./crypto-storage/");
|
||||
|
||||
// LogService.muteModule("Metrics");
|
||||
// LogService.trace = LogService.debug;
|
||||
// LogService.setLevel(LogLevel.TRACE);
|
||||
// This will be the URL where clients can reach your homeserver. Note that this might be different
|
||||
// from where the web/chat interface is hosted. The server must support password registration without
|
||||
// captcha or terms of service (public servers typically won't work).
|
||||
const homeserverUrl = MATRIX_URL;
|
||||
const jitsiAdminServerUrl = JITSI_ADMIN_URL;
|
||||
|
||||
// Use the access token you got from login or registration above.
|
||||
const accessToken = MATRIX_TOKEN;//"syt_aDJpbnZlbnRib3Q_NCOyzAPnpzQoyfACReZX_09Heja";
|
||||
|
||||
// In order to make sure the bot doesn't lose its state between restarts, we'll give it a place to cache
|
||||
// any information it needs to. You can implement your own storage provider if you like, but a JSON file
|
||||
// will work fine for this example.
|
||||
const storage = new SimpleFsStorageProvider("./crypto-storage/bot.json");
|
||||
|
||||
// Finally, let's create the client and set it to autojoin rooms. Autojoining is typical of bots to ensure
|
||||
// they can be easily added to any room.
|
||||
const client = new MatrixClient(homeserverUrl, accessToken, storage, cryptoProvider);
|
||||
AutojoinRoomsMixin.setupOnClient(client);
|
||||
|
||||
// Before we start the bot, register our command handler
|
||||
client.on("room.message", handleCommand);
|
||||
|
||||
// Now that everything is set up, start the bot. This will start the sync loop and run until killed.
|
||||
client.start().then(() => console.log("Bot started!"));
|
||||
|
||||
// This is the command handler we registered a few lines up
|
||||
async function handleCommand(roomId, event) {
|
||||
// Don't handle unhelpful events (ones that aren't text messages, are redacted, or sent by us)
|
||||
if (event['content']?.['msgtype'] !== 'm.text') return;
|
||||
if (event['sender'] === await client.getUserId()) return;
|
||||
|
||||
// Check to ensure that the `!hello` command is being run
|
||||
const body = event['content']['body'];
|
||||
if (!body?.startsWith("!jitsi")) return;
|
||||
// Now that we've passed all the checks, we can actually act upon the command
|
||||
await sendMessageWithUrl(client,roomId);
|
||||
await changeRoomName(client, roomId);
|
||||
}
|
||||
|
||||
function createConference(roomId) {
|
||||
var hash = md5(roomId);
|
||||
return jitsiAdminServerUrl+'/m/'+hash;
|
||||
}
|
||||
|
||||
async function sendMessageWithUrl(client,roomId) {
|
||||
await client.sendText(roomId, 'Die Konferenz für diesen Raum läuft unter dieser URL: '+createConference(roomId));
|
||||
}
|
||||
|
||||
async function changeRoomName(client, roomId){
|
||||
var roomDescription = await client.getRoomStateEvent(roomId,'m.room.topic');
|
||||
roomDescription = roomDescription.topic;
|
||||
var conferenceUrl = createConference(roomId);
|
||||
if (!roomDescription.includes(conferenceUrl)){
|
||||
await client.sendStateEvent(roomId,'m.room.topic','',{'topic':roomDescription+"\n\r"+conferenceUrl})
|
||||
}
|
||||
}
|
13
login.mjs
Normal file
13
login.mjs
Normal file
|
@ -0,0 +1,13 @@
|
|||
import {MatrixAuth} from "matrix-bot-sdk";
|
||||
import {MATRIX_PASSWORD, MATRIX_URL, MATRIX_USERNAME} from './config.mjs'
|
||||
// This will be the URL where clients can reach your homeserver. Note that this might be different
|
||||
// from where the web/chat interface is hosted. The server must support password registration without
|
||||
// captcha or terms of service (public servers typically won't work).
|
||||
const homeserverUrl = MATRIX_URL;
|
||||
const username = MATRIX_USERNAME;
|
||||
const password = MATRIX_PASSWORD;
|
||||
|
||||
const auth = new MatrixAuth(homeserverUrl);
|
||||
const client = await auth.passwordLogin(username, password);
|
||||
|
||||
console.log("Copy this access token to your bot's config: ", client.accessToken);
|
1776
package-lock.json
generated
Normal file
1776
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
15
package.json
Normal file
15
package.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"name": "matrix_node_bot",
|
||||
"version": "1.0.0",
|
||||
"description": "matrix bot for jitsi-admin",
|
||||
"main": "index.mjs",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"js-md5": "^0.8.3",
|
||||
"matrix-bot-sdk": "^0.7.0"
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue