UNPKG

wa-chat-server-mluvii

Version:

Mluvii adapter for the modules wa-chat-server

101 lines (100 loc) 3.79 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WatsonToMluviiConverter = void 0; const turndown_1 = __importDefault(require("turndown")); class WatsonToMluviiConverter { constructor(watsonResponse /* AssistantV1.Response */, dependencies, session) { this.watsonResponse = watsonResponse; this.dependencies = dependencies; this.session = session; this.turndown = new turndown_1.default(); this.log = dependencies.log; } textToActivity(item) { return { type: 'message', text: this.turndown.turndown(item.text), }; } optionsToActivity(item) { return { type: 'message', attachments: [ { contentType: 'application/vnd.microsoft.card.hero', content: { buttons: item.options.map((option) => ({ type: 'imBack', title: option.label, value: option.value.input.text, })), }, }, ], }; } suggestionsToActivity(item) { return { type: 'message', text: this.turndown.turndown(item.title), attachments: [ { contentType: 'application/vnd.microsoft.card.hero', content: { buttons: item.suggestions.map((option) => ({ type: 'imBack', title: option.label, value: option.label, })), }, }, ], }; } responseItemToActivity(item) { if (item.response_type === 'text') { return this.textToActivity(item); } if (item.response_type === 'option') { return this.optionsToActivity(item); } if (item.response_type === 'suggestion') { this.session.data.mluvii = this.session.data.mluvii || {}; this.session.data.mluvii.suggestions = item.suggestions.map((suggestion) => ({ label: suggestion.label, input: suggestion.value.input, })); this.log.info(`WatsonToMluviiConverter (${this.session.sessionId}) - suggestions identified,` + ' saving to session:', { suggestions: this.session.data.mluvii.suggestions }); return this.suggestionsToActivity(item); } } joinActivities(activities) { return activities.reduce((result, activity) => { if (result.length > 0 && !result[result.length - 1].attachments && activity.attachments && !activity.text) { result[result.length - 1].attachments = activity.attachments; } else { result.push(activity); } return result; }, []); } /* * Converts watson response to array of Mluvii activities */ convert() { var _a, _b, _c; const activities = (((_c = (_b = (_a = this.watsonResponse) === null || _a === void 0 ? void 0 : _a.result) === null || _b === void 0 ? void 0 : _b.output) === null || _c === void 0 ? void 0 : _c.generic) || []) .map(this.responseItemToActivity.bind(this)) .filter(item => item !== null); return this.joinActivities(activities); } } exports.WatsonToMluviiConverter = WatsonToMluviiConverter;