Merge pull request #2 from nomadic-labs/botsignal
Move timeouts to bot and use custom event to signal clients to close
This commit is contained in:
commit
836d4751ad
@ -157,6 +157,10 @@ export const mockGetGroupUsers = jest.fn(() => {
|
|||||||
|
|
||||||
export const mockGetUser = jest.fn().mockReturnValue({ presence: 'online'});
|
export const mockGetUser = jest.fn().mockReturnValue({ presence: 'online'});
|
||||||
|
|
||||||
|
export const mockSendStateEvent = jest.fn(() => {
|
||||||
|
return Promise.resolve();
|
||||||
|
});
|
||||||
|
|
||||||
export const mockClient = {
|
export const mockClient = {
|
||||||
registerRequest: mockRegisterRequest,
|
registerRequest: mockRegisterRequest,
|
||||||
initCrypto: mockInitCrypto,
|
initCrypto: mockInitCrypto,
|
||||||
@ -186,6 +190,7 @@ export const mockClient = {
|
|||||||
getJoinedRoomMembers: mockGetJoinedRoomMembers,
|
getJoinedRoomMembers: mockGetJoinedRoomMembers,
|
||||||
getUser: mockGetUser,
|
getUser: mockGetUser,
|
||||||
getGroupUsers: mockGetGroupUsers,
|
getGroupUsers: mockGetGroupUsers,
|
||||||
|
sendStateEvent: mockSendStateEvent,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const WebStorageSessionStore = jest.fn()
|
export const WebStorageSessionStore = jest.fn()
|
||||||
|
100
dist/bot.js
vendored
100
dist/bot.js
vendored
@ -26,6 +26,7 @@ var _logger = _interopRequireDefault(require("./logger"));
|
|||||||
var _encryptAttachment = _interopRequireDefault(require("./encrypt-attachment"));
|
var _encryptAttachment = _interopRequireDefault(require("./encrypt-attachment"));
|
||||||
|
|
||||||
global.Olm = require("olm");
|
global.Olm = require("olm");
|
||||||
|
const BOT_SIGNAL_END_CHAT = 'END_CHAT';
|
||||||
|
|
||||||
class OcrccBot {
|
class OcrccBot {
|
||||||
constructor(botConfig) {
|
constructor(botConfig) {
|
||||||
@ -227,35 +228,43 @@ class OcrccBot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleBotCommand(event) {
|
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 {
|
try {
|
||||||
const senderId = event.getSender();
|
const senderId = event.getSender();
|
||||||
const roomId = event.getRoomId();
|
const roomId = event.getRoomId();
|
||||||
const content = event.getContent();
|
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) {
|
if (!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);
|
this.sendTextMessage(roomId, `Sorry, I don't know that command. I'm not a very smart bot.`, senderId);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const args = commandText.substring(command.keyword.length).trim();
|
||||||
|
command.function(senderId, roomId, args);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
_logger.default.log("error", `ERROR EXECUTING BOT COMMAND: ${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);
|
_logger.default.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);
|
||||||
@ -489,8 +498,16 @@ class OcrccBot {
|
|||||||
const joinedRooms = roomData["joined_rooms"];
|
const joinedRooms = roomData["joined_rooms"];
|
||||||
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; // leave if there is nobody in the room
|
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();
|
const memberCount = room.getJoinedMemberCount();
|
||||||
|
|
||||||
if (memberCount === 1 && isBotInRoom) {
|
if (memberCount === 1 && isBotInRoom) {
|
||||||
@ -501,13 +518,28 @@ class OcrccBot {
|
|||||||
this.localStorage.removeItem(`${member.roomId}-facilitator`);
|
this.localStorage.removeItem(`${member.roomId}-facilitator`);
|
||||||
this.localStorage.removeItem(`${member.roomId}-transcript`);
|
this.localStorage.removeItem(`${member.roomId}-transcript`);
|
||||||
return this.client.leave(member.roomId);
|
return this.client.leave(member.roomId);
|
||||||
} // notify room if the facilitator has left
|
}
|
||||||
|
} 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
|
||||||
|
|
||||||
|
|
||||||
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`);
|
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 (isBotInRoom && member.userId === facilitatorId) {
|
if (!facilitatorInRoom) {
|
||||||
this.sendTextMessage(member.roomId, `${member.name} has left the chat.`);
|
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() {
|
async start() {
|
||||||
const localStorage = this.createLocalStorage();
|
const localStorage = this.createLocalStorage();
|
||||||
this.localStorage = localStorage;
|
this.localStorage = localStorage;
|
||||||
|
172
src/bot.js
172
src/bot.js
@ -11,12 +11,15 @@ 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) {
|
||||||
this.config = botConfig
|
this.config = botConfig
|
||||||
this.client = matrix.createClient(this.config.MATRIX_SERVER_URL);
|
this.client = matrix.createClient(this.config.MATRIX_SERVER_URL);
|
||||||
this.joinedRooms = [];
|
this.joinedRooms = [];
|
||||||
|
this.inactivityTimers = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
createLocalStorage() {
|
createLocalStorage() {
|
||||||
@ -176,12 +179,20 @@ class OcrccBot {
|
|||||||
|
|
||||||
handleMessageEvent(event) {
|
handleMessageEvent(event) {
|
||||||
const content = event.getContent();
|
const content = event.getContent();
|
||||||
|
const sender = event.getSender()
|
||||||
|
const roomId = event.getRoomId();
|
||||||
|
|
||||||
// do nothing if there's no content
|
// do nothing if there's no content
|
||||||
if (!content) {
|
if (!content) {
|
||||||
return;
|
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
|
// bot commands
|
||||||
if (content.body.startsWith("!bot")) {
|
if (content.body.startsWith("!bot")) {
|
||||||
return this.handleBotCommand(event);
|
return this.handleBotCommand(event);
|
||||||
@ -217,23 +228,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 +253,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:
|
}
|
||||||
|
]
|
||||||
|
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(
|
this.sendTextMessage(
|
||||||
roomId,
|
roomId,
|
||||||
`Sorry, I don't know that command. I'm not a very smart bot.`,
|
`Sorry, I don't know that command. I'm not a very smart bot.`,
|
||||||
senderId
|
senderId
|
||||||
);
|
);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,13 +445,14 @@ 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);
|
||||||
this.inviteFacilitators(room.roomId)
|
this.inviteFacilitators(room.roomId)
|
||||||
|
this.setTimeoutforFacilitator(room.roomId)
|
||||||
}
|
}
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
logger.log("error", "ERROR JOINING ROOM => " + err)
|
logger.log("error", "ERROR JOINING ROOM => " + err)
|
||||||
@ -441,6 +464,7 @@ class OcrccBot {
|
|||||||
member.userId !== this.config.BOT_USERID &&
|
member.userId !== this.config.BOT_USERID &&
|
||||||
this.localStorage.getItem(`${member.roomId}-waiting`)
|
this.localStorage.getItem(`${member.roomId}-waiting`)
|
||||||
) {
|
) {
|
||||||
|
try {
|
||||||
// make sure it's a facilitator joining
|
// make sure it's a facilitator joining
|
||||||
const roomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID)
|
const roomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID)
|
||||||
const members = Object.keys(roomMembers["joined"]);
|
const members = Object.keys(roomMembers["joined"]);
|
||||||
@ -497,31 +521,24 @@ class OcrccBot {
|
|||||||
this.localStorage.setItem(`${member.roomId}-transcript`, filepath)
|
this.localStorage.setItem(`${member.roomId}-transcript`, filepath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch(err) {
|
||||||
|
logger.log("error", `ERROR WHEN FACILITATOR JOINED ROOM ==> ${err}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
member.membership === "leave" &&
|
member.membership === "leave" &&
|
||||||
member.userId !== this.config.BOT_USERID
|
member.userId !== this.config.BOT_USERID
|
||||||
) {
|
) {
|
||||||
// ensure bot is still in the room
|
|
||||||
const roomData = await this.client.getJoinedRooms()
|
|
||||||
const joinedRooms = roomData["joined_rooms"]
|
|
||||||
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 roomMembers = await room.getJoinedMembers() // array
|
||||||
const memberCount = room.getJoinedMemberCount()
|
const facilitatorRoomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID) // object
|
||||||
if (memberCount === 1 && isBotInRoom) { // just the bot left
|
const isBotInRoom = roomMembers.find(member => member.userId === this.config.BOT_USERID)
|
||||||
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
|
||||||
|
try {
|
||||||
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`)
|
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`)
|
||||||
if (isBotInRoom && member.userId === facilitatorId) {
|
if (isBotInRoom && member.userId === facilitatorId) {
|
||||||
this.sendTextMessage(
|
this.sendTextMessage(
|
||||||
@ -529,8 +546,71 @@ class OcrccBot {
|
|||||||
`${member.name} has left the chat.`
|
`${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
|
||||||
|
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']
|
||||||
|
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}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeoutforFacilitator(roomId) {
|
||||||
|
setTimeout(() => {
|
||||||
|
const stillWaiting = this.localStorage.getItem(`${roomId}-waiting`)
|
||||||
|
if (stillWaiting) {
|
||||||
|
this.sendBotSignal(roomId, BOT_SIGNAL_END_CHAT)
|
||||||
|
}
|
||||||
|
}, 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() {
|
async setMessageListeners() {
|
||||||
@ -563,6 +643,18 @@ class OcrccBot {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async sendBotSignal (roomId, signal, args) {
|
||||||
|
let content = {
|
||||||
|
signal: signal,
|
||||||
|
args: args,
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await this.client.sendStateEvent(roomId, 'm.bot.signal', content)
|
||||||
|
} catch(err) {
|
||||||
|
logger.log('error', "ERROR SENDING BOT SIGNAL => " + err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async start() {
|
async start() {
|
||||||
const localStorage = this.createLocalStorage();
|
const localStorage = this.createLocalStorage();
|
||||||
this.localStorage = localStorage
|
this.localStorage = localStorage
|
||||||
|
@ -32,7 +32,8 @@ import {
|
|||||||
mockKick,
|
mockKick,
|
||||||
mockGetJoinedRoomMembers,
|
mockGetJoinedRoomMembers,
|
||||||
mockGetUser,
|
mockGetUser,
|
||||||
mockGetGroupUsers
|
mockGetGroupUsers,
|
||||||
|
mockSendStateEvent,
|
||||||
} from "matrix-js-sdk";
|
} from "matrix-js-sdk";
|
||||||
|
|
||||||
import OcrccBot from './bot'
|
import OcrccBot from './bot'
|
||||||
@ -100,6 +101,7 @@ describe('OcrccBot', () => {
|
|||||||
mockSendTextMessage.mockClear()
|
mockSendTextMessage.mockClear()
|
||||||
mockAppendFileSync.mockClear()
|
mockAppendFileSync.mockClear()
|
||||||
mockGetGroupUsers.mockClear()
|
mockGetGroupUsers.mockClear()
|
||||||
|
mockSendStateEvent.mockClear()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@ -325,4 +327,17 @@ describe('OcrccBot', () => {
|
|||||||
expect(mockStartClient).toHaveBeenCalled()
|
expect(mockStartClient).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('#sendBotSignal should send custom state event', () => {
|
||||||
|
const bot = new OcrccBot(botConfig)
|
||||||
|
bot.start()
|
||||||
|
const test_room_id = 'test_room_id'
|
||||||
|
const signal = 'END_CHAT'
|
||||||
|
|
||||||
|
bot.sendBotSignal(test_room_id, signal)
|
||||||
|
|
||||||
|
waitForExpect(() => {
|
||||||
|
expect(mockSendStateEvent).toHaveBeenCalledWith(test_room_id, 'm.bot.signal', { signal })
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
@ -5,6 +5,7 @@ const KICK_REASON = "A facilitator has already joined this chat.";
|
|||||||
const BOT_ERROR_MESSAGE =
|
const BOT_ERROR_MESSAGE =
|
||||||
"Something went wrong on our end, please restart the chat and try again.";
|
"Something went wrong on our end, please restart the chat and try again.";
|
||||||
const MAX_RETRIES = 3;
|
const MAX_RETRIES = 3;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
MATRIX_SERVER_URL,
|
MATRIX_SERVER_URL,
|
||||||
BOT_USERNAME,
|
BOT_USERNAME,
|
||||||
@ -13,6 +14,9 @@ const {
|
|||||||
BOT_DISPLAY_NAME,
|
BOT_DISPLAY_NAME,
|
||||||
FACILITATOR_ROOM_ID,
|
FACILITATOR_ROOM_ID,
|
||||||
CAPTURE_TRANSCRIPTS,
|
CAPTURE_TRANSCRIPTS,
|
||||||
|
CHAT_NOT_AVAILABLE_MESSAGE,
|
||||||
|
MAX_WAIT_TIME,
|
||||||
|
MAX_INACTIVE,
|
||||||
} = process.env;
|
} = process.env;
|
||||||
|
|
||||||
const botConfig = {
|
const botConfig = {
|
||||||
@ -27,6 +31,9 @@ const botConfig = {
|
|||||||
BOT_DISPLAY_NAME,
|
BOT_DISPLAY_NAME,
|
||||||
FACILITATOR_ROOM_ID,
|
FACILITATOR_ROOM_ID,
|
||||||
CAPTURE_TRANSCRIPTS,
|
CAPTURE_TRANSCRIPTS,
|
||||||
|
CHAT_NOT_AVAILABLE_MESSAGE,
|
||||||
|
MAX_WAIT_TIME,
|
||||||
|
MAX_INACTIVE,
|
||||||
}
|
}
|
||||||
|
|
||||||
import OcrccBot from './bot'
|
import OcrccBot from './bot'
|
||||||
|
Loading…
Reference in New Issue
Block a user