25 Commits

Author SHA1 Message Date
Sharon
46c2467368 Merge pull request #4 from nomadic-labs/use_admin_settings
Use admin settings
2020-12-01 13:26:02 -05:00
Sharon Kennedy
0ad65e9dcc remove encryption config 2020-12-01 13:25:06 -05:00
Sharon Kennedy
6c882a44cc latest build 2020-12-01 13:21:16 -05:00
Sharon Kennedy
666f2361c8 pull in user settings, fix race conditions on facilitator invitation, add more logging and notifications. 2020-12-01 13:20:13 -05:00
Sharon Kennedy
d3747cc654 fetch and use admin settings 2020-11-30 22:47:32 -05:00
Sharon Kennedy
687dfa84e9 2.0.2 2020-09-11 16:29:31 -04:00
Sharon Kennedy
b7da515969 Merge branch 'master' of github.com:nomadic-labs/ocrcc-bot 2020-09-11 16:29:08 -04:00
Sharon Kennedy
e2c291c040 change notice to signal 2020-09-11 16:12:01 -04:00
brent
87f2c5290f add timezone to dockerfile 2020-09-08 10:41:29 -04:00
Sharon Kennedy
4fdddc8c92 2.0.1 2020-09-07 12:00:16 -04:00
Sharon Kennedy
d53a5c5e26 change order of statements when handling member leaving to avoid sending close signal twice 2020-09-07 11:54:05 -04:00
brent
9772b3b1fd different strategy for python dependencies 2020-09-06 17:47:05 -04:00
brent
84fe91adbb new attempt at python install 2020-09-06 17:38:57 -04:00
brent
b6cd85eab0 removed parameters from python install 2020-09-06 17:33:12 -04:00
brent
209b3aaa45 removed sudo 2020-09-06 17:28:23 -04:00
edwardsbrentg
4912ce6f61 Merge pull request #3 from nomadic-labs/install-python-dockerfile
add python installation to dockerfile
2020-09-06 17:22:13 -04:00
brent
013ffa318f add python installation to dockerfile 2020-09-06 17:20:49 -04:00
Sharon Kennedy
ac2f953b90 2.0.0 2020-09-06 14:07:50 -04:00
Sharon Kennedy
a25c71a04a latest build 2020-09-06 14:07:44 -04:00
Sharon
836d4751ad Merge pull request #2 from nomadic-labs/botsignal
Move timeouts to bot and use custom event to signal clients to close
2020-09-06 14:06:28 -04:00
Sharon Kennedy
2983a14038 bit of cleanup 2020-09-06 14:05:54 -04:00
Sharon Kennedy
6e71ba4b5b add test for bot signal 2020-09-06 13:39:39 -04:00
Sharon Kennedy
5dc6aa5660 timeouts 2020-09-06 01:20:50 -04:00
Sharon Kennedy
6d4b6b1609 close if facilitator doesn't join within 3 minutes 2020-09-05 18:12:33 -04:00
Sharon Kennedy
1e3b63fe5b fixed bug on leaving room 2020-09-05 15:35:38 -04:00
16 changed files with 773 additions and 435 deletions

View File

@@ -1,7 +0,0 @@
MATRIX_SERVER_URL=
BOT_DISPLAY_NAME=
BOT_USERNAME=
BOT_PASSWORD=
BOT_USERID=
FACILITATOR_ROOM_ID=
CAPTURE_TRANSCRIPTS

1
.gitignore vendored
View File

