2020-03-16 18:41:56 +00:00
|
|
|
"use strict";
|
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
|
|
|
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
2020-03-16 18:41:56 +00:00
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
2020-04-22 22:04:48 +00:00
|
|
|
exports.default = void 0;
|
2020-03-16 18:41:56 +00:00
|
|
|
|
|
|
|
var fs = _interopRequireWildcard(require("fs"));
|
|
|
|
|
|
|
|
var os = _interopRequireWildcard(require("os"));
|
|
|
|
|
|
|
|
var path = _interopRequireWildcard(require("path"));
|
|
|
|
|
|
|
|
var util = _interopRequireWildcard(require("util"));
|
|
|
|
|
|
|
|
var _nodeLocalstorage = require("node-localstorage");
|
|
|
|
|
|
|
|
var matrix = _interopRequireWildcard(require("matrix-js-sdk"));
|
|
|
|
|
|
|
|
var _logger = _interopRequireDefault(require("./logger"));
|
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
global.Olm = require("olm");
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
class OcrccBot {
|
|
|
|
constructor(botConfig) {
|
|
|
|
this.config = botConfig;
|
|
|
|
this.awaitingFacilitator = {};
|
|
|
|
this.client = matrix.createClient(this.config.MATRIX_SERVER_URL);
|
|
|
|
this.joinedRooms = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
createLocalStorage() {
|
|
|
|
const storageLoc = `matrix-chatbot-${this.config.BOT_USERNAME}`;
|
|
|
|
const dir = path.resolve(path.join(os.homedir(), ".local-storage"));
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
if (!fs.existsSync(dir)) {
|
|
|
|
fs.mkdirSync(dir);
|
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
const localStoragePath = path.resolve(path.join(dir, storageLoc));
|
|
|
|
return new _nodeLocalstorage.LocalStorage(localStoragePath);
|
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
sendTextMessage(roomId, msgText, showToUser = null) {
|
|
|
|
const content = {
|
|
|
|
msgtype: "m.text",
|
|
|
|
body: msgText,
|
|
|
|
showToUser: showToUser
|
|
|
|
};
|
|
|
|
this.sendMessage(roomId, content);
|
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
async sendMessage(roomId, content) {
|
|
|
|
_logger.default.log("info", `SENDING MESSAGE: ${content.body}`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.client.sendMessage(roomId, content);
|
|
|
|
} catch (err) {
|
|
|
|
switch (err["name"]) {
|
|
|
|
case "UnknownDeviceError":
|
|
|
|
Object.keys(err.devices).forEach(userId => {
|
|
|
|
Object.keys(err.devices[userId]).map(async deviceId => {
|
|
|
|
try {
|
|
|
|
await this.client.setDeviceVerified(userId, deviceId, true);
|
|
|
|
} catch (err) {
|
|
|
|
_logger.default.log("error", `ERROR VERIFYING DEVICE: ${err}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
await this.sendMessage(roomId, content);
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
default:
|
|
|
|
_logger.default.log("error", `ERROR SENDING MESSAGE: ${err}`);
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
inviteUserToRoom(roomId, member) {
|
|
|
|
try {
|
|
|
|
this.client.invite(roomId, member);
|
|
|
|
} catch (err) {
|
|
|
|
this.handleBotCrash(roomId, err);
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
kickUserFromRoom(roomId, member) {
|
|
|
|
try {
|
|
|
|
this.client.kick(roomId, member, this.config.KICK_REASON);
|
|
|
|
} catch (err) {
|
|
|
|
this.handleBotCrash(roomId, err);
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
_logger.default.log("error", `ERROR KICKING OUT MEMBER: ${err}`);
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
2020-04-22 22:04:48 +00:00
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
async inviteFacilitators(roomId) {
|
|
|
|
this.localStorage.setItem(`${roomId}-waiting`, 'true');
|
|
|
|
let chatOffline = true;
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
try {
|
|
|
|
const data = await this.client.getGroupUsers(this.config.FACILITATOR_GROUP_ID);
|
|
|
|
const members = data.chunk;
|
|
|
|
members.forEach(member => {
|
|
|
|
const memberId = member.user_id;
|
|
|
|
const user = this.client.getUser(memberId);
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
if (user && user.presence === "online" && memberId !== this.config.BOT_USERID) {
|
|
|
|
chatOffline = false;
|
|
|
|
this.inviteUserToRoom(roomId, memberId);
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
if (chatOffline) {
|
|
|
|
_logger.default.log('info', "NO FACILITATORS ONLINE");
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
this.sendTextMessage(roomId, this.config.CHAT_OFFLINE_MESSAGE);
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
2020-04-22 22:04:48 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.handleBotCrash(roomId, err);
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
_logger.default.log("error", `ERROR GETTING FACILITATORS: ${err}`);
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
async uninviteFacilitators(roomId) {
|
|
|
|
this.localStorage.removeItem(`${roomId}-waiting`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const groupUsers = await this.client.getGroupUsers(this.config.FACILITATOR_GROUP_ID);
|
|
|
|
const roomMembers = await this.client.getJoinedRoomMembers(roomId);
|
|
|
|
const membersIds = Object.keys(roomMembers["joined"]);
|
|
|
|
const facilitatorsIds = groupUsers.chunk.map(f => f.user_id);
|
|
|
|
facilitatorsIds.forEach(f => {
|
|
|
|
if (!membersIds.includes(f)) {
|
|
|
|
this.kickUserFromRoom(roomId, f);
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
|
|
|
});
|
2020-04-22 22:04:48 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.handleBotCrash(roomId, err);
|
|
|
|
|
|
|
|
_logger.default.log("ERROR UNINVITING FACILITATORS", err);
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
2020-04-22 22:04:48 +00:00
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
handleBotCrash(roomId, error) {
|
|
|
|
if (roomId) {
|
|
|
|
this.sendTextMessage(roomId, this.config.BOT_ERROR_MESSAGE);
|
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, `The Help Bot ran into an error: ${error}. Please verify that the chat service is working.`);
|
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
handleMessageEvent(event) {
|
|
|
|
const content = event.getContent(); // do nothing if there's no content
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
if (!content) {
|
|
|
|
return;
|
|
|
|
} // bot commands
|
2020-03-16 18:41:56 +00:00
|
|
|
|
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
if (content.body.startsWith("!bot")) {
|
|
|
|
return this.handleBotCommand(event);
|
|
|
|
} // write to transcript
|
2020-03-16 18:41:56 +00:00
|
|
|
|
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
if (this.config.CAPTURE_TRANSCRIPTS) {
|
|
|
|
return this.writeToTranscript(event);
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
2020-04-22 22:04:48 +00:00
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
writeToTranscript(event) {
|
|
|
|
try {
|
|
|
|
const sender = event.getSender();
|
|
|
|
const roomId = event.getRoomId();
|
|
|
|
const content = event.getContent();
|
|
|
|
const date = event.getDate();
|
|
|
|
const time = date.toLocaleTimeString("en-GB", {
|
|
|
|
timeZone: "America/New_York"
|
2020-03-16 18:41:56 +00:00
|
|
|
});
|
2020-04-22 22:04:48 +00:00
|
|
|
const filepath = this.localStorage.getItem(`${roomId}-transcript`);
|
2020-04-23 14:24:03 +00:00
|
|
|
|
|
|
|
if (!filepath) {
|
|
|
|
return _logger.default.log("error", `NO TRANSCRIPT FILE FOR ROOM: ${roomId}`);
|
|
|
|
}
|
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
const message = `${sender} [${time}]: ${content.body}\n`;
|
|
|
|
fs.appendFileSync(filepath, message, "utf8");
|
|
|
|
} catch (err) {
|
|
|
|
_logger.default.log("error", `ERROR APPENDING TO TRANSCRIPT FILE: ${err}`);
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
2020-04-22 22:04:48 +00:00
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
handleBotCommand(event) {
|
|
|
|
try {
|
|
|
|
const senderId = event.getSender();
|
|
|
|
const roomId = event.getRoomId();
|
|
|
|
const content = event.getContent();
|
|
|
|
const command = content.body.substring("!bot".length).trim();
|
|
|
|
|
|
|
|
switch (command) {
|
|
|
|
case "transcript":
|
|
|
|
this.sendTranscript(senderId, roomId);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "transcript please":
|
|
|
|
this.sendTranscript(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;
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
2020-04-22 22:04:48 +00:00
|
|
|
} catch (err) {
|
|
|
|
_logger.default.log("error", `ERROR EXECUTING BOT COMMAND: ${err}`);
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
2020-04-22 22:04:48 +00:00
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
async leaveEmptyRooms(senderId) {
|
|
|
|
try {
|
|
|
|
const roomData = await this.client.getJoinedRooms();
|
|
|
|
const joinedRoomsIds = roomData["joined_rooms"];
|
|
|
|
joinedRoomsIds.forEach(async roomId => {
|
|
|
|
const room = this.client.getRoom(roomId);
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
if (room && room.getJoinedMemberCount() === 1) {
|
|
|
|
try {
|
|
|
|
_logger.default.log('info', "LEAVING EMPTY ROOM => " + roomId);
|
2020-03-18 07:00:43 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
await this.client.leave(roomId);
|
|
|
|
} catch (err) {
|
|
|
|
_logger.default.log('error', "ERROR LEAVING EMPTY ROOM => " + err);
|
|
|
|
}
|
2020-03-18 07:00:43 +00:00
|
|
|
}
|
2020-04-22 22:04:48 +00:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
_logger.default.log("error", `ERROR GETTING JOINED ROOMS: ${err}`);
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 07:00:43 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
async sendTranscript(senderId, roomId) {
|
|
|
|
try {
|
|
|
|
const transcriptFile = this.localStorage.getItem(`${roomId}-transcript`);
|
2020-03-18 07:00:43 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
if (!transcriptFile) {
|
|
|
|
this.sendTextMessage(roomId, "There is no transcript for this chat.", senderId);
|
|
|
|
}
|
2020-03-18 07:00:43 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
const filename = path.basename(transcriptFile) || "Transcript";
|
|
|
|
const stream = fs.createReadStream(transcriptFile);
|
|
|
|
const contentUrl = await this.client.uploadContent({
|
|
|
|
stream: stream,
|
|
|
|
name: filename
|
|
|
|
});
|
|
|
|
const content = {
|
|
|
|
msgtype: "m.file",
|
|
|
|
body: filename,
|
|
|
|
url: JSON.parse(contentUrl).content_uri,
|
|
|
|
showToUser: senderId
|
2020-03-18 07:00:43 +00:00
|
|
|
};
|
2020-04-22 22:04:48 +00:00
|
|
|
this.sendMessage(roomId, content);
|
|
|
|
} catch (err) {
|
|
|
|
_logger.default.log("error", `ERROR UPLOADING CONTENT: ${err}`);
|
2020-03-18 07:00:43 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
this.sendTextMessage(roomId, "There was an error uploading the transcript.", senderId);
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
async deleteOldDevices() {
|
|
|
|
const currentDeviceId = this.client.getDeviceId();
|
|
|
|
const deviceData = await this.client.getDevices();
|
|
|
|
const allDeviceIds = deviceData.devices.map(d => d.device_id);
|
|
|
|
const oldDevices = allDeviceIds.filter(id => id !== currentDeviceId);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.client.deleteMultipleDevices(oldDevices);
|
|
|
|
} catch (err) {
|
|
|
|
_logger.default.log("info", "RETRYING DELETE OLD DEVICES WITH AUTH");
|
|
|
|
|
|
|
|
const auth = {
|
|
|
|
session: err.data.session,
|
|
|
|
type: "m.login.password",
|
|
|
|
user: this.config.BOT_USERID,
|
|
|
|
identifier: {
|
|
|
|
type: "m.id.user",
|
|
|
|
user: this.config.BOT_USERID
|
|
|
|
},
|
|
|
|
password: this.config.BOT_PASSWORD
|
|
|
|
};
|
|
|
|
await this.client.deleteMultipleDevices(oldDevices, auth);
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
_logger.default.log("info", "DELETED OLD DEVICES");
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
async trackJoinedRooms() {
|
|
|
|
const roomData = await this.client.getJoinedRooms();
|
|
|
|
this.joinedRooms = roomData["joined_rooms"];
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
_logger.default.log("info", "JOINED ROOMS => " + this.joinedRooms);
|
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
async setMembershipListeners() {
|
|
|
|
// Automatically accept all room invitations
|
|
|
|
this.client.on("RoomMember.membership", async (event, member) => {
|
|
|
|
if (member.membership === "invite" && member.userId === this.config.BOT_USERID && !this.joinedRooms.includes(member.roomId)) {
|
|
|
|
try {
|
|
|
|
const room = await this.client.joinRoom(member.roomId);
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
_logger.default.log("info", "AUTO JOINED ROOM => " + room.roomId);
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
this.sendTextMessage(this.config.FACILITATOR_ROOM_ID, `A support seeker requested a chat (Room ID: ${room.roomId})`);
|
|
|
|
this.inviteFacilitators(room.roomId);
|
|
|
|
} catch (err) {
|
|
|
|
_logger.default.log("error", "ERROR JOINING ROOM => " + err);
|
|
|
|
}
|
|
|
|
} // When a facilitator joins a support session, make them a moderator
|
|
|
|
// revoke the other invitations
|
|
|
|
|
|
|
|
|
|
|
|
if (member.membership === "join" && member.userId !== this.config.BOT_USERID && this.localStorage.getItem(`${member.roomId}-waiting`)) {
|
|
|
|
this.localStorage.setItem(`${member.roomId}-facilitator`, member.userId);
|
|
|
|
const event = {
|
|
|
|
getType: () => {
|
|
|
|
return "m.room.power_levels";
|
|
|
|
},
|
|
|
|
getContent: () => {
|
|
|
|
return {
|
|
|
|
users: {
|
|
|
|
[this.config.BOT_USERID]: 100,
|
|
|
|
[member.userId]: 50
|
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
};
|
|
|
|
}
|
2020-04-22 22:04:48 +00:00
|
|
|
};
|
|
|
|
this.client.setPowerLevel(member.roomId, member.userId, 50, event);
|
|
|
|
this.sendTextMessage(member.roomId, `${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);
|
|
|
|
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);
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
2020-04-22 22:04:48 +00:00
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
if (member.membership === "leave" && member.userId !== this.config.BOT_USERID) {
|
|
|
|
const facilitatorId = this.localStorage.getItem(`${member.roomId}-facilitator`);
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
if (member.userId === facilitatorId) {
|
|
|
|
this.sendTextMessage(member.roomId, `${member.name} has left the chat.`);
|
|
|
|
} // leave if there is nobody in the room
|
2020-03-16 18:41:56 +00:00
|
|
|
|
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
const room = this.client.getRoom(member.roomId);
|
|
|
|
if (!room) return;
|
|
|
|
const memberCount = room.getJoinedMemberCount();
|
|
|
|
|
|
|
|
if (memberCount === 1) {
|
|
|
|
// just the bot
|
|
|
|
_logger.default.log("info", `LEAVING EMPTY ROOM ==> ${member.roomId}`);
|
|
|
|
|
|
|
|
this.client.leave(member.roomId);
|
|
|
|
this.localStorage.removeItem(`${member.roomId}-facilitator`);
|
|
|
|
this.localStorage.removeItem(`${member.roomId}-transcript`);
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
2020-04-22 22:04:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async setMessageListeners() {
|
|
|
|
// encrypted messages
|
|
|
|
this.client.on("Event.decrypted", (event, err) => {
|
|
|
|
if (err) {
|
|
|
|
return _logger.default.log("error", `ERROR DECRYPTING EVENT: ${err}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.getType() === "m.room.message") {
|
|
|
|
this.handleMessageEvent(event);
|
|
|
|
}
|
|
|
|
}); // unencrypted messages
|
|
|
|
|
|
|
|
this.client.on("Room.timeline", (event, room, toStartOfTimeline) => {
|
|
|
|
if (event.getType() === "m.room.message" && !event.isEncrypted()) {
|
|
|
|
this.handleMessageEvent(event);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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 start() {
|
|
|
|
const localStorage = this.createLocalStorage();
|
|
|
|
this.localStorage = localStorage;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const auth = {
|
|
|
|
user: this.config.BOT_USERNAME,
|
|
|
|
password: this.config.BOT_PASSWORD,
|
|
|
|
initial_device_display_name: this.config.BOT_DISPLAY_NAME
|
|
|
|
};
|
|
|
|
const account = await this.client.login("m.login.password", auth);
|
|
|
|
|
|
|
|
_logger.default.log("info", `ACCOUNT ==> ${JSON.stringify(account)}`);
|
|
|
|
|
|
|
|
let opts = {
|
|
|
|
baseUrl: this.config.MATRIX_SERVER_URL,
|
|
|
|
accessToken: account.access_token,
|
|
|
|
userId: this.config.BOT_USERID,
|
|
|
|
deviceId: account.device_id,
|
|
|
|
sessionStore: new matrix.WebStorageSessionStore(localStorage)
|
|
|
|
};
|
|
|
|
this.client = matrix.createClient(opts);
|
|
|
|
await this.deleteOldDevices();
|
|
|
|
await this.trackJoinedRooms();
|
|
|
|
await this.client.initCrypto();
|
|
|
|
await this.setMembershipListeners();
|
|
|
|
await this.setMessageListeners();
|
|
|
|
this.client.startClient({
|
|
|
|
initialSyncLimit: 0
|
2020-03-16 18:41:56 +00:00
|
|
|
});
|
2020-04-22 22:04:48 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.handleBotCrash(undefined, err);
|
|
|
|
|
|
|
|
_logger.default.log("error", `ERROR INITIALIZING CLIENT: ${err}`);
|
2020-03-16 18:41:56 +00:00
|
|
|
}
|
2020-04-22 22:04:48 +00:00
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
2020-04-22 22:04:48 +00:00
|
|
|
}
|
2020-03-16 18:41:56 +00:00
|
|
|
|
|
|
|
var _default = OcrccBot;
|
2020-04-22 22:04:48 +00:00
|
|
|
exports.default = _default;
|