close if facilitator doesn't join within 3 minutes

This commit is contained in:
Sharon Kennedy 2020-09-05 18:11:06 -04:00
parent 1e3b63fe5b
commit 6d4b6b1609
3 changed files with 167 additions and 101 deletions

120
dist/bot.js vendored
View File

@ -26,6 +26,7 @@ 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) {
@ -227,35 +228,43 @@ 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);
}
}, {
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}`);
}
@ -418,9 +427,9 @@ 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);
@ -489,25 +498,48 @@ class OcrccBot {
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
const memberCount = room.getJoinedMemberCount();
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`);
return this.client.leave(member.roomId);
} // notify room if the facilitator has left
if (!room) return; // notify room if the facilitator has left
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`);
if (isBotInRoom && member.userId === facilitatorId) {
this.sendTextMessage(member.roomId, `${member.name} has left the chat.`);
} // leave if there is nobody in the room
try {
const memberCount = room.getJoinedMemberCount();
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`);
return this.client.leave(member.roomId);
}
} catch (err) {
_logger.default.log("error", `ERROR LEAVING EMPTY ROOM ==> ${err}`);
} // send signal to close the chat if there are no facilitators in the room
try {
const roomMembers = await room.getJoinedMembers();
const facilitatorRoomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID);
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) {
this.sendBotSignal(member.roomId, BOT_SIGNAL_END_CHAT);
}
} catch (err) {
_logger.default.log("error", `ERROR SENDING BOT SIGNAL ==> ${err}`);
}
}
});
@ -543,6 +575,14 @@ class OcrccBot {
});
}
async sendBotSignal(roomId, signal, args) {
let content = {
signal: signal,
args: args
};
await this.client.sendStateEvent(roomId, 'm.bot.signal', content);
}
async start() {
const localStorage = this.createLocalStorage();
this.localStorage = localStorage;

View File

@ -443,6 +443,7 @@ class OcrccBot {
const notification = `Incoming support chat at ${chatTime} (room ID: ${roomId})`
this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, notification);
this.inviteFacilitators(room.roomId)
this.setTimeoutforFacilitators(room.roomId)
}
} catch(err) {
logger.log("error", "ERROR JOINING ROOM => " + err)
@ -454,61 +455,65 @@ class OcrccBot {
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.log("error", `ERROR WHEN FACILITATOR JOINED ROOM ==> ${err}`);
}
}
@ -524,13 +529,17 @@ class OcrccBot {
const room = this.client.getRoom(member.roomId)
if (!room) return;
// notify room if the facilitator has left
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`)
if (isBotInRoom && member.userId === facilitatorId) {
this.sendTextMessage(
member.roomId,
`${member.name} has left the chat.`
);
try {
// notify room if the facilitator has left
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`)
if (isBotInRoom && member.userId === facilitatorId) {
this.sendTextMessage(
member.roomId,
`${member.name} has left the chat.`
);
}
} catch(err) {
logger.log("error", `ERROR NOTIFYING THAT FACLITATOR HAS LEFT THE ROOM ==> ${err}`);
}
// leave if there is nobody in the room
@ -571,6 +580,15 @@ class OcrccBot {
})
}
setTimeoutforFacilitators(roomId) {
setTimeout(() => {
const stillWaiting = this.localStorage.getItem(`${roomId}-waiting`)
if (stillWaiting) {
this.sendBotSignal(roomId, BOT_SIGNAL_END_CHAT)
}
}, this.config.MAX_WAIT_TIME)
}
async setMessageListeners() {
// encrypted messages
this.client.on("Event.decrypted", (event, err) => {
@ -606,8 +624,11 @@ class OcrccBot {
signal: signal,
args: args,
}
await this.client.sendStateEvent(roomId, 'm.bot.signal', content)
try {
await this.client.sendStateEvent(roomId, 'm.bot.signal', content)
} catch(err) {
logger.log('error', "ERROR SENDING BOT SIGNAL => " + err)
}
}
async start() {

View File

@ -5,6 +5,7 @@ const KICK_REASON = "A facilitator has already joined this chat.";
const BOT_ERROR_MESSAGE =
"Something went wrong on our end, please restart the chat and try again.";
const MAX_RETRIES = 3;
const {
MATRIX_SERVER_URL,
BOT_USERNAME,
@ -13,6 +14,8 @@ const {
BOT_DISPLAY_NAME,
FACILITATOR_ROOM_ID,
CAPTURE_TRANSCRIPTS,
CHAT_NOT_AVAILABLE_MESSAGE,
MAX_WAIT_TIME,
} = process.env;
const botConfig = {
@ -27,6 +30,8 @@ const botConfig = {
BOT_DISPLAY_NAME,
FACILITATOR_ROOM_ID,
CAPTURE_TRANSCRIPTS,
CHAT_NOT_AVAILABLE_MESSAGE,
MAX_WAIT_TIME,
}
import OcrccBot from './bot'