@botonic/core
Version:
Runtime and APIs for Botonic bots: actions, context, messaging, and integration hooks used by the **current** framework line.
124 lines • 5.44 kB
JavaScript
import { __awaiter } from "tslib";
import { isUpdateWebchatClientSettingsPayload, MessageType, } from '@botonic/shared';
import { updateWebchatClientSettingsAction } from '../actions';
import { runPlugins } from '../plugins';
import { Router } from '../routes/router';
import { BotonicAction, BotonicContextFactory, PluginMode, } from '../server';
import { HubtypeApiService } from '../services/hubtype-api-service';
/**
* CoreBot: accepts v2-shaped `BotonicRequest` and runs
* input() → createBotonicContext → runInput (route, plugins, redirect).
*/
export class CoreBot {
constructor({ appId, defaultDelay = 0.4, defaultRoutes = () => [], defaultTyping = 0.6, defaultTypingMode = 'inBetweenTyping', plugins, routes, }) {
this.hubtypeService = new HubtypeApiService();
this.appId = appId;
this.defaultDelay = defaultDelay;
this.defaultRoutes = defaultRoutes;
this.defaultTyping = defaultTyping;
this.defaultTypingMode = defaultTypingMode;
this.plugins = plugins || {};
this.routes = routes;
this.botonicContextFactory = new BotonicContextFactory(this.hubtypeService, this.plugins, this.defaultTyping, this.defaultDelay, this.defaultTypingMode);
}
run(botonicRequest) {
return __awaiter(this, void 0, void 0, function* () {
const botonicContext = this.botonicContextFactory.create(botonicRequest);
yield this.runInput(botonicContext);
if (botonicContext.session.isBotonicActionRedirect()) {
yield this.runRedirectAction(botonicContext);
}
});
}
runInput(botonicContext) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (botonicContext.input.type === MessageType.TypingEvent) {
botonicContext.response = {
status: 200,
response: 'OK',
};
return botonicContext;
}
yield this.runPrePlugins(botonicContext);
const route = yield this.getRoute(botonicContext);
const actionResponse = yield ((_a = route.action) === null || _a === void 0 ? void 0 : _a.call(route, botonicContext));
yield this.runPostPlugins(botonicContext, actionResponse);
botonicContext.response = actionResponse !== null && actionResponse !== void 0 ? actionResponse : { status: 200, response: 'OK' };
return botonicContext;
});
}
runPrePlugins(botonicContext) {
return __awaiter(this, void 0, void 0, function* () {
if (this.plugins) {
yield runPlugins({
botonicContext,
mode: PluginMode.PRE,
});
}
});
}
getRoute(botonicContext) {
return __awaiter(this, void 0, void 0, function* () {
const computedRoutes = this.routes(botonicContext);
const router = new Router([
...this.getCoreRoutes(botonicContext),
...computedRoutes,
...this.defaultRoutes(botonicContext),
]);
const route = router.processInput(botonicContext);
return route;
});
}
getCoreRoutes(botonicContext) {
return [
{
path: 'update-webchat-client-settings',
payload: payload => typeof payload === 'string' &&
isUpdateWebchatClientSettingsPayload(payload),
action: () => __awaiter(this, void 0, void 0, function* () { return yield updateWebchatClientSettingsAction(botonicContext); }),
},
];
}
runPostPlugins(botonicContext, response) {
return __awaiter(this, void 0, void 0, function* () {
if (this.plugins) {
yield runPlugins({
botonicContext,
mode: PluginMode.POST,
response,
});
}
});
}
runRedirectAction(botonicContext_1) {
return __awaiter(this, arguments, void 0, function* (botonicContext, numOfRedirects = 0) {
var _a;
if (numOfRedirects > 10) {
throw new Error('Maximum BotAction recursive calls reached (10)');
}
const nextPayload = botonicContext.session.getBotonicActionPayload(BotonicAction.Redirect);
const hadPayload = Boolean(nextPayload);
if (!hadPayload) {
yield botonicContext.updateBotSession({ botonicAction: null });
botonicContext.response = {
status: 200,
response: 'OK',
};
return botonicContext;
}
yield botonicContext.updateBotSession({ botonicAction: null });
botonicContext.input.setAsPostback(nextPayload);
const followUpContext = yield this.runInput(botonicContext);
if (botonicContext.session.isBotonicActionRedirect()) {
return yield this.runRedirectAction(botonicContext, numOfRedirects + 1);
}
followUpContext.response = (_a = followUpContext.response) !== null && _a !== void 0 ? _a : {
status: 200,
response: 'OK',
};
return followUpContext;
});
}
}
//# sourceMappingURL=index.js.map