forked from benparsons/elizabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.js
54 lines (49 loc) · 1.75 KB
/
simple.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
52
53
54
const Eliza = require('eliza-as-promised');
const MatrixClient = require("matrix-bot-sdk").MatrixClient;
const AutojoinRoomsMixin = require("matrix-bot-sdk").AutojoinRoomsMixin;
var config = require('config');
var access_token = "";
var homeserver = "";
var bot_user = "";
if (!( config.has('access_token') && config.has('homeserver') && config.has('bot_user'))) {
console.log("config fields required: access_token, homeserver, bot_user");
process.exit(1);
}
access_token = config.get('access_token');
homeserver = config.get('homeserver');
bot_user = config.get('bot_user');
const client = new MatrixClient(homeserver, access_token);
AutojoinRoomsMixin.setupOnClient(client);
client.start().then(() => console.log("Client started!"));
var elizas = {};
client.on("room.join", (roomId) => {
elizas[roomId] = {
eliza: new Eliza(),
last: (new Date()).getTime()
}
client.sendMessage(roomId, {
"msgtype": "m.notice",
"body": elizas[roomId].eliza.getInitial()
});
});
client.on("room.message", (roomId, event) => {
if (event.sender === bot_user) return;
elizas[roomId].eliza.getResponse(event.content.body)
.then((response) => {
var responseText = '';
if (response.reply) { responseText = response.reply; }
if (response.final) { responseText = response.final; }
client.sendMessage(roomId, {
"msgtype": "m.notice",
"body": responseText,
"responds": {
"sender": event.sender,
"message": event.content.body
}
}).then((eventId) => {
if (response.final) {
client.leaveRoom(roomId);
}
});
});
});