Compare commits
25 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
46c2467368 | ||
|
|
0ad65e9dcc | ||
|
|
6c882a44cc | ||
|
|
666f2361c8 | ||
|
|
d3747cc654 | ||
|
|
687dfa84e9 | ||
|
|
b7da515969 | ||
|
|
e2c291c040 | ||
|
|
87f2c5290f | ||
|
|
4fdddc8c92 | ||
|
|
d53a5c5e26 | ||
|
|
9772b3b1fd | ||
|
|
84fe91adbb | ||
|
|
b6cd85eab0 | ||
|
|
209b3aaa45 | ||
|
|
4912ce6f61 | ||
|
|
013ffa318f | ||
|
|
ac2f953b90 | ||
|
|
a25c71a04a | ||
|
|
836d4751ad | ||
|
|
2983a14038 | ||
|
|
6e71ba4b5b | ||
|
|
5dc6aa5660 | ||
|
|
6d4b6b1609 | ||
|
|
1e3b63fe5b |
@@ -1,7 +0,0 @@
|
|||||||
MATRIX_SERVER_URL=
|
|
||||||
BOT_DISPLAY_NAME=
|
|
||||||
BOT_USERNAME=
|
|
||||||
BOT_PASSWORD=
|
|
||||||
BOT_USERID=
|
|
||||||
FACILITATOR_ROOM_ID=
|
|
||||||
CAPTURE_TRANSCRIPTS
|
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ node_modules
|
|||||||
*.log
|
*.log
|
||||||
__mocks__/test_transcript.txt
|
__mocks__/test_transcript.txt
|
||||||
transcripts/*.txt
|
transcripts/*.txt
|
||||||
|
config.json
|
||||||
@@ -1,5 +1,10 @@
|
|||||||
FROM node:10-alpine
|
FROM node:10-alpine
|
||||||
|
|
||||||
|
RUN apk add g++ make python
|
||||||
|
RUN apk add tzdata
|
||||||
|
|
||||||
|
ENV TZ America/Montreal
|
||||||
|
|
||||||
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
|
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
|
||||||
|
|
||||||
WORKDIR /home/node/app
|
WORKDIR /home/node/app
|
||||||
|
|||||||
22
README.md
22
README.md
@@ -2,18 +2,9 @@
|
|||||||
|
|
||||||
A simple Matrix bot that handles inviting, uninviting, and notifying Riot users on the recieving end of the [Safe Support chatbox](https://github.com/nomadic-labs/safesupport-chatbox).
|
A simple Matrix bot that handles inviting, uninviting, and notifying Riot users on the recieving end of the [Safe Support chatbox](https://github.com/nomadic-labs/safesupport-chatbox).
|
||||||
|
|
||||||
The bot can be configured with an `.env` file with the following variables:
|
The bot configuration file is `config.json`. It can also pull in user-set configurations from the Safe Support Chat Admin app. To do so, run the command `yarn setup` before starting the bot.
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
MATRIX_SERVER_URL=
|
|
||||||
BOT_DISPLAY_NAME=
|
|
||||||
BOT_USERNAME=
|
|
||||||
BOT_PASSWORD=
|
|
||||||
BOT_USERID=
|
|
||||||
FACILITATOR_ROOM_ID=
|
|
||||||
CHAT_OFFLINE_MESSAGE=
|
|
||||||
CAPTURE_TRANSCRIPTS=
|
|
||||||
```
|
|
||||||
## What does the bot do?
|
## What does the bot do?
|
||||||
* The bot receives an invitation to every chatroom created by the embedded chatbox, and automatically accepts
|
* The bot receives an invitation to every chatroom created by the embedded chatbox, and automatically accepts
|
||||||
* Upon joining a new room, the bot invites all of the members of the Facilitators community
|
* Upon joining a new room, the bot invites all of the members of the Facilitators community
|
||||||
@@ -48,9 +39,14 @@ cd safesupport-bot
|
|||||||
yarn
|
yarn
|
||||||
```
|
```
|
||||||
|
|
||||||
Copy the sample `.env` file and add in your own variables
|
Copy the sample config file and add in the missing values.
|
||||||
```
|
```
|
||||||
cp .env.sample .env
|
cp sample.config.json config.json
|
||||||
|
```
|
||||||
|
|
||||||
|
Pull in the user-defined settings (if there are any).
|
||||||
|
```
|
||||||
|
yarn setup
|
||||||
```
|
```
|
||||||
|
|
||||||
Start the local server
|
Start the local server
|
||||||
|
|||||||
@@ -157,6 +157,10 @@ export const mockGetGroupUsers = jest.fn(() => {
|
|||||||
|
|
||||||
export const mockGetUser = jest.fn().mockReturnValue({ presence: 'online'});
|
export const mockGetUser = jest.fn().mockReturnValue({ presence: 'online'});
|
||||||
|
|
||||||
|
export const mockSendStateEvent = jest.fn(() => {
|
||||||
|
return Promise.resolve();
|
||||||
|
});
|
||||||
|
|
||||||
export const mockClient = {
|
export const mockClient = {
|
||||||
registerRequest: mockRegisterRequest,
|
registerRequest: mockRegisterRequest,
|
||||||
initCrypto: mockInitCrypto,
|
initCrypto: mockInitCrypto,
|
||||||
@@ -186,6 +190,7 @@ export const mockClient = {
|
|||||||
getJoinedRoomMembers: mockGetJoinedRoomMembers,
|
getJoinedRoomMembers: mockGetJoinedRoomMembers,
|
||||||
getUser: mockGetUser,
|
getUser: mockGetUser,
|
||||||
getGroupUsers: mockGetGroupUsers,
|
getGroupUsers: mockGetGroupUsers,
|
||||||
|
sendStateEvent: mockSendStateEvent,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const WebStorageSessionStore = jest.fn()
|
export const WebStorageSessionStore = jest.fn()
|
||||||
|
|||||||
341
dist/bot.js
vendored
341
dist/bot.js
vendored
File diff suppressed because it is too large
Load Diff
14
dist/bot.test.js
vendored
14
dist/bot.test.js
vendored
@@ -103,6 +103,8 @@ describe('OcrccBot', () => {
|
|||||||
mockAppendFileSync.mockClear();
|
mockAppendFileSync.mockClear();
|
||||||
|
|
||||||
_matrixJsSdk.mockGetGroupUsers.mockClear();
|
_matrixJsSdk.mockGetGroupUsers.mockClear();
|
||||||
|
|
||||||
|
_matrixJsSdk.mockSendStateEvent.mockClear();
|
||||||
});
|
});
|
||||||
test('constructor should inititialize class variables', () => {
|
test('constructor should inititialize class variables', () => {
|
||||||
const bot = new _bot.default(botConfig);
|
const bot = new _bot.default(botConfig);
|
||||||
@@ -276,4 +278,16 @@ describe('OcrccBot', () => {
|
|||||||
expect(_matrixJsSdk.mockStartClient).toHaveBeenCalled();
|
expect(_matrixJsSdk.mockStartClient).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
test('#sendBotSignal should send custom state event', () => {
|
||||||
|
const bot = new _bot.default(botConfig);
|
||||||
|
bot.start();
|
||||||
|
const test_room_id = 'test_room_id';
|
||||||
|
const signal = 'END_CHAT';
|
||||||
|
bot.sendBotSignal(test_room_id, signal);
|
||||||
|
(0, _waitForExpect.default)(() => {
|
||||||
|
expect(_matrixJsSdk.mockSendStateEvent).toHaveBeenCalledWith(test_room_id, 'm.bot.signal', {
|
||||||
|
signal
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
37
dist/index.js
vendored
37
dist/index.js
vendored
@@ -2,37 +2,14 @@
|
|||||||
|
|
||||||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
||||||
|
|
||||||
|
var _config = _interopRequireDefault(require("../config.json"));
|
||||||
|
|
||||||
var _bot = _interopRequireDefault(require("./bot"));
|
var _bot = _interopRequireDefault(require("./bot"));
|
||||||
|
|
||||||
require('dotenv').config();
|
const bot = new _bot.default(_config.default);
|
||||||
|
|
||||||
const ENCRYPTION_CONFIG = {
|
try {
|
||||||
algorithm: "m.megolm.v1.aes-sha2"
|
|
||||||
};
|
|
||||||
const KICK_REASON = "A facilitator has already joined this chat.";
|
|
||||||
const BOT_ERROR_MESSAGE = "Something went wrong on our end, please restart the chat and try again.";
|
|
||||||
const MAX_RETRIES = 3;
|
|
||||||
const {
|
|
||||||
MATRIX_SERVER_URL,
|
|
||||||
BOT_USERNAME,
|
|
||||||
BOT_USERID,
|
|
||||||
BOT_PASSWORD,
|
|
||||||
BOT_DISPLAY_NAME,
|
|
||||||
FACILITATOR_ROOM_ID,
|
|
||||||
CAPTURE_TRANSCRIPTS
|
|
||||||
} = process.env;
|
|
||||||
const botConfig = {
|
|
||||||
ENCRYPTION_CONFIG,
|
|
||||||
KICK_REASON,
|
|
||||||
BOT_ERROR_MESSAGE,
|
|
||||||
MAX_RETRIES,
|
|
||||||
MATRIX_SERVER_URL,
|
|
||||||
BOT_USERNAME,
|
|
||||||
BOT_USERID,
|
|
||||||
BOT_PASSWORD,
|
|
||||||
BOT_DISPLAY_NAME,
|
|
||||||
FACILITATOR_ROOM_ID,
|
|
||||||
CAPTURE_TRANSCRIPTS
|
|
||||||
};
|
|
||||||
const bot = new _bot.default(botConfig);
|
|
||||||
bot.start();
|
bot.start();
|
||||||
|
} catch (err) {
|
||||||
|
console.log("Unable to start bot", err);
|
||||||
|
}
|
||||||
54
dist/setup.js
vendored
Normal file
54
dist/setup.js
vendored
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const fetch = require('node-fetch');
|
||||||
|
|
||||||
|
const config = require('../config.json');
|
||||||
|
|
||||||
|
const getSettings = async () => {
|
||||||
|
try {
|
||||||
|
const url = `${config.settingsEndpoint}?homeserver=${encodeURIComponent(config.matrixServerUrl)}`;
|
||||||
|
|
||||||
|
if (!config.matrixServerUrl) {
|
||||||
|
throw new Error("The matrix server url is not provided");
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Fetching settings for ${config.matrixServerUrl}`);
|
||||||
|
const res = await fetch(url);
|
||||||
|
const data = await res.json();
|
||||||
|
const {
|
||||||
|
fields,
|
||||||
|
schedule = []
|
||||||
|
} = data;
|
||||||
|
return Object.entries(fields).reduce((settingsObj, [k, v]) => {
|
||||||
|
const [scope, key] = k.split('_');
|
||||||
|
|
||||||
|
if (scope === 'platform') {
|
||||||
|
settingsObj[key] = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
return settingsObj;
|
||||||
|
}, {});
|
||||||
|
} catch (err) {
|
||||||
|
console.log("Error fetching settings", err);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const writeConfig = async () => {
|
||||||
|
const settings = await getSettings();
|
||||||
|
|
||||||
|
if (!settings) {
|
||||||
|
return console.log('No settings to update');
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedSettings = Object.assign(config, settings);
|
||||||
|
fs.writeFile('config.json', JSON.stringify(updatedSettings, null, 2), function (err) {
|
||||||
|
if (err) return console.log("Error updating settings", err);
|
||||||
|
console.log(`Updated settings to config.json`);
|
||||||
|
console.log(updatedSettings);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
writeConfig();
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "private-safesupport-bot",
|
"name": "private-safesupport-bot",
|
||||||
"version": "1.2.0",
|
"version": "2.0.2",
|
||||||
"description": "Chatbot to manage interactions on Safe Support Chat",
|
"description": "Chatbot to manage interactions on Safe Support Chat",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"develop": "nodemon --exec babel-node src/index.js",
|
"develop": "nodemon --exec babel-node src/index.js",
|
||||||
"build": "babel src -d dist",
|
"build": "babel src -d dist",
|
||||||
|
"setup": "node src/setup.js",
|
||||||
"start": "yarn build && node dist/index.js",
|
"start": "yarn build && node dist/index.js",
|
||||||
"test": "jest"
|
"test": "jest"
|
||||||
},
|
},
|
||||||
@@ -14,6 +15,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"matrix-js-sdk": "^6.2.1",
|
"matrix-js-sdk": "^6.2.1",
|
||||||
|
"node-fetch": "^2.6.1",
|
||||||
"node-localstorage": "^2.1.5",
|
"node-localstorage": "^2.1.5",
|
||||||
"node-webcrypto-ossl": "^2.1.0",
|
"node-webcrypto-ossl": "^2.1.0",
|
||||||
"olm": "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz",
|
"olm": "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz",
|
||||||
|
|||||||
16
sample.config.json
Normal file
16
sample.config.json
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"matrixServerUrl": "",
|
||||||
|
"settingsEndpoint": "",
|
||||||
|
"facilitatorRoomId": "",
|
||||||
|
"kickReason": "A facilitator has already joined this chat.",
|
||||||
|
"botErrorMessage": "Something went wrong on our end, please restart the chat and try again.",
|
||||||
|
"botUserId": "",
|
||||||
|
"botUsername": "",
|
||||||
|
"botPassword": "",
|
||||||
|
"botDisplayName": "Help Bot",
|
||||||
|
"captureTranscripts": true,
|
||||||
|
"chatNotAvailableMessage": "The support chat is not available right now.",
|
||||||
|
"chatInactiveMessage": "This chat has been closed due to inactivity.",
|
||||||
|
"maxWaitTime": 180,
|
||||||
|
"maxInactiveTime": 3600
|
||||||
|
}
|
||||||
354
src/bot.js
354
src/bot.js
File diff suppressed because it is too large
Load Diff
@@ -32,7 +32,8 @@ import {
|
|||||||
mockKick,
|
mockKick,
|
||||||
mockGetJoinedRoomMembers,
|
mockGetJoinedRoomMembers,
|
||||||
mockGetUser,
|
mockGetUser,
|
||||||
mockGetGroupUsers
|
mockGetGroupUsers,
|
||||||
|
mockSendStateEvent,
|
||||||
} from "matrix-js-sdk";
|
} from "matrix-js-sdk";
|
||||||
|
|
||||||
import OcrccBot from './bot'
|
import OcrccBot from './bot'
|
||||||
@@ -100,6 +101,7 @@ describe('OcrccBot', () => {
|
|||||||
mockSendTextMessage.mockClear()
|
mockSendTextMessage.mockClear()
|
||||||
mockAppendFileSync.mockClear()
|
mockAppendFileSync.mockClear()
|
||||||
mockGetGroupUsers.mockClear()
|
mockGetGroupUsers.mockClear()
|
||||||
|
mockSendStateEvent.mockClear()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@@ -325,4 +327,17 @@ describe('OcrccBot', () => {
|
|||||||
expect(mockStartClient).toHaveBeenCalled()
|
expect(mockStartClient).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('#sendBotSignal should send custom state event', () => {
|
||||||
|
const bot = new OcrccBot(botConfig)
|
||||||
|
bot.start()
|
||||||
|
const test_room_id = 'test_room_id'
|
||||||
|
const signal = 'END_CHAT'
|
||||||
|
|
||||||
|
bot.sendBotSignal(test_room_id, signal)
|
||||||
|
|
||||||
|
waitForExpect(() => {
|
||||||
|
expect(mockSendStateEvent).toHaveBeenCalledWith(test_room_id, 'm.bot.signal', { signal })
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
40
src/index.js
40
src/index.js
@@ -1,35 +1,9 @@
|
|||||||
require('dotenv').config()
|
import config from '../config.json';
|
||||||
|
import OcrccBot from './bot';
|
||||||
|
|
||||||
const ENCRYPTION_CONFIG = { algorithm: "m.megolm.v1.aes-sha2" };
|
const bot = new OcrccBot(config);
|
||||||
const KICK_REASON = "A facilitator has already joined this chat.";
|
try {
|
||||||
const BOT_ERROR_MESSAGE =
|
|
||||||
"Something went wrong on our end, please restart the chat and try again.";
|
|
||||||
const MAX_RETRIES = 3;
|
|
||||||
const {
|
|
||||||
MATRIX_SERVER_URL,
|
|
||||||
BOT_USERNAME,
|
|
||||||
BOT_USERID,
|
|
||||||
BOT_PASSWORD,
|
|
||||||
BOT_DISPLAY_NAME,
|
|
||||||
FACILITATOR_ROOM_ID,
|
|
||||||
CAPTURE_TRANSCRIPTS,
|
|
||||||
} = process.env;
|
|
||||||
|
|
||||||
const botConfig = {
|
|
||||||
ENCRYPTION_CONFIG,
|
|
||||||
KICK_REASON,
|
|
||||||
BOT_ERROR_MESSAGE,
|
|
||||||
MAX_RETRIES,
|
|
||||||
MATRIX_SERVER_URL,
|
|
||||||
BOT_USERNAME,
|
|
||||||
BOT_USERID,
|
|
||||||
BOT_PASSWORD,
|
|
||||||
BOT_DISPLAY_NAME,
|
|
||||||
FACILITATOR_ROOM_ID,
|
|
||||||
CAPTURE_TRANSCRIPTS,
|
|
||||||
}
|
|
||||||
|
|
||||||
import OcrccBot from './bot'
|
|
||||||
|
|
||||||
const bot = new OcrccBot(botConfig);
|
|
||||||
bot.start();
|
bot.start();
|
||||||
|
} catch(err) {
|
||||||
|
console.log("Unable to start bot", err)
|
||||||
|
}
|
||||||
|
|||||||
50
src/setup.js
Normal file
50
src/setup.js
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
const fetch = require('node-fetch');
|
||||||
|
const config = require('../config.json');
|
||||||
|
|
||||||
|
const getSettings = async () => {
|
||||||
|
try {
|
||||||
|
const url = `${config.settingsEndpoint}?homeserver=${encodeURIComponent(config.matrixServerUrl)}`;
|
||||||
|
|
||||||
|
if (!config.matrixServerUrl) {
|
||||||
|
throw new Error("The matrix server url is not provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Fetching settings for ${config.matrixServerUrl}`);
|
||||||
|
|
||||||
|
const res = await fetch(url);
|
||||||
|
const data = await res.json();
|
||||||
|
|
||||||
|
const { fields, schedule = [] } = data;
|
||||||
|
|
||||||
|
return Object.entries(fields).reduce(((settingsObj, [k,v]) => {
|
||||||
|
const [scope, key] = k.split('_');
|
||||||
|
|
||||||
|
if (scope === 'platform') {
|
||||||
|
settingsObj[key] = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
return settingsObj
|
||||||
|
}), {});
|
||||||
|
} catch (err) {
|
||||||
|
console.log("Error fetching settings", err);
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const writeConfig = async () => {
|
||||||
|
const settings = await getSettings()
|
||||||
|
if (!settings) {
|
||||||
|
return console.log('No settings to update')
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedSettings = Object.assign(config, settings)
|
||||||
|
|
||||||
|
fs.writeFile('config.json', JSON.stringify(updatedSettings, null, 2), function (err) {
|
||||||
|
if (err) return console.log("Error updating settings", err);
|
||||||
|
console.log(`Updated settings to config.json`);
|
||||||
|
console.log(updatedSettings);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
writeConfig();
|
||||||
@@ -3954,6 +3954,11 @@ node-environment-flags@^1.0.5:
|
|||||||
object.getownpropertydescriptors "^2.0.3"
|
object.getownpropertydescriptors "^2.0.3"
|
||||||
semver "^5.7.0"
|
semver "^5.7.0"
|
||||||
|
|
||||||
|
node-fetch@^2.6.1:
|
||||||
|
version "2.6.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
|
||||||
|
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
|
||||||
|
|
||||||
node-int64@^0.4.0:
|
node-int64@^0.4.0:
|
||||||
version "0.4.0"
|
version "0.4.0"
|
||||||
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
|
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
|
||||||
|
|||||||
Reference in New Issue
Block a user