|
|
|
|
@@ -26,12 +26,14 @@ var _logger = _interopRequireDefault(require("./logger"));
|
|
|
|
|
var _encryptAttachment = _interopRequireDefault(require("./encrypt-attachment"));
|
|
|
|
|
|
|
|
|
|
global.Olm = require("olm");
|
|
|
|
|
const BOT_SIGNAL_END_CHAT = 'END_CHAT';
|
|
|
|
|
|
|
|
|
|
class OcrccBot {
|
|
|
|
|
constructor(botConfig) {
|
|
|
|
|
this.config = botConfig;
|
|
|
|
|
this.client = matrix.createClient(this.config.MATRIX_SERVER_URL);
|
|
|
|
|
this.joinedRooms = [];
|
|
|
|
|
this.inactivityTimers = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createLocalStorage() {
|
|
|
|
|
@@ -46,20 +48,20 @@ class OcrccBot {
|
|
|
|
|
return new _nodeLocalstorage.LocalStorage(localStoragePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendTextMessage(roomId, msgText, showToUser = null) {
|
|
|
|
|
async sendTextMessage(roomId, msgText, showToUser = null) {
|
|
|
|
|
const content = {
|
|
|
|
|
msgtype: "m.text",
|
|
|
|
|
body: msgText,
|
|
|
|
|
showToUser: showToUser
|
|
|
|
|
};
|
|
|
|
|
this.sendMessage(roomId, content);
|
|
|
|
|
await this.sendMessage(roomId, content);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendNotice(roomId, message) {
|
|
|
|
|
_logger.default.log("info", `SENDING *NOTICE*: ${message}`);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await this.client.sendNotice(roomId, message);
|
|
|
|
|
|
|
|
|
|
_logger.default.log("info", `SENT *NOTICE*: ${message}`);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
switch (err["name"]) {
|
|
|
|
|
case "UnknownDeviceError":
|
|
|
|
|
@@ -128,10 +130,9 @@ class OcrccBot {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async inviteFacilitators(roomId) {
|
|
|
|
|
this.localStorage.setItem(`${roomId}-waiting`, 'true');
|
|
|
|
|
let invitations = [];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
this.localStorage.setItem(`${roomId}-waiting`, 'true');
|
|
|
|
|
let invitations = [];
|
|
|
|
|
const roomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID);
|
|
|
|
|
const members = Object.keys(roomMembers["joined"]);
|
|
|
|
|
members.forEach(memberId => {
|
|
|
|
|
@@ -158,9 +159,8 @@ class OcrccBot {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async uninviteFacilitators(roomId) {
|
|
|
|
|
this.localStorage.removeItem(`${roomId}-waiting`);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
this.localStorage.removeItem(`${roomId}-waiting`);
|
|
|
|
|
const facilitatorsRoomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID);
|
|
|
|
|
const supportRoomMembers = await this.client.getJoinedRoomMembers(roomId);
|
|
|
|
|
const roomMembersIds = Object.keys(supportRoomMembers["joined"]);
|
|
|
|
|
@@ -183,14 +183,23 @@ class OcrccBot {
|
|
|
|
|
this.sendTextMessage(roomId, this.config.BOT_ERROR_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, `The Help Bot ran into an error: ${error}. Please verify that the chat service is working.`);
|
|
|
|
|
this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, `${this.config.BOT_DISPLAY_NAME} ran into an error: ${error}. Please verify that the chat service is working.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleMessageEvent(event) {
|
|
|
|
|
const content = event.getContent(); // do nothing if there's no content
|
|
|
|
|
const content = event.getContent();
|
|
|
|
|
const sender = event.getSender();
|
|
|
|
|
const roomId = event.getRoomId(); // do nothing if there's no content
|
|
|
|
|
|
|
|
|
|
if (!content) {
|
|
|
|
|
return;
|
|
|
|
|
} // if it's a chat message and the facilitator has joined, reset the inactivity timeout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const facilitatorId = this.localStorage.getItem(`${roomId}-facilitator`);
|
|
|
|
|
|
|
|
|
|
if (Boolean(facilitatorId) && sender !== this.config.BOT_USERID) {
|
|
|
|
|
this.setInactivityTimeout(roomId);
|
|
|
|
|
} // bot commands
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -216,7 +225,7 @@ class OcrccBot {
|
|
|
|
|
const filepath = this.localStorage.getItem(`${roomId}-transcript`);
|
|
|
|
|
|
|
|
|
|
if (!filepath) {
|
|
|
|
|
return _logger.default.log("error", `NO TRANSCRIPT FILE FOR ROOM: ${roomId}`);
|
|
|
|
|
return _logger.default.log("error", `NO TRANSCRIPT FILE FOR ROOM: ${roomId}. This message will not be added: ${content.body}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const message = `${sender} [${time}]: ${content.body}\n`;
|
|
|
|
|
@@ -227,41 +236,50 @@ class OcrccBot {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleBotCommand(event) {
|
|
|
|
|
const botCommands = [{
|
|
|
|
|
keyword: 'transcript',
|
|
|
|
|
function: (senderId, roomId) => {
|
|
|
|
|
this.sendTranscript(senderId, roomId);
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
keyword: 'delete transcript',
|
|
|
|
|
function: (senderId, roomId) => {
|
|
|
|
|
this.deleteTranscript(senderId, roomId, true);
|
|
|
|
|
} // delete transcript and send confirmation message
|
|
|
|
|
|
|
|
|
|
}, {
|
|
|
|
|
keyword: 'say',
|
|
|
|
|
function: (senderId, roomId, message) => {
|
|
|
|
|
this.sendTextMessage(roomId, message, senderId);
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
keyword: 'hi',
|
|
|
|
|
function: (senderId, roomId) => {
|
|
|
|
|
const responses = ["Hi!", "Hello", "Hey :)", "Hi there", "Bleep bloop"];
|
|
|
|
|
const message = responses[Math.floor(Math.random() * responses.length)];
|
|
|
|
|
this.sendTextMessage(roomId, message, senderId);
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const senderId = event.getSender();
|
|
|
|
|
const roomId = event.getRoomId();
|
|
|
|
|
const content = event.getContent();
|
|
|
|
|
const command = content.body.substring("!bot".length).trim();
|
|
|
|
|
const commandText = content.body.substring("!bot".length).trim();
|
|
|
|
|
const command = botCommands.find(c => commandText.startsWith(c.keyword));
|
|
|
|
|
|
|
|
|
|
switch (command) {
|
|
|
|
|
case "transcript":
|
|
|
|
|
this.sendTranscript(senderId, roomId);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "transcript please":
|
|
|
|
|
this.sendTranscript(senderId, roomId);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "delete transcript":
|
|
|
|
|
this.deleteTranscript(senderId, roomId);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "hi":
|
|
|
|
|
const responses = ["Hi!", "Hello", "Hey :)", "Hi there", "Bleep bloop"];
|
|
|
|
|
const message = responses[Math.floor(Math.random() * responses.length)];
|
|
|
|
|
this.sendTextMessage(roomId, message, senderId);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
this.sendTextMessage(roomId, `Sorry, I don't know that command. I'm not a very smart bot.`, senderId);
|
|
|
|
|
break;
|
|
|
|
|
if (!command) {
|
|
|
|
|
this.sendTextMessage(roomId, `Sorry, I don't know that command. I'm not a very smart bot.`, senderId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const args = commandText.substring(command.keyword.length).trim();
|
|
|
|
|
command.function(senderId, roomId, args);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
_logger.default.log("error", `ERROR EXECUTING BOT COMMAND: ${err}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async leaveEmptyRooms(senderId) {
|
|
|
|
|
async leaveEmptyRooms() {
|
|
|
|
|
try {
|
|
|
|
|
const roomData = await this.client.getJoinedRooms();
|
|
|
|
|
const joinedRoomsIds = roomData["joined_rooms"];
|
|
|
|
|
@@ -270,16 +288,16 @@ class OcrccBot {
|
|
|
|
|
|
|
|
|
|
if (room && room.getJoinedMemberCount() === 1) {
|
|
|
|
|
try {
|
|
|
|
|
_logger.default.log('info', "LEAVING EMPTY ROOM => " + roomId);
|
|
|
|
|
|
|
|
|
|
await this.client.leave(roomId);
|
|
|
|
|
|
|
|
|
|
_logger.default.log('info', `LEAVING EMPTY ROOM => ${roomId}`);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
_logger.default.log('error', "ERROR LEAVING EMPTY ROOM => " + err);
|
|
|
|
|
_logger.default.log('error', `ERROR LEAVING ROOM ${roomId} => ${err}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
_logger.default.log("error", `ERROR GETTING JOINED ROOMS: ${err}`);
|
|
|
|
|
_logger.default.log("error", `ERROR LEAVING EMPTY ROOMS: ${err}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -288,7 +306,7 @@ class OcrccBot {
|
|
|
|
|
const transcriptFile = this.localStorage.getItem(`${roomId}-transcript`);
|
|
|
|
|
|
|
|
|
|
if (!transcriptFile) {
|
|
|
|
|
this.sendTextMessage(roomId, "There is no transcript for this chat.", senderId);
|
|
|
|
|
this.sendTextMessage(roomId, "Cannot send transcript, there is no transcript for this chat.", senderId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.client.isRoomEncrypted(roomId)) {
|
|
|
|
|
@@ -347,11 +365,11 @@ class OcrccBot {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deleteTranscript(senderId, roomId) {
|
|
|
|
|
deleteTranscript(senderId, roomId, sendConfirmation = false) {
|
|
|
|
|
const transcriptFile = this.localStorage.getItem(`${roomId}-transcript`);
|
|
|
|
|
|
|
|
|
|
if (!transcriptFile) {
|
|
|
|
|
return this.sendTextMessage(roomId, "There is no transcript for this chat.", senderId);
|
|
|
|
|
return this.sendTextMessage(roomId, "Cannot delete transcript, there is no transcript for this chat.", senderId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs.unlink(transcriptFile, err => {
|
|
|
|
|
@@ -363,11 +381,13 @@ class OcrccBot {
|
|
|
|
|
return this.sendTextMessage(roomId, `There was an error deleting the transcript: ${err}`, senderId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.localStorage.removeItem(`${roomId}-transcript`);
|
|
|
|
|
if (sendConfirmation) {
|
|
|
|
|
this.sendTextMessage(roomId, `The transcript file has been deleted.`, senderId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.default.log('info', "DELETED TRANSCRIPT FILE => " + transcriptFile);
|
|
|
|
|
|
|
|
|
|
this.sendTextMessage(roomId, `The transcript file has been deleted.`, senderId);
|
|
|
|
|
this.localStorage.removeItem(`${roomId}-transcript`);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -418,13 +438,14 @@ class OcrccBot {
|
|
|
|
|
|
|
|
|
|
_logger.default.log("info", "AUTO JOINED ROOM => " + room.roomId);
|
|
|
|
|
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
|
const chatDate = currentDate.toLocaleDateString();
|
|
|
|
|
const chatTime = currentDate.toLocaleTimeString();
|
|
|
|
|
const inviteDate = event.getDate();
|
|
|
|
|
const chatDate = inviteDate.toLocaleDateString();
|
|
|
|
|
const chatTime = inviteDate.toLocaleTimeString();
|
|
|
|
|
const roomId = room.roomId.split(':')[0];
|
|
|
|
|
const notification = `Incoming support chat at ${chatTime} (room ID: ${roomId})`;
|
|
|
|
|
this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, notification);
|
|
|
|
|
this.inviteFacilitators(room.roomId);
|
|
|
|
|
this.setTimeoutforFacilitator(room.roomId);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
_logger.default.log("error", "ERROR JOINING ROOM => " + err);
|
|
|
|
|
@@ -432,87 +453,150 @@ class OcrccBot {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (member.membership === "join" && member.userId !== this.config.BOT_USERID && this.localStorage.getItem(`${member.roomId}-waiting`)) {
|
|
|
|
|
// make sure it's a facilitator joining
|
|
|
|
|
const roomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID);
|
|
|
|
|
const members = Object.keys(roomMembers["joined"]);
|
|
|
|
|
const isFacilitator = members.includes(member.userId);
|
|
|
|
|
try {
|
|
|
|
|
// make sure it's a facilitator joining
|
|
|
|
|
const roomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID);
|
|
|
|
|
const members = Object.keys(roomMembers["joined"]);
|
|
|
|
|
const isFacilitator = members.includes(member.userId);
|
|
|
|
|
|
|
|
|
|
if (isFacilitator) {
|
|
|
|
|
// made facilitator a moderator in the room
|
|
|
|
|
this.localStorage.setItem(`${member.roomId}-facilitator`, member.userId);
|
|
|
|
|
const event = {
|
|
|
|
|
getType: () => {
|
|
|
|
|
return "m.room.power_levels";
|
|
|
|
|
},
|
|
|
|
|
getContent: () => {
|
|
|
|
|
return {
|
|
|
|
|
users: {
|
|
|
|
|
[this.config.BOT_USERID]: 100,
|
|
|
|
|
[member.userId]: 50
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
this.client.setPowerLevel(member.roomId, member.userId, 50, event); // send notification to Support Chat Notifications room
|
|
|
|
|
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
|
const chatTime = currentDate.toLocaleTimeString();
|
|
|
|
|
const roomId = member.roomId.split(':')[0];
|
|
|
|
|
const notification = `${member.name} joined the chat at ${chatTime} (room ID: ${roomId})`;
|
|
|
|
|
this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, notification); // send notification to chat room
|
|
|
|
|
|
|
|
|
|
this.sendTextMessage(member.roomId, `${member.name} has joined the chat.`); // revoke the other invitations
|
|
|
|
|
|
|
|
|
|
this.uninviteFacilitators(member.roomId); // set transcript file
|
|
|
|
|
|
|
|
|
|
if (this.config.CAPTURE_TRANSCRIPTS) {
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
|
const dateOpts = {
|
|
|
|
|
year: "numeric",
|
|
|
|
|
month: "short",
|
|
|
|
|
day: "numeric"
|
|
|
|
|
if (isFacilitator) {
|
|
|
|
|
// made facilitator a moderator in the room
|
|
|
|
|
this.localStorage.setItem(`${member.roomId}-facilitator`, member.userId);
|
|
|
|
|
const event = {
|
|
|
|
|
getType: () => {
|
|
|
|
|
return "m.room.power_levels";
|
|
|
|
|
},
|
|
|
|
|
getContent: () => {
|
|
|
|
|
return {
|
|
|
|
|
users: {
|
|
|
|
|
[this.config.BOT_USERID]: 100,
|
|
|
|
|
[member.userId]: 50
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const chatDate = currentDate.toLocaleDateString("en-GB", dateOpts);
|
|
|
|
|
const chatTime = currentDate.toLocaleTimeString("en-GB", {
|
|
|
|
|
timeZone: "America/New_York"
|
|
|
|
|
});
|
|
|
|
|
const filename = `${chatDate} - ${chatTime} - ${member.roomId}.txt`;
|
|
|
|
|
const filepath = path.resolve(path.join("transcripts", filename));
|
|
|
|
|
this.localStorage.setItem(`${member.roomId}-transcript`, filepath);
|
|
|
|
|
this.client.setPowerLevel(member.roomId, member.userId, 50, event); // send notification to Support Chat Notifications room
|
|
|
|
|
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
|
const chatTime = currentDate.toLocaleTimeString();
|
|
|
|
|
const roomId = member.roomId.split(':')[0];
|
|
|
|
|
const notification = `${member.name} joined the chat at ${chatTime} (room ID: ${roomId})`;
|
|
|
|
|
this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, notification); // send notification to chat room
|
|
|
|
|
|
|
|
|
|
this.sendTextMessage(member.roomId, `${member.name} has joined the chat.`); // revoke the other invitations
|
|
|
|
|
|
|
|
|
|
this.uninviteFacilitators(member.roomId); // set transcript file
|
|
|
|
|
|
|
|
|
|
if (this.config.CAPTURE_TRANSCRIPTS) {
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
|
const dateOpts = {
|
|
|
|
|
year: "numeric",
|
|
|
|
|
month: "short",
|
|
|
|
|
day: "numeric"
|
|
|
|
|
};
|
|
|
|
|
const chatDate = currentDate.toLocaleDateString("en-GB", dateOpts);
|
|
|
|
|
const chatTime = currentDate.toLocaleTimeString("en-GB", {
|
|
|
|
|
timeZone: "America/New_York"
|
|
|
|
|
});
|
|
|
|
|
const filename = `${chatDate} - ${chatTime} - ${member.roomId}.txt`;
|
|
|
|
|
const filepath = path.resolve(path.join("transcripts", filename));
|
|
|
|
|
this.localStorage.setItem(`${member.roomId}-transcript`, filepath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
_logger.default.log("error", `ERROR WHEN FACILITATOR JOINED ROOM ==> ${err}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (member.membership === "leave" && member.userId !== this.config.BOT_USERID) {
|
|
|
|
|
// ensure bot is still in the room
|
|
|
|
|
const roomData = await this.client.getJoinedRooms();
|
|
|
|
|
const joinedRooms = roomData["joined_rooms"];
|
|
|
|
|
const isBotInRoom = joinedRooms.includes(member.roomId);
|
|
|
|
|
const room = this.client.getRoom(member.roomId);
|
|
|
|
|
if (!room) return; // leave if there is nobody in the room
|
|
|
|
|
if (!room) return;
|
|
|
|
|
const roomMembers = await room.getJoinedMembers(); // array
|
|
|
|
|
|
|
|
|
|
const memberCount = room.getJoinedMemberCount();
|
|
|
|
|
const facilitatorRoomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID); // object
|
|
|
|
|
|
|
|
|
|
if (memberCount === 1 && isBotInRoom) {
|
|
|
|
|
// just the bot left
|
|
|
|
|
_logger.default.log("info", `LEAVING EMPTY ROOM ==> ${member.roomId}`);
|
|
|
|
|
const isBotInRoom = Boolean(roomMembers.find(member => member.userId === this.config.BOT_USERID)); // leave if there is nobody in the room
|
|
|
|
|
|
|
|
|
|
this.deleteTranscript(member.userId, member.roomId);
|
|
|
|
|
this.localStorage.removeItem(`${member.roomId}-facilitator`);
|
|
|
|
|
this.localStorage.removeItem(`${member.roomId}-transcript`);
|
|
|
|
|
return this.client.leave(member.roomId);
|
|
|
|
|
try {
|
|
|
|
|
const memberCount = roomMembers.length;
|
|
|
|
|
|
|
|
|
|
if (memberCount === 1 && isBotInRoom) {
|
|
|
|
|
// just the bot left
|
|
|
|
|
_logger.default.log("info", `LEAVING EMPTY ROOM: ${member.roomId}`);
|
|
|
|
|
|
|
|
|
|
this.deleteTranscript(member.userId, member.roomId);
|
|
|
|
|
this.localStorage.removeItem(`${member.roomId}-facilitator`);
|
|
|
|
|
this.localStorage.removeItem(`${member.roomId}-transcript`);
|
|
|
|
|
this.localStorage.removeItem(`${member.roomId}-waiting`);
|
|
|
|
|
return await this.client.leave(member.roomId);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
return _logger.default.log("error", `ERROR LEAVING EMPTY ROOM ==> ${err}`);
|
|
|
|
|
} // notify room if the facilitator has left
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`);
|
|
|
|
|
try {
|
|
|
|
|
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`);
|
|
|
|
|
|
|
|
|
|
if (isBotInRoom && member.userId === facilitatorId) {
|
|
|
|
|
this.sendTextMessage(member.roomId, `${member.name} has left the chat.`);
|
|
|
|
|
if (isBotInRoom && member.userId === facilitatorId) {
|
|
|
|
|
this.sendTextMessage(member.roomId, `${member.name} has left the chat.`); // send notification to Support Chat Notifications room
|
|
|
|
|
|
|
|
|
|
const currentDate = new Date();
|
|
|
|
|
const chatTime = currentDate.toLocaleTimeString();
|
|
|
|
|
const roomId = member.roomId.split(':')[0];
|
|
|
|
|
const notification = `${member.name} left the chat at ${chatTime} (room ID: ${roomId})`;
|
|
|
|
|
await this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, notification);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
_logger.default.log("error", `ERROR NOTIFYING THAT FACLITATOR HAS LEFT THE ROOM ==> ${err}`);
|
|
|
|
|
} // send signal to close the chat if there are no facilitators in the room
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const facilitators = facilitatorRoomMembers['joined'];
|
|
|
|
|
let facilitatorInRoom = false;
|
|
|
|
|
roomMembers.forEach(member => {
|
|
|
|
|
if (member.userId !== this.config.BOT_USERID && Boolean(facilitators[member.userId])) {
|
|
|
|
|
facilitatorInRoom = true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!facilitatorInRoom) {
|
|
|
|
|
_logger.default.log("info", `NO FACILITATORS LEFT, SENDING SIGNAL TO END CHAT`);
|
|
|
|
|
|
|
|
|
|
this.sendBotSignal(member.roomId, BOT_SIGNAL_END_CHAT);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
_logger.default.log("error", `ERROR SENDING BOT SIGNAL ==> ${err}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setTimeoutforFacilitator(roomId) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
const stillWaiting = this.localStorage.getItem(`${roomId}-waiting`);
|
|
|
|
|
|
|
|
|
|
if (stillWaiting) {
|
|
|
|
|
this.sendBotSignal(roomId, BOT_SIGNAL_END_CHAT);
|
|
|
|
|
}
|
|
|
|
|
}, this.config.MAX_WAIT_TIME);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setInactivityTimeout(roomId) {
|
|
|
|
|
const oldTimeout = this.inactivityTimers[roomId];
|
|
|
|
|
|
|
|
|
|
if (oldTimeout) {
|
|
|
|
|
clearTimeout(oldTimeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newTimeout = setTimeout(() => {
|
|
|
|
|
this.sendTextMessage(roomId, `This chat has been closed due to inactivity.`);
|
|
|
|
|
this.sendBotSignal(roomId, BOT_SIGNAL_END_CHAT);
|
|
|
|
|
}, this.config.MAX_INACTIVE);
|
|
|
|
|
this.inactivityTimers[roomId] = newTimeout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async setMessageListeners() {
|
|
|
|
|
// encrypted messages
|
|
|
|
|
this.client.on("Event.decrypted", (event, err) => {
|
|
|
|
|
@@ -532,22 +616,23 @@ class OcrccBot {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async leaveOldRooms() {
|
|
|
|
|
const roomData = await this.client.getJoinedRooms();
|
|
|
|
|
roomData["joined_rooms"].forEach(async roomId => {
|
|
|
|
|
try {
|
|
|
|
|
await this.client.leave(roomId);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
_logger.default.log('error', "ERROR LEAVING ROOM => " + err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
async sendBotSignal(roomId, signal, args) {
|
|
|
|
|
let content = {
|
|
|
|
|
signal: signal,
|
|
|
|
|
args: args
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await this.client.sendStateEvent(roomId, 'm.bot.signal', content);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
_logger.default.log('error', "ERROR SENDING BOT SIGNAL => " + err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async start() {
|
|
|
|
|
const localStorage = this.createLocalStorage();
|
|
|
|
|
this.localStorage = localStorage;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const localStorage = this.createLocalStorage();
|
|
|
|
|
this.localStorage = localStorage;
|
|
|
|
|
const auth = {
|
|
|
|
|
user: this.config.BOT_USERNAME,
|
|
|
|
|
password: this.config.BOT_PASSWORD,
|
|
|
|
|
@@ -574,6 +659,7 @@ class OcrccBot {
|
|
|
|
|
|
|
|
|
|
if (state === 'PREPARED') {
|
|
|
|
|
await this.deleteOldDevices();
|
|
|
|
|
await this.leaveEmptyRooms();
|
|
|
|
|
await this.trackJoinedRooms();
|
|
|
|
|
await this.setMembershipListeners();
|
|
|
|
|
await this.setMessageListeners();
|
|
|
|
|
|