@ebonydevcopy/framework
Version:
A module-based NodeJS chatbot framework.
173 lines • 4.29 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function createScenario(id, adapter) {
const scenarios = {
adapter,
_consolidated: false,
id,
_actions: [],
end,
send,
wait,
types,
typeAndWait,
seen,
stopTyping,
notify
// handover
};
return scenarios;
}
exports.default = createScenario;
function notify(...params) {
if (this._consolidated) {
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'notify',
params: [...params]
});
return this;
}
function handover(...params) {
if (this._consolidated) {
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'handover',
params: [this.id, ...params]
});
return this;
}
async function end() {
try {
this._consolidated = true;
Object.freeze(this);
const actions = processScenario(this._actions);
await this.adapter.sender(actions, 'ORDERED');
}
catch (err) {
throw err;
}
}
function wait(millis) {
if (this._consolidated) {
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'wait',
params: [millis]
});
return this;
}
function send(message, options = {}) {
if (this._consolidated) {
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'message',
params: [this.id, message, options]
});
return this;
}
function types() {
if (this._consolidated) {
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'typing_on',
params: [this.id]
});
return this;
}
function seen() {
if (this._consolidated) {
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'mark_seen',
params: [this.id]
});
return this;
}
function stopTyping() {
if (this._consolidated) {
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'typing_off',
params: [this.id]
});
return this;
}
function typeAndWait(millis) {
this.types();
this.wait(millis);
return this;
}
function processScenario(actions) {
let waiter = 0;
const messages = [];
for (const action of actions) {
const { call, params } = action;
switch (call) {
case 'wait':
waiter += params[0];
continue;
case 'message':
messages.push({
type: 'message',
id: params[0],
message: params[1],
options: {
...params[2],
delay: waiter
}
});
waiter = 0;
continue;
case 'typing_on':
messages.push({
type: 'typing_on',
id: params[0],
options: {
delay: waiter
}
});
waiter = 0;
continue;
case 'typing_off':
messages.push({
type: 'typing_off',
id: params[0],
options: {
delay: waiter
}
});
waiter = 0;
continue;
case 'mark_seen':
messages.push({
type: 'mark_seen',
id: params[0],
options: {
delay: waiter
}
});
waiter = 0;
continue;
case 'notify':
messages.push({
type: 'notify',
notifyData: params[0],
options: {
delay: waiter
}
});
waiter = 0;
continue;
}
}
return messages;
}
//# sourceMappingURL=scenario.js.map