timeouts
This commit is contained in:
parent
6d4b6b1609
commit
5dc6aa5660
31
src/bot.js
31
src/bot.js
@ -19,6 +19,7 @@ class OcrccBot {
|
||||
this.config = botConfig
|
||||
this.client = matrix.createClient(this.config.MATRIX_SERVER_URL);
|
||||
this.joinedRooms = [];
|
||||
this.inactivityTimers = {};
|
||||
}
|
||||
|
||||
createLocalStorage() {
|
||||
@ -178,12 +179,20 @@ class OcrccBot {
|
||||
|
||||
handleMessageEvent(event) {
|
||||
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
|
||||
if (content.body.startsWith("!bot")) {
|
||||
return this.handleBotCommand(event);
|
||||
@ -443,7 +452,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)
|
||||
this.setTimeoutforFacilitator(room.roomId)
|
||||
}
|
||||
} catch(err) {
|
||||
logger.log("error", "ERROR JOINING ROOM => " + err)
|
||||
@ -580,7 +589,7 @@ class OcrccBot {
|
||||
})
|
||||
}
|
||||
|
||||
setTimeoutforFacilitators(roomId) {
|
||||
setTimeoutforFacilitator(roomId) {
|
||||
setTimeout(() => {
|
||||
const stillWaiting = this.localStorage.getItem(`${roomId}-waiting`)
|
||||
if (stillWaiting) {
|
||||
@ -589,6 +598,24 @@ class OcrccBot {
|
||||
}, 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) => {
|
||||
|
@ -16,6 +16,7 @@ const {
|
||||
CAPTURE_TRANSCRIPTS,
|
||||
CHAT_NOT_AVAILABLE_MESSAGE,
|
||||
MAX_WAIT_TIME,
|
||||
MAX_INACTIVE,
|
||||
} = process.env;
|
||||
|
||||
const botConfig = {
|
||||
@ -32,6 +33,7 @@ const botConfig = {
|
||||
CAPTURE_TRANSCRIPTS,
|
||||
CHAT_NOT_AVAILABLE_MESSAGE,
|
||||
MAX_WAIT_TIME,
|
||||
MAX_INACTIVE,
|
||||
}
|
||||
|
||||
import OcrccBot from './bot'
|
||||
|
Loading…
Reference in New Issue
Block a user