close if facilitator doesn't join within 3 minutes
This commit is contained in:
120
dist/bot.js
vendored
120
dist/bot.js
vendored
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user