@@ -3,3 +3,4 @@ node_modules
*.log
__mocks__/test_transcript.txt
transcripts/*.txt
config.json

View File

@@ -1,5 +1,10 @@
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
WORKDIR /home/node/app

View File

@@ -1,19 +1,10 @@
# Safe Support Chat Bot
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 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.
The bot can be configured with an `.env` file with the following variables:
```
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?
* 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
@@ -29,7 +20,7 @@ CAPTURE_TRANSCRIPTS=
### Bot commands
|Command|Response|
--- | ---
--- | ---
|`!bot hi`|Bot responds with a greeting|
|`!bot transcript`|Bot sends the chat transcript as a .txt file|
|`!bot transcript please`|Bot happily sends the transcript :)|
@@ -48,9 +39,14 @@ cd safesupport-bot
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

View File

@@ -157,6 +157,10 @@ export const mockGetGroupUsers = jest.fn(() => {
export const mockGetUser = jest.fn().mockReturnValue({ presence: 'online'});
export const mockSendStateEvent = jest.fn(() => {
return Promise.resolve();
});
export const mockClient = {
registerRequest: mockRegisterRequest,
initCrypto: mockInitCrypto,
@@ -186,6 +190,7 @@ export const mockClient = {
getJoinedRoomMembers: mockGetJoinedRoomMembers,
getUser: mockGetUser,
getGroupUsers: mockGetGroupUsers,
sendStateEvent: mockSendStateEvent,
}
export const WebStorageSessionStore = jest.fn()

437
dist/bot.js vendored

File diff suppressed because it is too large Load Diff

14
dist/bot.test.js vendored
View File

@@ -103,6 +103,8 @@ describe('OcrccBot', () => {
mockAppendFileSync.mockClear();
_matrixJsSdk.mockGetGroupUsers.mockClear();
_matrixJsSdk.mockSendStateEvent.mockClear();
});
test('constructor should inititialize class variables', () => {
const bot = new _bot.default(botConfig);
@@ -276,4 +278,16 @@ describe('OcrccBot', () => {
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
});
});
});
});

39
dist/index.js vendored
View File

@@ -2,37 +2,14 @@
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _config = _interopRequireDefault(require("../config.json"));
var _bot = _interopRequireDefault(require("./bot"));
require('dotenv').config();
const bot = new _bot.default(_config.default);
const ENCRYPTION_CONFIG = {
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();
try {
bot.start();
} catch (err) {
console.log("Unable to start bot", err);
}

54
dist/setup.js vendored Normal file
View 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();

View File

@@ -1,11 +1,12 @@
{
"name": "private-safesupport-bot",
"version": "1.2.0",
"version": "2.0.2",
"description": "Chatbot to manage interactions on Safe Support Chat",
"main": "dist/index.js",
"scripts": {
"develop": "nodemon --exec babel-node src/index.js",
"build": "babel src -d dist",
"setup": "node src/setup.js",
"start": "yarn build && node dist/index.js",
"test": "jest"
},
@@ -14,6 +15,7 @@
"dependencies": {
"dotenv": "^8.2.0",
"matrix-js-sdk": "^6.2.1",
"node-fetch": "^2.6.1",
"node-localstorage": "^2.1.5",
"node-webcrypto-ossl": "^2.1.0",
"olm": "https://packages.matrix.org/npm/olm/olm-3.1.4.tgz",

16
sample.config.json Normal file
View 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
}

File diff suppressed because it is too large Load Diff

View File

@@ -32,7 +32,8 @@ import {
mockKick,
mockGetJoinedRoomMembers,
mockGetUser,
mockGetGroupUsers
mockGetGroupUsers,
mockSendStateEvent,
} from "matrix-js-sdk";
import OcrccBot from './bot'
@@ -100,6 +101,7 @@ describe('OcrccBot', () => {
mockSendTextMessage.mockClear()
mockAppendFileSync.mockClear()
mockGetGroupUsers.mockClear()
mockSendStateEvent.mockClear()
})
@@ -325,4 +327,17 @@ describe('OcrccBot', () => {
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 })
})
})
})

View File

@@ -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 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 OcrccBot(config);
try {
bot.start();
} catch(err) {
console.log("Unable to start bot", err)
}
import OcrccBot from './bot'
const bot = new OcrccBot(botConfig);
bot.start();

50
src/setup.js Normal file
View 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();

View File

@@ -3954,6 +3954,11 @@ node-environment-flags@^1.0.5:
object.getownpropertydescriptors "^2.0.3"
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:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"