@ebonydevcopy/framework
Version:
A module-based NodeJS chatbot framework.
91 lines • 3.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPayload = exports.addTextRule = exports.addBaseLocationRule = exports.addPostbackRule = exports.addAction = exports.createModule = void 0;
function createModule(name = 'global') {
const module = {
actions: {},
intents: {},
routes: {
stringPayloads: {},
objectPayloads: {}
},
referrals: {},
text: [],
preMiddlewares: [],
postMiddlewares: [],
name
};
return module;
}
exports.createModule = createModule;
function addAction(module, action) {
if (module.actions === undefined) {
module.actions = {};
}
const actionName = module.name + '/' + action.name;
if (actionName in module.actions) {
throw new Error(`Action with name: '${actionName}', already exists!`);
}
module.actions[module.name + '/' + action.name] = action;
}
exports.addAction = addAction;
function addPostbackRule(module, action, type) {
const actionName = module.name + '/' + action.name;
if (module.actions === undefined || !(actionName in module.actions)) {
throw new Error(`Action with name: '${actionName}', doesn't exist!`);
}
if (module.routes === undefined) {
module.routes = {
stringPayloads: {},
objectPayloads: {}
};
}
const categoryName = type === 'string' ? 'stringPayloads' : 'objectPayloads';
let category = type === 'string' ? module.routes.stringPayloads : module.routes.objectPayloads;
if (!category) {
category = {};
module.routes[categoryName] = category;
}
// Here we need to add the bot object (bot.actions.exec...)
category[actionName] = actionName;
if (type === 'string') {
return actionName;
}
return { type: actionName };
}
exports.addPostbackRule = addPostbackRule;
function addBaseLocationRule(module, action) {
return addTextRule(module, action, /USER_SEND_LOCATION/);
}
exports.addBaseLocationRule = addBaseLocationRule;
function addTextRule(module, action, rule) {
const actionName = module.name + '/' + action.name;
if (module.actions === undefined || !(actionName in module.actions)) {
throw new Error(`Action with name: '${actionName}', doesn't exist!`);
}
if (module.text === undefined) {
module.text = [];
}
if (Array.isArray(rule)) {
module.text.concat(rule.map((r) => ({ regex: r, action: actionName })));
}
else {
module.text.push({ regex: rule, action: actionName });
}
}
exports.addTextRule = addTextRule;
function createPayload(module, action, type = 'string', payloadData) {
const actionName = module.name + '/' + action.name;
if (module.actions === undefined || !(actionName in module.actions)) {
throw new Error(`Action with name: '${actionName}', doesn't exist!`);
}
if (type === 'string') {
return actionName;
}
if (payloadData !== undefined) {
return { type: actionName, ...payloadData };
}
return { type: actionName };
}
exports.createPayload = createPayload;
//# sourceMappingURL=index.js.map