refactor into a bot class

This commit is contained in:
Sharon Kennedy
2020-02-23 13:58:09 -05:00
parent 5570f79e69
commit febd3feb61
5 changed files with 701 additions and 9866 deletions

View File

@@ -1,206 +1,205 @@
const path = require("path")
const fs = require("fs")
const os = require("os")
const util = require("util")
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import * as util from 'util'
import { LocalStorage } from "node-localstorage";
import { uuid } from "uuidv4"
import config from 'config';
const config = require('config');
const winston = require('winston');
const uuid = require('uuidv4').uuid;
const LocalStorage = require('node-localstorage').LocalStorage;
global.Olm = require('olm');
const matrix = require('matrix-js-sdk');
import * as matrix from "matrix-js-sdk";
import logger from './logger'
const ENCRYPTION_CONFIG = { "algorithm": "m.megolm.v1.aes-sha2" };
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write all logs with level `error` and below to `error.log`
// - Write all logs with level `info` and below to `combined.log`
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
class OcrccBot {
constructor() {
this.awaitingAgreement = {}
this.awaitingFacilitator = {}
this.client = matrix.createClient(config.get('homeserverUrl'))
}
//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}
const homeserverUrl = 'https://matrix.rhok.space'
const accessToken = 'MDAxOGxvY2F0aW9uIHJob2suc3BhY2UKMDAxM2lkZW50aWZpZXIga2V5CjAwMTBjaWQgZ2VuID0gMQowMDI3Y2lkIHVzZXJfaWQgPSBAaGVscC1ib3Q6cmhvay5zcGFjZQowMDE2Y2lkIHR5cGUgPSBhY2Nlc3MKMDAyMWNpZCBub25jZSA9IGZBOCsjWWQ4MF9LeTssaF8KMDAyZnNpZ25hdHVyZSA370YUvuoVD3r08AwdgGV9sE0aNWBRTrKvB1me8Bm0tQo'
const botName = 'Help Bot'
const username = 'help-bot'
const password = 'ocrccdemo'
const userId = "@help-bot:rhok.space"
const waitingRoomId = '!pYVVPyFKacZeKZbWyz:rhok.space'
const introMessage = 'This chat application does not collect any of your personal data or any data from your use of this service.'
const termsUrl = 'https://tosdr.org/'
const agreementMessage = 'Do you want to continue?'
const confirmationMessage = 'A faciltator will be with you soon.'
const exitMessage = 'That chat was not started. You can close this chatbox.'
let awaitingAgreement = {}
let awaitingFacilitator = {}
let client = matrix.createClient(homeserverUrl)
let localStorage = global.localStorage;
if (typeof localStorage === "undefined" || localStorage === null) {
const storageLoc = `matrix-chatbot-${username}`
createLocalStorage() {
const storageLoc = `matrix-chatbot-${config.get('username')}`
const dir = path.resolve(path.join(os.homedir(), ".local-storage"))
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const localStoragePath = path.resolve(path.join(dir, storageLoc))
localStorage = new LocalStorage(localStoragePath);
return new LocalStorage(localStoragePath);
}
let deviceId = localStorage.getItem('deviceId')
const sendMessage = (roomId, msgText) => {
return client.sendTextMessage(roomId, msgText)
.then((res) => {
logger.log('info', "Message sent")
logger.log('info', res)
})
.catch((err) => {
switch (err["name"]) {
case "UnknownDeviceError":
Object.keys(err.devices).forEach((userId) => {
Object.keys(err.devices[userId]).map((deviceId) => {
client.setDeviceVerified(userId, deviceId, true);
});
});
return sendMessage(roomId, msgText)
break;
default:
logger.log('error', "Error sending message");
logger.log('error', err);
break;
}
})
}
const inviteFacilitators = (roomId) => {
awaitingFacilitator[roomId] = true
client.getJoinedRoomMembers(waitingRoomId)
.then((members) => {
logger.log("info", "MEMBERS")
logger.log("info", members)
Object.keys(members["joined"]).forEach((member) => {
if (member !== userId)
client.invite(roomId, member)
})
})
// const notif = `There is a support seeker waiting. Go to https://riot.im/app/#/room/${roomId} to respond.`
// sendMessage(waitingRoomId, notif)
}
const kickFacilitators = (roomId) => {
awaitingFacilitator[roomId] = false
client.getJoinedRoomMembers(waitingRoomId)
.then((allFacilitators) => {
client.getJoinedRoomMembers(roomId)
.then((roomMembers) => {
const membersIds = Object.keys(roomMembers["joined"])
const facilitatorsIds = Object.keys(allFacilitators["joined"])
facilitatorsIds.forEach((f) => {
if (!membersIds.includes(f)) {
logger.log("info", "kicking out " + f + " from " + roomId)
client.kick(roomId, f, "A facilitator has already joined this chat.")
.then(() => {
logger.log("info", "Kick success")
})
.catch((err) => {
logger.log("error", err)
})
sendMessage(roomId, msgText) {
return this.client.sendTextMessage(roomId, msgText)
.then((res) => {
logger.log('info', "Message sent")
logger.log('info', res)
})
.catch((err) => {
switch (err["name"]) {
case "UnknownDeviceError":
Object.keys(err.devices).forEach((userId) => {
Object.keys(err.devices[userId]).map((deviceId) => {
this.client.setDeviceVerified(userId, deviceId, true);
});
});
return this.sendMessage(roomId, msgText)
break;
default:
logger.log('error', "Error sending message");
logger.log('error', err);
break;
}
})
})
})
}
client.login('m.login.password', {
user: username,
password: password,
initial_device_display_name: botName,
deviceId: deviceId,
})
.then((data) => {
const accessToken = data.access_token
const deviceId = data.device_id
localStorage.setItem('deviceId', data.device_id)
// create new client with full options
let opts = {
baseUrl: homeserverUrl,
accessToken: accessToken,
userId: userId,
deviceId: deviceId,
sessionStore: new matrix.WebStorageSessionStore(localStorage),
}
client = matrix.createClient(opts)
})
.catch(err => {
logger.log('error', `Login error: ${err}`)
})
.then(() => client.initCrypto())
.then(() => {
// Automatically join all room invitations
client.on("RoomMember.membership", (event, member) => {
if (member.membership === "invite" && member.userId === userId) {
logger.log("info", "Auto-joining room " + member.roomId)
client.joinRoom(member.roomId)
.then(() => client.setRoomEncryption(member.roomId, ENCRYPTION_CONFIG))
.then(() => {
if (member.roomId !== waitingRoomId) {
sendMessage(member.roomId, introMessage)
.then(() => sendMessage(member.roomId, `Please read the terms and conditions at ${termsUrl}`))
.then(() => sendMessage(member.roomId, agreementMessage))
.then(() => awaitingAgreement[member.roomId] = true)
sendHtmlMessage(roomId, msgText, msgHtml) {
return this.client.sendHtmlMessage(roomId, msgText, msgHtml)
.then((res) => {
logger.log('info', "Message sent")
logger.log('info', res)
})
.catch((err) => {
switch (err["name"]) {
case "UnknownDeviceError":
Object.keys(err.devices).forEach((userId) => {
Object.keys(err.devices[userId]).map((deviceId) => {
this.client.setDeviceVerified(userId, deviceId, true);
});
});
return this.sendHtmlMessage(roomId, msgText, msgHtml)
break;
default:
logger.log('error', "Error sending message");
logger.log('error', err);
break;
}
})
}
}
logger.log("info", "Membership event: " + JSON.stringify(member))
logger.log("info", "Awaiting facilitator: " + awaitingFacilitator[member.roomId])
inviteFacilitators(roomId) {
this.awaitingFacilitator[roomId] = true
this.client.getJoinedRoomMembers(config.get('waitingRoomId'))
.then((members) => {
Object.keys(members["joined"]).forEach((member) => {
if (member !== config.get('userId'))
this.client.invite(roomId, member)
})
})
// const notif = `There is a support seeker waiting. Go to https://riot.im/app/#/room/${roomId} to respond.`
// sendMessage(waitingRoomId, notif)
}
if (member.membership === 'join' && awaitingFacilitator[member.roomId]) {
kickFacilitators(member.roomId)
}
});
uninviteFacilitators(roomId) {
this.awaitingFacilitator[roomId] = false
this.client.getJoinedRoomMembers(config.get('waitingRoomId'))
.then((allFacilitators) => {
this.client.getJoinedRoomMembers(roomId)
.then((roomMembers) => {
const membersIds = Object.keys(roomMembers["joined"])
const facilitatorsIds = Object.keys(allFacilitators["joined"])
facilitatorsIds.forEach((f) => {
if (!membersIds.includes(f)) {
logger.log("info", "kicking out " + f + " from " + roomId)
this.client.kick(roomId, f, "A facilitator has already joined this chat.")
.then(() => {
logger.log("info", "Kick success")
})
.catch((err) => {
logger.log("error", err)
})
}
})
})
})
}
client.on('Event.decrypted', (event) => {
if (event.getType() === 'm.room.message') {
const roomId = event.getRoomId()
const sender = event.getSender()
const content = event.getContent()
const body = content.body
start() {
const localStorage = this.createLocalStorage()
let deviceId = localStorage.getItem('deviceId')
if (sender !== userId && awaitingAgreement[roomId]) {
if (body.toLowerCase().startsWith('yes')) {
sendMessage(roomId, confirmationMessage)
inviteFacilitators(roomId)
awaitingAgreement[roomId] = false
} else {
sendMessage(roomId, exitMessage)
awaitingAgreement[roomId] = false
}
this.client.login('m.login.password', {
user: config.get('username'),
password: config.get('password'),
initial_device_display_name: config.get('botName'),
deviceId: deviceId,
})
.then((data) => {
const accessToken = data.access_token
const deviceId = data.device_id
localStorage.setItem('deviceId', data.device_id)
// create new client with full options
let opts = {
baseUrl: config.get('homeserverUrl'),
accessToken: accessToken,
userId: config.get('userId'),
deviceId: deviceId,
sessionStore: new matrix.WebStorageSessionStore(localStorage),
}
}
});
})
.finally(() => client.startClient())
this.client = matrix.createClient(opts)
})
.catch(err => {
logger.log('error', `Login error: ${err}`)
})
.then(() => this.client.initCrypto())
.then(() => {
// Automatically accept all room invitations
// On joining a room, send the intro messages and wait for agreement to continue
this.client.on("RoomMember.membership", (event, member) => {
if (member.membership === "invite" && member.userId === config.get('userId')) {
logger.log("info", "Auto-joining room " + member.roomId)
this.client.joinRoom(member.roomId)
.then(() => this.client.setRoomEncryption(member.roomId, ENCRYPTION_CONFIG))
.then(() => {
if (member.roomId !== config.get('waitingRoomId')) {
this.sendMessage(member.roomId, config.get('introMessage'))
.then(() => this.sendHtmlMessage(member.roomId, `Please read the terms and conditions at ${config.get('termsUrl')}`, `Please read the full <a href="${config.get('termsUrl')}">terms and conditions</a>.`))
.then(() => this.sendMessage(member.roomId, config.get('agreementMessage')))
.then(() => this.awaitingAgreement[member.roomId] = true)
}
})
}
// When the first facilitator joins a support session, uninvite the other facilitators
if (member.membership === 'join' && this.awaitingFacilitator[member.roomId]) {
this.uninviteFacilitators(member.roomId)
}
});
// Listen for incoming messages
this.client.on('Event.decrypted', (event) => {
if (event.getType() === 'm.room.message') {
const roomId = event.getRoomId()
const sender = event.getSender()
const content = event.getContent()
const body = content.body
// Listen for the user to agree to continue, then invite facilitators to join
if (sender !== config.get('userId') && this.awaitingAgreement[roomId]) {
if (body.toLowerCase().startsWith('yes')) {
this.sendMessage(roomId, config.get('confirmationMessage'))
this.inviteFacilitators(roomId)
this.awaitingAgreement[roomId] = false
} else {
this.sendMessage(roomId, config.get('exitMessage'))
this.awaitingAgreement[roomId] = false
}
}
}
});
})
.finally(() => this.client.startClient())
}
}
const bot = new OcrccBot();
bot.start()

27
src/logger.js Normal file
View File

@@ -0,0 +1,27 @@
import winston from 'winston'
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
//
// - Write all logs with level `error` and below to `error.log`
// - Write all logs with level `info` and below to `combined.log`
//
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
//
// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}
export default logger;