Browse Source

Starting to break bot out

pull/8/head
Drew Short 5 years ago
parent
commit
1fc5f74c5e
  1. 62
      bot/bot.js
  2. 8
      bot/message.js
  3. 50
      index.js
  4. 24
      pipeline.yml

62
bot/bot.js

@ -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;

8
bot/message.js

@ -0,0 +1,8 @@
function createBasicMessage(body) {
return {
"body": body,
"msgtype": "m.text"
}
}
exports.createBasic = createBasicMessage;

50
index.js

@ -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")));

24
pipeline.yml

@ -31,4 +31,26 @@ jobs:
echo "NPM Version: $(npm --version)" echo "NPM Version: $(npm --version)"
cd baphomet-js-git cd baphomet-js-git
npm install npm install
npm test
npm test
- name: version
public: true
plan:
- get: baphomet-js-git
trigger: true
- task: capture-version
config:
platform: linux
image_resource:
type: registry-image
source: { repository: bitnami/git, tag: "2-debian-9"}
inputs:
- name: baphomet-js-git
run:
path: /bin/sh
args:
- -c
- |
cd baphomet-js-git
$(git describe --tags)-$(git rev-parse HEAD)-$(date --rfc-3339=seconds) > VERSION
outputs:
- name: versioned-project
Loading…
Cancel
Save