let sdk = require('matrix-js-sdk'); let message = require('./message'); let utility = require('./utility'); let { logger } = require('./logging'); class Bot { constructor(config, buildInfo) { this.config = config; this.buildInfo = buildInfo; this.connected = false; } /** * Initialize the bot connection */ init() { logger.info("Creating Matrix Client") this.client = sdk.createClient({ baseUrl: this.config.baseUrl, accessToken: this.config.accessToken, userId: this.config.userId }); } sendStatusStartup() { let promises = [Promise.resolve(true)] if (this.connected) { this.config.statusRooms.forEach(roomId => { logger.debug("Notifying %s of startup", roomId); promises.push(this.client.sendMessage( roomId, message.createBasic("Started with version: " + this.buildInfo, message.types.NOTICE) ).then(() => { logger.debug("Notified %s of startup", roomId); })); }); } else { logger.warn("Attempting to send startup message while disconnected"); } return Promise.all(promises); } sendStatusShutdown() { let promises = [Promise.resolve(true)] if (this.connected) { this.config.statusRooms.forEach(roomId => { logger.debug("Notifying %s of shutdown", roomId); promises.push(this.client.sendMessage( roomId, message.createBasic("Shutting down", message.types.NOTICE) ).then(() => { logger.debug("Notified %s of shutdown", roomId); })); }); } else { logger.warn("Attempting to send shutdown message while disconnected"); } return Promise.all(promises); } } function sanitizeConfig(config) { let clonedConfig = { ...config }; clonedConfig.accessToken = "******" return clonedConfig; } function create(configFile) { let config = require(configFile); logger.info("Running with config:"); logger.debug("%o", sanitizeConfig(config)); let buildInfo = utility.getBuildInfo("../build.info") logger.info("Running version: %s", buildInfo); return new Bot(config, buildInfo); } exports.create = create;