Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5ac3b9d367 | ||
|
|
f8a1698c56 | ||
|
|
4cddeae508 | ||
|
|
a323602a1d | ||
|
|
21a15c5efc | ||
|
|
16c9fd4148 |
@@ -3,5 +3,6 @@ BOT_DISPLAY_NAME=
|
|||||||
BOT_USERNAME=
|
BOT_USERNAME=
|
||||||
BOT_PASSWORD=
|
BOT_PASSWORD=
|
||||||
BOT_USERID=
|
BOT_USERID=
|
||||||
|
FACILITATOR_GROUP_ID=
|
||||||
FACILITATOR_ROOM_ID=
|
FACILITATOR_ROOM_ID=
|
||||||
CHAT_OFFLINE_MESSAGE=
|
CAPTURE_TRANSCRIPTS
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "private-safesupport-bot",
|
"name": "private-safesupport-bot",
|
||||||
"version": "1.0.1",
|
"version": "1.1.3",
|
||||||
"description": "Chatbot to manage interactions on Safe Support Chat",
|
"description": "Chatbot to manage interactions on Safe Support Chat",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"matrix-js-sdk": "^5.0.1",
|
"matrix-js-sdk": "^6.2.1",
|
||||||
"node-localstorage": "^2.1.5",
|
"node-localstorage": "^2.1.5",
|
||||||
"olm": "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz",
|
"olm": "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz",
|
||||||
"uuidv4": "^6.0.2",
|
"uuidv4": "^6.0.2",
|
||||||
|
|||||||
133
src/bot.js
133
src/bot.js
@@ -106,7 +106,7 @@ class OcrccBot {
|
|||||||
|
|
||||||
async inviteFacilitators(roomId) {
|
async inviteFacilitators(roomId) {
|
||||||
this.localStorage.setItem(`${roomId}-waiting`, 'true')
|
this.localStorage.setItem(`${roomId}-waiting`, 'true')
|
||||||
let chatOffline = true;
|
let invitations = []
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const roomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID)
|
const roomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID)
|
||||||
@@ -116,18 +116,19 @@ class OcrccBot {
|
|||||||
const user = this.client.getUser(memberId);
|
const user = this.client.getUser(memberId);
|
||||||
if (
|
if (
|
||||||
user &&
|
user &&
|
||||||
user.presence === "online" &&
|
(user.presence !== "offline") &&
|
||||||
memberId !== this.config.BOT_USERID
|
memberId !== this.config.BOT_USERID
|
||||||
) {
|
) {
|
||||||
chatOffline = false;
|
invitations.push(memberId)
|
||||||
this.inviteUserToRoom(roomId, memberId);
|
this.inviteUserToRoom(roomId, memberId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (chatOffline) {
|
if (invitations.length > 0) {
|
||||||
|
this.localStorage.setItem(`${roomId}-invitations`, invitations)
|
||||||
|
} else {
|
||||||
logger.log('info', "NO FACILITATORS ONLINE")
|
logger.log('info', "NO FACILITATORS ONLINE")
|
||||||
this.sendTextMessage(roomId, this.config.CHAT_OFFLINE_MESSAGE);
|
this.sendNotice(roomId, "CHAT_OFFLINE")
|
||||||
this.sendNotice(roomId, "Chat is offline")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
@@ -289,17 +290,20 @@ class OcrccBot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const filename = path.basename(transcriptFile) || "Transcript";
|
const filename = path.basename(transcriptFile) || "Transcript";
|
||||||
const stream = fs.createReadStream(transcriptFile);
|
const file = fs.readFileSync(transcriptFile);
|
||||||
|
const stats = fs.statSync(transcriptFile);
|
||||||
|
|
||||||
const contentUrl = await this.client.uploadContent({
|
const url = await this.client.uploadContent(file, { rawResponse: false, type: 'text/plain' })
|
||||||
stream: stream,
|
logger.log('info', url)
|
||||||
name: filename
|
|
||||||
})
|
|
||||||
|
|
||||||
const content = {
|
const content = {
|
||||||
msgtype: "m.file",
|
msgtype: "m.file",
|
||||||
body: filename,
|
body: filename,
|
||||||
url: JSON.parse(contentUrl).content_uri,
|
info: {
|
||||||
|
size: stats.size,
|
||||||
|
mimetype: 'text/plain'
|
||||||
|
},
|
||||||
|
url: url.content_uri,
|
||||||
showToUser: senderId
|
showToUser: senderId
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -386,61 +390,78 @@ class OcrccBot {
|
|||||||
try {
|
try {
|
||||||
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)
|
||||||
this.sendTextMessage(
|
const currentDate = new Date()
|
||||||
this.config.FACILITATOR_ROOM_ID,
|
const chatDate = currentDate.toLocaleDateString()
|
||||||
`A support seeker requested a chat (Room ID: ${room.roomId})`
|
const chatTime = currentDate.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);
|
||||||
this.inviteFacilitators(room.roomId)
|
this.inviteFacilitators(room.roomId)
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
logger.log("error", "ERROR JOINING ROOM => " + err)
|
logger.log("error", "ERROR JOINING ROOM => " + err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// When a facilitator joins a support session, make them a moderator
|
|
||||||
// revoke the other invitations
|
|
||||||
if (
|
if (
|
||||||
member.membership === "join" &&
|
member.membership === "join" &&
|
||||||
member.userId !== this.config.BOT_USERID &&
|
member.userId !== this.config.BOT_USERID &&
|
||||||
this.localStorage.getItem(`${member.roomId}-waiting`)
|
this.localStorage.getItem(`${member.roomId}-waiting`)
|
||||||
) {
|
) {
|
||||||
this.localStorage.setItem(`${member.roomId}-facilitator`, member.userId)
|
// make sure it's a facilitator joining
|
||||||
const event = {
|
const roomMembers = await this.client.getJoinedRoomMembers(this.config.FACILITATOR_ROOM_ID)
|
||||||
getType: () => {
|
const members = Object.keys(roomMembers["joined"]);
|
||||||
return "m.room.power_levels";
|
const isFacilitator = members.includes(member.userId)
|
||||||
},
|
|
||||||
getContent: () => {
|
if (isFacilitator) {
|
||||||
return {
|
// made facilitator a moderator in the room
|
||||||
users: {
|
this.localStorage.setItem(`${member.roomId}-facilitator`, member.userId)
|
||||||
[this.config.BOT_USERID]: 100,
|
const event = {
|
||||||
[member.userId]: 50
|
getType: () => {
|
||||||
}
|
return "m.room.power_levels";
|
||||||
};
|
},
|
||||||
}
|
getContent: () => {
|
||||||
};
|
return {
|
||||||
this.client.setPowerLevel(member.roomId, member.userId, 50, event);
|
users: {
|
||||||
this.sendTextMessage(
|
[this.config.BOT_USERID]: 100,
|
||||||
member.roomId,
|
[member.userId]: 50
|
||||||
`${member.name} has joined the chat.`
|
}
|
||||||
);
|
};
|
||||||
this.sendTextMessage(
|
}
|
||||||
this.config.FACILITATOR_ROOM_ID,
|
|
||||||
`${member.name} joined the chat (Room ID: ${member.roomId})`
|
|
||||||
);
|
|
||||||
this.uninviteFacilitators(member.roomId);
|
|
||||||
if (this.config.CAPTURE_TRANSCRIPTS) {
|
|
||||||
const currentDate = new Date();
|
|
||||||
const dateOpts = {
|
|
||||||
year: "numeric",
|
|
||||||
month: "short",
|
|
||||||
day: "numeric"
|
|
||||||
};
|
};
|
||||||
const chatDate = currentDate.toLocaleDateString("en-GB", dateOpts);
|
this.client.setPowerLevel(member.roomId, member.userId, 50, event);
|
||||||
const chatTime = currentDate.toLocaleTimeString("en-GB", {
|
|
||||||
timeZone: "America/New_York"
|
// send notification to Support Chat Notifications room
|
||||||
});
|
const currentDate = new Date()
|
||||||
const filename = `${chatDate} - ${chatTime} - ${member.roomId}.txt`;
|
const chatTime = currentDate.toLocaleTimeString()
|
||||||
const filepath = path.resolve(path.join("transcripts", filename));
|
const roomId = member.roomId.split(':')[0]
|
||||||
this.localStorage.setItem(`${member.roomId}-transcript`, filepath)
|
const notification = `${member.name} joined the chat at ${chatTime} (room ID: ${roomId})`
|
||||||
|
this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, notification);
|
||||||
|
|
||||||
|
// send notification to chat room
|
||||||
|
this.sendTextMessage(
|
||||||
|
member.roomId,
|
||||||
|
`${member.name} has joined the chat.`
|
||||||
|
);
|
||||||
|
|
||||||
|
// revoke the other invitations
|
||||||
|
this.uninviteFacilitators(member.roomId);
|
||||||
|
|
||||||
|
// set transcript file
|
||||||
|
if (this.config.CAPTURE_TRANSCRIPTS) {
|
||||||
|
const currentDate = new Date();
|
||||||
|
const dateOpts = {
|
||||||
|
year: "numeric",
|
||||||
|
month: "short",
|
||||||
|
day: "numeric"
|
||||||
|
};
|
||||||
|
const chatDate = currentDate.toLocaleDateString("en-GB", dateOpts);
|
||||||
|
const chatTime = currentDate.toLocaleTimeString("en-GB", {
|
||||||
|
timeZone: "America/New_York"
|
||||||
|
});
|
||||||
|
const filename = `${chatDate} - ${chatTime} - ${member.roomId}.txt`;
|
||||||
|
const filepath = path.resolve(path.join("transcripts", filename));
|
||||||
|
this.localStorage.setItem(`${member.roomId}-transcript`, filepath)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -470,7 +491,7 @@ class OcrccBot {
|
|||||||
this.client.leave(member.roomId)
|
this.client.leave(member.roomId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async setMessageListeners() {
|
async setMessageListeners() {
|
||||||
|
|||||||
@@ -13,8 +13,7 @@ const {
|
|||||||
BOT_DISPLAY_NAME,
|
BOT_DISPLAY_NAME,
|
||||||
FACILITATOR_GROUP_ID,
|
FACILITATOR_GROUP_ID,
|
||||||
FACILITATOR_ROOM_ID,
|
FACILITATOR_ROOM_ID,
|
||||||
CHAT_OFFLINE_MESSAGE,
|
CAPTURE_TRANSCRIPTS,
|
||||||
CAPTURE_TRANSCRIPTS
|
|
||||||
} = process.env;
|
} = process.env;
|
||||||
|
|
||||||
const botConfig = {
|
const botConfig = {
|
||||||
@@ -29,8 +28,7 @@ const botConfig = {
|
|||||||
BOT_DISPLAY_NAME,
|
BOT_DISPLAY_NAME,
|
||||||
FACILITATOR_GROUP_ID,
|
FACILITATOR_GROUP_ID,
|
||||||
FACILITATOR_ROOM_ID,
|
FACILITATOR_ROOM_ID,
|
||||||
CHAT_OFFLINE_MESSAGE,
|
CAPTURE_TRANSCRIPTS,
|
||||||
CAPTURE_TRANSCRIPTS
|
|
||||||
}
|
}
|
||||||
|
|
||||||
import OcrccBot from './bot'
|
import OcrccBot from './bot'
|
||||||
|
|||||||
@@ -3762,10 +3762,10 @@ map-visit@^1.0.0:
|
|||||||
dependencies:
|
dependencies:
|
||||||
object-visit "^1.0.0"
|
object-visit "^1.0.0"
|
||||||
|
|
||||||
matrix-js-sdk@^5.0.1:
|
matrix-js-sdk@^6.2.1:
|
||||||
version "5.1.0"
|
version "6.2.1"
|
||||||
resolved "https://registry.yarnpkg.com/matrix-js-sdk/-/matrix-js-sdk-5.1.0.tgz#9b3b02af227ecc2d0cc35fb7312c92b8a6754293"
|
resolved "https://registry.yarnpkg.com/matrix-js-sdk/-/matrix-js-sdk-6.2.1.tgz#d5f76491a650c0a36fcdd078cff59f2da96edd7b"
|
||||||
integrity sha512-IGRq5iACiKp3iIxAghwtdBPrbdgORowc0i8YuIMkZZMpRJDXnNaudt2BFwyQdukV7gvzz7F0sfxBUerySfOnKA==
|
integrity sha512-X12Y2SMg8MOJwE5P3VMsMV/mnQHOmyJkF+FZRida124w4B4tBJouaNxteYyYaH34w+wyaKGxuqEBXecfSpfvbw==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@babel/runtime" "^7.8.3"
|
"@babel/runtime" "^7.8.3"
|
||||||
another-json "^0.2.0"
|
another-json "^0.2.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user