juicy-chat-bot
Version:
A light-weight and totally "secure" library to easily deploy simple chat bots
63 lines • 2.17 kB
JavaScript
"use strict";
/*
* Copyright (c) 2020-2023 Bjoern Kimminich & the OWASP Juice Shop contributors.
* SPDX-License-Identifier: MIT
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const vm2_1 = require("vm2");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const nlp_1 = __importDefault(require("./nlp"));
const ctx = fs_1.default.readFileSync(path_1.default.join(__dirname, 'factory.js')).toString();
class Bot {
constructor(name, greeting, trainingSet, defaultResponse) {
this.name = name;
this.greeting = greeting;
this.defaultResponse = { action: 'response', body: defaultResponse };
this.training = {
state: false,
data: trainingSet
};
this.factory = new vm2_1.VM({
sandbox: {
Nlp: nlp_1.default,
training: this.training
}
});
this.factory.run(ctx);
this.factory.run(`trainingSet=${trainingSet}`);
}
greet(token) {
return this.render(this.greeting, token);
}
render(statement, token) {
const currentUser = String(this.factory.run(`currentUser("${token}")`));
return statement.replace(/<bot-name>/g, this.name).replace(/<customer-name>/g, currentUser);
}
addUser(token, name) {
this.factory.run(`users.addUser("${token}", "${name}")`);
}
getUser(token) {
return this.factory.run(`users.get("${token}")`);
}
async respond(query, token) {
const response = (await this.factory.run(`processQuery("${query}", "${token}")`)).answer;
if (response == null) {
return this.defaultResponse;
}
else {
if (response.body != null) {
response.body = this.render(response.body, token);
}
return response;
}
}
train() {
return this.factory.run('train()');
}
}
exports.default = Bot;
//# sourceMappingURL=index.js.map