UNPKG

wa-chat-server-microsoft

Version:

wa-chat-server adapter for the Microsoft Bot Framework

145 lines (144 loc) 6.79 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WatsonHandler = void 0; const botbuilder_1 = require("botbuilder"); const turndown_1 = __importDefault(require("turndown")); class WatsonHandler extends botbuilder_1.ActivityHandler { constructor(sessionId, waChatServer) { super(); this.sessionId = sessionId; this.waChatServer = waChatServer; this.log = waChatServer.getLogger(); this.onMessage(this.onMessageHandler.bind(this)); this.onConversationUpdate(this.onConversationUpdateHandler.bind(this)); this.turndown = new turndown_1.default(); } startSession() { return __awaiter(this, void 0, void 0, function* () { if (!this.session) { this.session = yield this.waChatServer.startSession(this.sessionId); } }); } onMessageHandler(context, next) { return __awaiter(this, void 0, void 0, function* () { this.log.info(`MicrosoftAdapter - WatsonHandler.ts - onConversationUpdate (${this.sessionId})`); yield this.startSession(); const watsonRequest = this.activityToWatsonRequest(context.activity); yield this.callWatsonAndRespond(watsonRequest, context); yield next(); }); } callWatsonAndRespond(watsonRequest, context) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { const response = yield this.waChatServer.getResponse(this.sessionId, watsonRequest); let item; for (item of ((_c = (_b = (_a = response.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) || []) { yield this.processWatsonResponseItem(item, context); } }); } processWatsonResponseItem(item, context) { return __awaiter(this, void 0, void 0, function* () { if (item.response_type === 'text') { return this.processWatsonResponseItemText(item, context); } if (item.response_type === 'option') { return this.processWatsonResponseItemOptions(item, context); } if (item.response_type === 'suggestion') { return this.processWatsonResponseItemSuggestions(item, context); } }); } processWatsonResponseItemText(item, context) { return __awaiter(this, void 0, void 0, function* () { yield context.sendActivity(this.turndown.turndown(item.text)); }); } processWatsonResponseItemOptions(item, context) { return __awaiter(this, void 0, void 0, function* () { const message = botbuilder_1.MessageFactory.suggestedActions(item.options.map((option) => { return { type: 'imBack', title: option.label, value: option.value.input.text, }; })); yield context.sendActivity(message); }); } processWatsonResponseItemSuggestions(item, context) { return __awaiter(this, void 0, void 0, function* () { this.session.data.microsoft = this.session.data.microsoft || {}; this.session.data.microsoft.suggestions = item.suggestions.map((suggestion) => ({ label: suggestion.label, input: suggestion.value.input, })); this.log.info(`MicrosoftAdapter - WatsonHandler (${this.session.sessionId}) - suggestions identified,` + ' saving to session:', { suggestions: this.session.data.microsoft.suggestions }); const message = botbuilder_1.MessageFactory.suggestedActions(item.suggestions.map((suggestion) => { return { type: 'imBack', title: suggestion.label, value: suggestion.label, }; })); context.sendActivity(message); }); } // TODO - the conversion start event needs some fine tuning (this method may be called not only // on conversation start) onConversationUpdateHandler(context, next) { return __awaiter(this, void 0, void 0, function* () { this.log.info(`MicrosoftAdapter - WatsonHandler.ts - onConversationUpdate (${this.sessionId})`); yield this.startSession(); const watsonRequest = { input: { text: '' } }; yield this.callWatsonAndRespond(watsonRequest, context); yield next(); }); } activityToText(activity) { return activity.text || activity.value; } activityToWatsonRequest(activity) { const result = {}; const suggestionInput = this.getInputForSuggestion(this.activityToText(activity)); result.input = suggestionInput || { text: this.activityToText(activity) }; return result; } getInputForSuggestion(utterance) { this.session.data.microsoft = this.session.data.microsoft || {}; const suggestions = this.session.data.microsoft.suggestions; delete this.session.data.microsoft.suggestions; if (suggestions) { const suggestion = suggestions.find((suggestion) => suggestion.label === utterance); const logHeader = `MicrosoftAdapter - WatsonHandler.ts (${this.session.sessionId}): `; if (suggestion) { this.log.info(`${logHeader} - Suggestion identified, using input:`, suggestion); return suggestion.input; } else { this.log.info(`${logHeader} - Response to suggestions does not match any suggestion`, { utterance, suggestions, }); } } return null; } } exports.WatsonHandler = WatsonHandler;