change order of statements when handling member leaving to avoid sending close signal twice
This commit is contained in:
parent
9772b3b1fd
commit
d53a5c5e26
99
dist/bot.js
vendored
99
dist/bot.js
vendored
@ -48,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":
|
||||
@ -130,10 +130,9 @@ class OcrccBot {
|
||||
}
|
||||
|
||||
async inviteFacilitators(roomId) {
|
||||
try {
|
||||
this.localStorage.setItem(`${roomId}-waiting`, 'true');
|
||||
let invitations = [];
|
||||
|
||||
try {
|
||||
const roomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID);
|
||||
const members = Object.keys(roomMembers["joined"]);
|
||||
members.forEach(memberId => {
|
||||
@ -160,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"]);
|
||||
@ -185,7 +183,7 @@ 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) {
|
||||
@ -227,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`;
|
||||
@ -246,8 +244,9 @@ class OcrccBot {
|
||||
}, {
|
||||
keyword: 'delete transcript',
|
||||
function: (senderId, roomId) => {
|
||||
this.deleteTranscript(senderId, roomId);
|
||||
}
|
||||
this.deleteTranscript(senderId, roomId, true);
|
||||
} // delete transcript and send confirmation message
|
||||
|
||||
}, {
|
||||
keyword: 'say',
|
||||
function: (senderId, roomId, message) => {
|
||||
@ -280,7 +279,7 @@ class OcrccBot {
|
||||
}
|
||||
}
|
||||
|
||||
async leaveEmptyRooms(senderId) {
|
||||
async leaveEmptyRooms() {
|
||||
try {
|
||||
const roomData = await this.client.getJoinedRooms();
|
||||
const joinedRoomsIds = roomData["joined_rooms"];
|
||||
@ -289,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}`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -307,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)) {
|
||||
@ -366,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 => {
|
||||
@ -382,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`);
|
||||
});
|
||||
}
|
||||
|
||||
@ -514,33 +515,40 @@ class OcrccBot {
|
||||
|
||||
const facilitatorRoomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID); // object
|
||||
|
||||
const isBotInRoom = roomMembers.find(member => member.userId === this.config.BOT_USERID); // notify room if the facilitator has left
|
||||
|
||||
try {
|
||||
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.default.log("error", `ERROR NOTIFYING THAT FACLITATOR HAS LEFT THE ROOM ==> ${err}`);
|
||||
} // leave if there is nobody in the room
|
||||
|
||||
const isBotInRoom = Boolean(roomMembers.find(member => member.userId === this.config.BOT_USERID)); // leave if there is nobody in the room
|
||||
|
||||
try {
|
||||
const memberCount = roomMembers.length;
|
||||
|
||||
if (memberCount === 1 && isBotInRoom) {
|
||||
// just the bot left
|
||||
_logger.default.log("info", `LEAVING EMPTY ROOM ==> ${member.roomId}`);
|
||||
_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);
|
||||
this.localStorage.removeItem(`${member.roomId}-waiting`);
|
||||
return await this.client.leave(member.roomId);
|
||||
}
|
||||
} catch (err) {
|
||||
_logger.default.log("error", `ERROR LEAVING EMPTY ROOM ==> ${err}`);
|
||||
return _logger.default.log("error", `ERROR LEAVING EMPTY ROOM ==> ${err}`);
|
||||
} // notify room if the facilitator has left
|
||||
|
||||
|
||||
try {
|
||||
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`);
|
||||
|
||||
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
|
||||
|
||||
|
||||
@ -554,6 +562,8 @@ class OcrccBot {
|
||||
});
|
||||
|
||||
if (!facilitatorInRoom) {
|
||||
_logger.default.log("info", `NO FACILITATORS LEFT, SENDING SIGNAL TO END CHAT`);
|
||||
|
||||
this.sendBotSignal(member.roomId, BOT_SIGNAL_END_CHAT);
|
||||
}
|
||||
} catch (err) {
|
||||
@ -606,17 +616,6 @@ 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,
|
||||
@ -631,10 +630,9 @@ class OcrccBot {
|
||||
}
|
||||
|
||||
async start() {
|
||||
try {
|
||||
const localStorage = this.createLocalStorage();
|
||||
this.localStorage = localStorage;
|
||||
|
||||
try {
|
||||
const auth = {
|
||||
user: this.config.BOT_USERNAME,
|
||||
password: this.config.BOT_PASSWORD,
|
||||
@ -661,6 +659,7 @@ class OcrccBot {
|
||||
|
||||
if (state === 'PREPARED') {
|
||||
await this.deleteOldDevices();
|
||||
await this.leaveEmptyRooms();
|
||||
await this.trackJoinedRooms();
|
||||
await this.setMembershipListeners();
|
||||
await this.setMessageListeners();
|
||||
|
7
dist/index.js
vendored
7
dist/index.js
vendored
@ -41,4 +41,9 @@ const botConfig = {
|
||||
MAX_INACTIVE
|
||||
};
|
||||
const bot = new _bot.default(botConfig);
|
||||
bot.start();
|
||||
|
||||
try {
|
||||
bot.start();
|
||||
} catch (err) {
|
||||
console.log("AAAAAAAAAAAAA", err);
|
||||
}
|
94
src/bot.js
94
src/bot.js
@ -32,21 +32,20 @@ class OcrccBot {
|
||||
return new 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.log("info", `SENDING *NOTICE*: ${message}`)
|
||||
try {
|
||||
await this.client.sendNotice(roomId, message)
|
||||
logger.log("info", `SENT *NOTICE*: ${message}`)
|
||||
} catch(err) {
|
||||
switch (err["name"]) {
|
||||
case "UnknownDeviceError":
|
||||
@ -109,10 +108,9 @@ class OcrccBot {
|
||||
}
|
||||
|
||||
async inviteFacilitators(roomId) {
|
||||
try {
|
||||
this.localStorage.setItem(`${roomId}-waiting`, 'true')
|
||||
let invitations = []
|
||||
|
||||
try {
|
||||
const roomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID)
|
||||
const members = Object.keys(roomMembers["joined"]);
|
||||
|
||||
@ -143,9 +141,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)
|
||||
|
||||
@ -173,7 +170,7 @@ class OcrccBot {
|
||||
|
||||
this.sendTextMessage(
|
||||
this.config.FACILITATOR_ROOM_ID,
|
||||
`The Help Bot ran into an error: ${error}. Please verify that the chat service is working.`
|
||||
`${this.config.BOT_DISPLAY_NAME} ran into an error: ${error}. Please verify that the chat service is working.`
|
||||
);
|
||||
}
|
||||
|
||||
@ -216,7 +213,7 @@ class OcrccBot {
|
||||
const filepath = this.localStorage.getItem(`${roomId}-transcript`)
|
||||
|
||||
if (!filepath) {
|
||||
return logger.log("error", `NO TRANSCRIPT FILE FOR ROOM: ${roomId}`);
|
||||
return logger.log("error", `NO TRANSCRIPT FILE FOR ROOM: ${roomId}. This message will not be added: ${content.body}`);
|
||||
}
|
||||
|
||||
const message = `${sender} [${time}]: ${content.body}\n`;
|
||||
@ -235,7 +232,7 @@ class OcrccBot {
|
||||
},
|
||||
{
|
||||
keyword: 'delete transcript',
|
||||
function: (senderId, roomId) => { this.deleteTranscript(senderId, roomId) }
|
||||
function: (senderId, roomId) => { this.deleteTranscript(senderId, roomId, true) } // delete transcript and send confirmation message
|
||||
},
|
||||
{
|
||||
keyword: 'say',
|
||||
@ -280,7 +277,7 @@ class OcrccBot {
|
||||
}
|
||||
}
|
||||
|
||||
async leaveEmptyRooms(senderId) {
|
||||
async leaveEmptyRooms() {
|
||||
try {
|
||||
const roomData = await this.client.getJoinedRooms()
|
||||
const joinedRoomsIds = roomData["joined_rooms"]
|
||||
@ -288,15 +285,15 @@ class OcrccBot {
|
||||
const room = this.client.getRoom(roomId)
|
||||
if (room && room.getJoinedMemberCount() === 1) {
|
||||
try {
|
||||
logger.log('info', "LEAVING EMPTY ROOM => " + roomId)
|
||||
await this.client.leave(roomId)
|
||||
logger.log('info', `LEAVING EMPTY ROOM => ${roomId}`)
|
||||
} catch(err) {
|
||||
logger.log('error', "ERROR LEAVING EMPTY ROOM => " + err)
|
||||
logger.log('error', `ERROR LEAVING ROOM ${roomId} => ${err}`)
|
||||
}
|
||||
}
|
||||
})
|
||||
} catch(err) {
|
||||
logger.log("error", `ERROR GETTING JOINED ROOMS: ${err}`);
|
||||
logger.log("error", `ERROR LEAVING EMPTY ROOMS: ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -307,7 +304,7 @@ class OcrccBot {
|
||||
if (!transcriptFile) {
|
||||
this.sendTextMessage(
|
||||
roomId,
|
||||
"There is no transcript for this chat.",
|
||||
"Cannot send transcript, there is no transcript for this chat.",
|
||||
senderId
|
||||
);
|
||||
}
|
||||
@ -370,13 +367,13 @@ 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.",
|
||||
"Cannot delete transcript, there is no transcript for this chat.",
|
||||
senderId
|
||||
);
|
||||
}
|
||||
@ -392,13 +389,17 @@ class OcrccBot {
|
||||
senderId
|
||||
);
|
||||
}
|
||||
this.localStorage.removeItem(`${roomId}-transcript`)
|
||||
logger.log('info', "DELETED TRANSCRIPT FILE => " + transcriptFile)
|
||||
|
||||
if (sendConfirmation) {
|
||||
this.sendTextMessage(
|
||||
roomId,
|
||||
`The transcript file has been deleted.`,
|
||||
senderId
|
||||
);
|
||||
}
|
||||
|
||||
logger.log('info', "DELETED TRANSCRIPT FILE => " + transcriptFile)
|
||||
this.localStorage.removeItem(`${roomId}-transcript`)
|
||||
});
|
||||
}
|
||||
|
||||
@ -535,7 +536,22 @@ class OcrccBot {
|
||||
|
||||
const roomMembers = await room.getJoinedMembers() // array
|
||||
const facilitatorRoomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID) // object
|
||||
const isBotInRoom = roomMembers.find(member => member.userId === this.config.BOT_USERID)
|
||||
const isBotInRoom = Boolean(roomMembers.find(member => member.userId === this.config.BOT_USERID))
|
||||
|
||||
// leave if there is nobody in the room
|
||||
try {
|
||||
const memberCount = roomMembers.length
|
||||
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`)
|
||||
this.localStorage.removeItem(`${member.roomId}-waiting`)
|
||||
return await this.client.leave(member.roomId)
|
||||
}
|
||||
} catch(err) {
|
||||
return logger.log("error", `ERROR LEAVING EMPTY ROOM ==> ${err}`);
|
||||
}
|
||||
|
||||
// notify room if the facilitator has left
|
||||
try {
|
||||
@ -545,25 +561,17 @@ class OcrccBot {
|
||||
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.log("error", `ERROR NOTIFYING THAT FACLITATOR HAS LEFT THE ROOM ==> ${err}`);
|
||||
}
|
||||
|
||||
// leave if there is nobody in the room
|
||||
try {
|
||||
const memberCount = roomMembers.length
|
||||
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 facilitators = facilitatorRoomMembers['joined']
|
||||
@ -576,6 +584,7 @@ class OcrccBot {
|
||||
})
|
||||
|
||||
if (!facilitatorInRoom) {
|
||||
logger.log("info", `NO FACILITATORS LEFT, SENDING SIGNAL TO END CHAT`);
|
||||
this.sendBotSignal(member.roomId, BOT_SIGNAL_END_CHAT)
|
||||
}
|
||||
|
||||
@ -631,18 +640,6 @@ 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.log('error', "ERROR LEAVING ROOM => " + err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async sendBotSignal (roomId, signal, args) {
|
||||
let content = {
|
||||
signal: signal,
|
||||
@ -656,10 +653,10 @@ class OcrccBot {
|
||||
}
|
||||
|
||||
async start() {
|
||||
try {
|
||||
const localStorage = this.createLocalStorage();
|
||||
this.localStorage = localStorage
|
||||
|
||||
try {
|
||||
const auth = {
|
||||
user: this.config.BOT_USERNAME,
|
||||
password: this.config.BOT_PASSWORD,
|
||||
@ -684,6 +681,7 @@ class OcrccBot {
|
||||
logger.log("info", `SYNC STATUS: ${state}`)
|
||||
if (state === 'PREPARED') {
|
||||
await this.deleteOldDevices()
|
||||
await this.leaveEmptyRooms()
|
||||
await this.trackJoinedRooms()
|
||||
await this.setMembershipListeners();
|
||||
await this.setMessageListeners();
|
||||
|
@ -39,4 +39,8 @@ const botConfig = {
|
||||
import OcrccBot from './bot'
|
||||
|
||||
const bot = new OcrccBot(botConfig);
|
||||
bot.start();
|
||||
try {
|
||||
bot.start();
|
||||
} catch(err) {
|
||||
console.log("AAAAAAAAAAAAA", err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user