let sdk = require("matrix-js-sdk"); let message = require("./message.js"); class Bot { constructor(config) { this.config = config } } function create(configFile) { let config = require(configFile); console.log("Running with config:"); console.log(config); return new Bot(config); } function init(bot) { console.log("Creating Matrix Client") bot.client = sdk.createClient({ baseUrl: bot.config.baseUrl, accessToken: bot.config.accessToken, userId: bot.config.userId }); function sendClientStatusUpdate() { bot.config.statusRooms.forEach(roomId => { console.log("Notifying %s", roomId); bot.client.sendMessage(roomId, message.createBasic("Started!")).done(function () { console.log("Notified %s", roomId); }) }); } // Prep the bot bot.client.on("sync", function (state, previousState, data) { switch (state) { case "PREPARED": sendClientStatusUpdate(); break; } }); // auto join rooms that an admin user has invited the bot to bot.client.on("RoomMember.membership", function (event, member) { if (member.membership === "invite" && bot.config.admin.indexOf(ember.userId) >= 0) { bot.client.joinRoom(member.roomId).done(function () { console.log("Auto-joined %s", member.roomId); }); } }); return bot; } function run(bot) { bot.client.startClient() } exports.create = create; exports.init = init; exports.run = run;