Compare commits

..

No commits in common. "master" and "botsignal" have entirely different histories.

14 changed files with 343 additions and 535 deletions

7
.env.sample Normal file
View File

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

1
.gitignore vendored
View File

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

View File

@ -1,10 +1,5 @@
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

View File

@ -1,10 +1,19 @@
# Safe Support Chat Bot # 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/Safe-Support-Chat/ocrcc-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? ## 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
@ -30,7 +39,7 @@ If you prefer to develop locally instead of on Glitch:
Clone the project Clone the project
``` ```
git clone https://github.com/Safe-Support-Chat/ocrcc-bot.git git clone https://github.com/nomadic-labs/safesupport-bot.git
``` ```
Install dependencies Install dependencies
@ -39,14 +48,9 @@ cd safesupport-bot
yarn yarn
``` ```
Copy the sample config file and add in the missing values. Copy the sample `.env` file and add in your own variables
``` ```
cp sample.config.json config.json cp .env.sample .env
```
Pull in the user-defined settings (if there are any).
```
yarn setup
``` ```
Start the local server Start the local server

271
dist/bot.js vendored

File diff suppressed because it is too large Load Diff

14
dist/bot.test.js vendored
View File

@ -103,8 +103,6 @@ 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);
@ -278,16 +276,4 @@ 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
View File

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

54
dist/setup.js vendored
View File

@ -1,54 +0,0 @@
"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,12 +1,11 @@
{ {
"name": "private-safesupport-bot", "name": "private-safesupport-bot",
"version": "3.1.0", "version": "1.2.0",
"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"
}, },
@ -15,7 +14,6 @@
"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",

View File

@ -1,16 +0,0 @@
{
"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

@ -1,9 +1,42 @@
import config from '../config.json'; require('dotenv').config()
import OcrccBot from './bot';
const bot = new OcrccBot(config); const ENCRYPTION_CONFIG = { algorithm: "m.megolm.v1.aes-sha2" };
try { const KICK_REASON = "A facilitator has already joined this chat.";
bot.start(); const BOT_ERROR_MESSAGE =
} catch(err) { "Something went wrong on our end, please restart the chat and try again.";
console.log("Unable to start bot", err) const MAX_RETRIES = 3;
const {
MATRIX_SERVER_URL,
BOT_USERNAME,
BOT_USERID,
BOT_PASSWORD,
BOT_DISPLAY_NAME,
FACILITATOR_ROOM_ID,
CAPTURE_TRANSCRIPTS,
CHAT_NOT_AVAILABLE_MESSAGE,
MAX_WAIT_TIME,
MAX_INACTIVE,
} = 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,
CHAT_NOT_AVAILABLE_MESSAGE,
MAX_WAIT_TIME,
MAX_INACTIVE,
} }
import OcrccBot from './bot'
const bot = new OcrccBot(botConfig);
bot.start();

View File

@ -1,50 +0,0 @@
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,11 +3954,6 @@ 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"