-
Notifications
You must be signed in to change notification settings - Fork 0
/
bots.js
51 lines (42 loc) · 1.56 KB
/
bots.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"use strict";
const bugs = require('./bugs.js')
const MatrixSDK = require("matrix-bot-sdk");
const MatrixClient = MatrixSDK.MatrixClient;
const SimpleFsStorageProvider = MatrixSDK.SimpleFsStorageProvider;
const AutojoinRoomsMixin = MatrixSDK.AutojoinRoomsMixin;
const redis = require('redis').createClient({
url: (process.env.REDIS_URL || 'redis://localhost:6379/0')
});
const bots = {};
console.log("booting bots!");
module.exports = async () => {
try {
redis.on('error', (err) => bugs.log('bots redis client error', err));
await redis.connect();
let bots = {}
const subscriber = redis.duplicate();
await subscriber.connect();
console.log("bot redis connected");
await subscriber.pSubscribe('chatbot/*', (json, channel) => {
console.log(`bot message: channel: ${channel}, json: ${json}`);
const params = JSON.parse(json);
if (channel == 'chatbot/test') {
const client = new MatrixClient(params['server'], params['access_token']);
client.resolveRoom(params['channel']).then((roomId) => {
client.sendMessage(roomId, {"msgtype": "m.notice", "body": params['message']});
})
}
if (channel == 'chatbot/publish') {
const key = JSON.stringify(params.config)
if (!bots[key]) {
bots[key] = new MatrixClient(params.config.server, params.config.access_token);
}
bots[key].resolveRoom(params.config.channel).then((roomId) => {
bots[key].sendHtmlText(roomId, params.payload.html);
})
}
});
} catch (e) {
bugs.log(e)
}
}