UNPKG

@botonic/core

Version:

Runtime and APIs for Botonic bots: actions, context, messaging, and integration hooks used by the **current** framework line.

288 lines 8.92 kB
import { MessagingChannel } from '@botonic/shared'; import { DEFAULT_WEBCHAT_CLIENT_SETTINGS, } from '@botonic/shared'; import { BotonicAction } from './botonic-action'; /** * Maps camelCase session updates to snake_case PATCH body (v2 only). */ export function sessionUpdateToPatchBody(update) { const body = {}; if (update.isFirstInteraction !== undefined) { body.is_first_interaction = update.isFirstInteraction; } if (update.shadowing !== undefined) body.shadowing = update.shadowing; if (update.flowThreadId !== undefined) { body.flow_thread_id = update.flowThreadId; } if (update.custom !== undefined) body.custom = update.custom; if (update.webviewsCustom !== undefined) { body.webviews_custom = update.webviewsCustom; } if (update.webchatClientSettings !== undefined) { body.webchat_client_settings = update.webchatClientSettings; } if (update.captureUserInputNodeId !== undefined) { const id = update.captureUserInputNodeId; if (id === null || id === '') { body.capture_user_input_node_id = null; } else { body.capture_user_input_node_id = id; } } return body; } /** * Mutates `session.raw` immediately so the rest of the turn sees updates. * Handles `capture_user_input_node_id` (mirror of {@link sessionUpdateToPatchBody}) and * `_botonic_action` (not sent on session PATCH). * For `isFirstInteraction`, also updates in-memory `is_first_interaction`; the same * value is emitted by {@link sessionUpdateToPatchBody} for `PATCH …/session/`. */ export function applyNonPatchSessionUpdateFields(session, updates) { if (updates.captureUserInputNodeId !== undefined) { const id = updates.captureUserInputNodeId; if (id === null || id === '') { delete session.capture_user_input_node_id; } else { session.capture_user_input_node_id = id; } } if (updates.isFirstInteraction !== undefined) { session.is_first_interaction = updates.isFirstInteraction; } if (updates.botonicAction !== undefined) { const w = session; if (updates.botonicAction === null || updates.botonicAction === '') { delete w._botonic_action; } else { w._botonic_action = updates.botonicAction; } } } /** * Maps `BotonicSessionUpdate.user` to `PATCH …/users/<user_id>/` body (ChatUserPatchSerializer keys, snake_case). * Unknown keys are omitted — only serializer fields are sent. */ export function userUpdateToChatUserPatchBody(user) { if (user === undefined) return {}; const body = {}; if (user.overrideName !== undefined) { body.override_name = user.overrideName; } if (user.locale !== undefined) { body.locale = user.locale; } if (user.country !== undefined) { body.country = user.country; } if (user.systemLocale !== undefined) { body.system_locale = user.systemLocale; } if (user.extraData !== undefined) { body.extra_data = user.extraData; } return body; } export class BotonicContextSessionUser { constructor(rawUser, onUserUpdate) { this.rawUser = rawUser; this.onUserUpdate = onUserUpdate; } get raw() { return this.rawUser; } get id() { return this.rawUser.id; } get username() { var _a; return (_a = this.rawUser.username) !== null && _a !== void 0 ? _a : undefined; } get name() { var _a; return (_a = this.rawUser.name) !== null && _a !== void 0 ? _a : undefined; } get overrideName() { var _a; return (_a = this.rawUser.override_name) !== null && _a !== void 0 ? _a : undefined; } get providerId() { return this.rawUser.provider_id; } get locale() { return this.rawUser.locale; } set locale(v) { this.rawUser.locale = v; } get country() { return this.rawUser.country; } set country(v) { this.rawUser.country = v; } get systemLocale() { return this.rawUser.system_locale; } set systemLocale(v) { this.rawUser.system_locale = v; } get extraData() { return this.rawUser.extra_data; } set extraData(v) { var _a; this.rawUser.extra_data = v; void ((_a = this.onUserUpdate) === null || _a === void 0 ? void 0 : _a.call(this, { extraData: v })); } get contactInfo() { return this.rawUser.contact_info; } } export class BotonicContextSession { constructor(sessionData) { this.sessionData = sessionData; } setOnUpdate(cb) { this.onUpdate = cb; } /** Replace underlying session object (e.g. after GET session from Hubtype). */ replaceSession(next) { this.sessionData = next; } get raw() { return this.sessionData; } get bot() { return this.sessionData.bot; } get user() { const notify = this.onUpdate; return new BotonicContextSessionUser(this.sessionData.user, notify !== undefined ? updates => { notify({ user: updates }); } : undefined); } get organizationId() { return this.sessionData.organization.id; } get organizationName() { return this.sessionData.organization.name; } get isFirstInteraction() { return this.sessionData.is_first_interaction; } get botonicAction() { const w = this.sessionData; const v = w._botonic_action; return typeof v === 'string' ? v : undefined; } set botonicAction(v) { const w = this.sessionData; if (v === undefined || v === '') { delete w._botonic_action; } else { w._botonic_action = v; } } isBotonicActionRedirect() { const a = this.botonicAction; const prefix = `${BotonicAction.Redirect}:`; return typeof a === 'string' && a.startsWith(prefix); } getBotonicActionPayload(action) { const prefix = `${action}:`; const a = this.botonicAction; if (typeof a !== 'string' || !a.startsWith(prefix)) return undefined; return a.slice(prefix.length); } get isTestIntegration() { return this.sessionData.is_test_integration; } get flowThreadId() { return this.sessionData.flow_thread_id; } get shadowing() { return this.sessionData.shadowing; } set shadowing(v) { this.sessionData.shadowing = v; } get hadHubtypeHandoff() { return this.sessionData.had_hubtype_handoff; } get hubtypeCase() { return this.sessionData.hubtype_case; } /** Convenience for legacy code paths; prefer {@link hubtypeCase}. */ get hubtypeCaseId() { var _a; return (_a = this.sessionData.hubtype_case) === null || _a === void 0 ? void 0 : _a.id; } get custom() { return this.sessionData.custom; } get external() { return this.sessionData.external; } get webviewsCustom() { return this.sessionData.webviews_custom; } get webchatClientSettings() { var _a; return ((_a = this.sessionData.webchat_client_settings) !== null && _a !== void 0 ? _a : DEFAULT_WEBCHAT_CLIENT_SETTINGS); } /** Flow Builder: node id capturing user input (`capture_user_input_node_id` in JSON). */ get captureUserInputNodeId() { const id = this.sessionData.capture_user_input_node_id; if (id == null || id === '') { return undefined; } return id; } /** Channel provider (e.g. webchat, whatsapp). */ get provider() { return this.sessionData.channel.provider; } get impId() { return this.sessionData.channel.imp_id; } get unformattedPhoneNumber() { var _a; return (_a = this.sessionData.channel.unformatted_phone_number) !== null && _a !== void 0 ? _a : undefined; } isDev() { return this.provider === MessagingChannel.Webchat; } isWebchat() { return this.provider === MessagingChannel.Webchat; } isTelegram() { return this.provider === MessagingChannel.Telegram; } isFacebook() { return this.provider === MessagingChannel.Facebook; } isInstagram() { return this.provider === MessagingChannel.Instagram; } isTwitter() { return this.provider === MessagingChannel.Twitter; } isWhatsapp() { return this.provider === MessagingChannel.Whatsapp; } hasFlowThreadId() { const ft = this.flowThreadId; return ft != null && ft !== ''; } } //# sourceMappingURL=botonic-context-session.js.map