Drew Short
5 years ago
4 changed files with 95 additions and 49 deletions
-
62bot/bot.js
-
8bot/message.js
-
50index.js
-
22pipeline.yml
@ -0,0 +1,62 @@ |
|||
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; |
@ -0,0 +1,8 @@ |
|||
function createBasicMessage(body) { |
|||
return { |
|||
"body": body, |
|||
"msgtype": "m.text" |
|||
} |
|||
} |
|||
|
|||
exports.createBasic = createBasicMessage; |
@ -1,48 +1,2 @@ |
|||
let sdk = require("matrix-js-sdk"); |
|||
let config = require("./data/config.json"); |
|||
|
|||
console.log("Running with config:"); |
|||
console.log(config); |
|||
|
|||
let client = sdk.createClient({ |
|||
baseUrl: config.baseUrl, |
|||
accessToken: config.accessToken, |
|||
userId: config.userId |
|||
}); |
|||
|
|||
function createBasicTextMessage(body) { |
|||
return { |
|||
"body": body, |
|||
"msgtype": "m.text" |
|||
} |
|||
} |
|||
|
|||
function sendClientStatusUpdate() { |
|||
config.statusRooms.forEach(roomId => { |
|||
console.log("Notifying %s", roomId); |
|||
client.sendMessage(roomId, createBasicTextMessage("Started!")).done(function() { |
|||
console.log("Notified %s", roomId); |
|||
}) |
|||
}); |
|||
} |
|||
|
|||
// Prep the 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
|
|||
client.on("RoomMember.membership", function (event, member) { |
|||
if (member.membership === "invite" |
|||
&& config.admin.indexOf(ember.userId) >= 0) { |
|||
client.joinRoom(member.roomId).done(function () { |
|||
console.log("Auto-joined %s", member.roomId); |
|||
}); |
|||
} |
|||
}); |
|||
|
|||
client.startClient() |
|||
let bot = require('./bot/bot.js'); |
|||
bot.run(bot.init(bot.create("../data/config.json"))); |
Write
Preview
Loading…
Cancel
Save
Reference in new issue