fixed bug on leaving room

This commit is contained in:
Sharon Kennedy 2020-09-05 14:57:25 -04:00
parent 9fb9d442fe
commit 1e3b63fe5b

View File

@ -11,6 +11,8 @@ import * as matrix from "matrix-js-sdk";
import logger from "./logger"; import logger from "./logger";
import encrypt from "./encrypt-attachment"; import encrypt from "./encrypt-attachment";
const BOT_SIGNAL_END_CHAT = 'END_CHAT'
class OcrccBot { class OcrccBot {
constructor(botConfig) { constructor(botConfig) {
@ -217,23 +219,24 @@ class OcrccBot {
} }
handleBotCommand(event) { handleBotCommand(event) {
try { const botCommands = [
const senderId = event.getSender(); {
const roomId = event.getRoomId(); keyword: 'transcript',
const content = event.getContent(); function: (senderId, roomId) => { this.sendTranscript(senderId, roomId) }
const command = content.body.substring("!bot".length).trim(); },
{
switch (command) { keyword: 'delete transcript',
case "transcript": function: (senderId, roomId) => { this.deleteTranscript(senderId, roomId) }
this.sendTranscript(senderId, roomId); },
break; {
case "transcript please": keyword: 'say',
this.sendTranscript(senderId, roomId); function: (senderId, roomId, message) => {
break; this.sendTextMessage(roomId, message, senderId);
case "delete transcript": }
this.deleteTranscript(senderId, roomId); },
break; {
case "hi": keyword: 'hi',
function: (senderId, roomId) => {
const responses = [ const responses = [
"Hi!", "Hi!",
"Hello", "Hello",
@ -241,18 +244,28 @@ class OcrccBot {
"Hi there", "Hi there",
"Bleep bloop" "Bleep bloop"
]; ];
const message = const message = responses[Math.floor(Math.random() * responses.length)];
responses[Math.floor(Math.random() * responses.length)];
this.sendTextMessage(roomId, message, senderId); 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;
} }
]
try {
const senderId = event.getSender();
const roomId = event.getRoomId();
const content = event.getContent();
const commandText = content.body.substring("!bot".length).trim();
const command = botCommands.find(c => commandText.startsWith(c.keyword))
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) { } catch (err) {
logger.log("error", `ERROR EXECUTING BOT COMMAND: ${err}`); logger.log("error", `ERROR EXECUTING BOT COMMAND: ${err}`);
} }
@ -423,9 +436,9 @@ class OcrccBot {
if (!joinedRooms.includes(member.roomId)) { if (!joinedRooms.includes(member.roomId)) {
const room = await this.client.joinRoom(member.roomId) const room = await this.client.joinRoom(member.roomId)
logger.log("info", "AUTO JOINED ROOM => " + room.roomId) logger.log("info", "AUTO JOINED ROOM => " + room.roomId)
const currentDate = new Date() const inviteDate = event.getDate()
const chatDate = currentDate.toLocaleDateString() const chatDate = inviteDate.toLocaleDateString()
const chatTime = currentDate.toLocaleTimeString() const chatTime = inviteDate.toLocaleTimeString()
const roomId = room.roomId.split(':')[0] const roomId = room.roomId.split(':')[0]
const notification = `Incoming support chat at ${chatTime} (room ID: ${roomId})` const notification = `Incoming support chat at ${chatTime} (room ID: ${roomId})`
this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, notification); this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, notification);
@ -509,17 +522,7 @@ class OcrccBot {
const isBotInRoom = joinedRooms.includes(member.roomId) const isBotInRoom = joinedRooms.includes(member.roomId)
const room = this.client.getRoom(member.roomId) const room = this.client.getRoom(member.roomId)
if (!room) return if (!room) return;
// leave if there is nobody in the room
const memberCount = room.getJoinedMemberCount()
if (memberCount === 1 && isBotInRoom) { // just the bot left
logger.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 // notify room if the facilitator has left
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`) const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`)
@ -529,6 +532,41 @@ class OcrccBot {
`${member.name} has left the chat.` `${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.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.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.log("error", `ERROR SENDING BOT SIGNAL ==> ${err}`);
}
} }
}) })
} }
@ -563,6 +601,15 @@ class OcrccBot {
}) })
} }
async sendBotSignal (roomId, signal, args) {
let content = {
signal: signal,
args: args,
}
await this.client.sendStateEvent(roomId, 'm.bot.signal', content)
}
async start() { async start() {
const localStorage = this.createLocalStorage(); const localStorage = this.createLocalStorage();
this.localStorage = localStorage this.localStorage = localStorage