UNPKG

botbuilder-core

Version:

Core components for Microsoft Bot Builder. Components in this library can run either in a browser or on the server.

456 lines 14.2 kB
"use strict"; /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ActivityFactory = void 0; const botframework_schema_1 = require("botframework-schema"); const messageFactory_1 = require("./messageFactory"); const cardFactory_1 = require("./cardFactory"); /** * The ActivityFactory * to generate text and then uses simple markdown semantics like chatdown to create Activity. */ class ActivityFactory { /** * Generate the activity. * * @param lgResult string result from languageGenerator. * @returns The generated MessageActivity. */ static fromObject(lgResult) { if (lgResult == null) { return { type: botframework_schema_1.ActivityTypes.Message }; } if (typeof lgResult === 'string') { return this.buildActivityFromText(lgResult.trim()); } if (typeof lgResult === 'object') { return this.buildActivityFromLGStructuredResult(lgResult); } else { return this.buildActivityFromText(JSON.stringify(lgResult).trim()); } } /** * Given a lg result, create a text activity. This method will create a MessageActivity from text. * * @param text lg text output. * @returns The created MessageActivity. */ static buildActivityFromText(text) { const msg = { type: botframework_schema_1.ActivityTypes.Message, }; if (text) { msg.text = text; msg.speak = text; } return msg; } /** * Given a structured lg result, create an activity. This method will create an MessageActivity from object * * @param lgValue lg output. * @returns The created MessageActivity. */ static buildActivityFromLGStructuredResult(lgValue) { let activity = {}; const type = this.getStructureType(lgValue); if (this.genericCardTypeMapping.has(type) || type === 'attachment') { activity = messageFactory_1.MessageFactory.attachment(this.getAttachment(lgValue)); } else if (type === 'activity') { activity = this.buildActivity(lgValue); } else if (lgValue) { activity = this.buildActivityFromText(JSON.stringify(lgValue).trim()); } return activity; } /** * Builds an [Activity](xref:botframework-schema.Activity) with a given message. * * @param messageValue Message value on which to base the activity. * @returns [Activity](xref:botframework-schema.Activity) with the given message. */ static buildActivity(messageValue) { const activity = { type: botframework_schema_1.ActivityTypes.Message }; for (const key of Object.keys(messageValue)) { const property = key.trim(); if (property === this.lgType) { continue; } const value = messageValue[key]; switch (property.toLowerCase()) { case 'text': activity.text = typeof value === 'string' ? value : JSON.stringify(value); break; case 'speak': activity.speak = typeof value === 'string' ? value : JSON.stringify(value); break; case 'attachments': activity.attachments = this.getAttachments(value); break; case 'suggestedactions': activity.suggestedActions = this.getSuggestions(value); break; default: activity[this.realProperty(property, this.activityProperties)] = value; break; } } return activity; } /** * @private */ static getSuggestions(suggestionsValue) { const actions = this.normalizedToList(suggestionsValue); const suggestedActions = { actions: this.getCardActions(actions), to: [], }; return suggestedActions; } /** * @private */ static getButtons(buttonsValue) { const actions = this.normalizedToList(buttonsValue); return this.getCardActions(actions); } /** * @private */ static getCardActions(actions) { return actions.map((u) => this.getCardAction(u)); } /** * @private */ static getCardAction(action) { let cardAction; if (typeof action === 'string') { cardAction = { type: botframework_schema_1.ActionTypes.ImBack, value: action, title: action, channelData: undefined }; } else { const type = this.getStructureType(action); cardAction = { type: botframework_schema_1.ActionTypes.ImBack, title: '', value: '', }; if (type === 'cardaction') { for (const key of Object.keys(action)) { const property = key.trim(); if (property === this.lgType) { continue; } cardAction[this.realProperty(property, this.cardActionProperties)] = action[key]; } } } return cardAction; } /** * @private */ static getAttachments(input) { const attachments = []; const attachmentsJsonList = this.normalizedToList(input); for (const attachmentsJson of attachmentsJsonList) { if (typeof attachmentsJson === 'object') { attachments.push(this.getAttachment(attachmentsJson)); } } return attachments; } /** * @private */ static getAttachment(input) { let attachment = { contentType: '', }; const type = this.getStructureType(input); if (this.genericCardTypeMapping.has(type)) { attachment = this.getCardAttachment(this.genericCardTypeMapping.get(type), input); } else if (type === 'adaptivecard') { attachment = cardFactory_1.CardFactory.adaptiveCard(input); } else if (type === 'attachment') { attachment = this.getNormalAttachment(input); } else { attachment = { contentType: type, content: input }; } return attachment; } /** * @private */ static getNormalAttachment(input) { const attachment = { contentType: '' }; for (const key of Object.keys(input)) { const property = key.trim(); const value = input[key]; switch (property.toLowerCase()) { case 'contenttype': { const type = value.toString().toLowerCase(); if (this.genericCardTypeMapping.has(type)) { attachment.contentType = this.genericCardTypeMapping.get(type); } else if (type === 'adaptivecard') { attachment.contentType = this.adaptiveCardType; } else { attachment.contentType = type; } break; } default: { attachment[this.realProperty(property, this.attachmentProperties)] = value; break; } } } return attachment; } /** * @private */ static getCardAttachment(type, input) { const card = {}; for (const key of Object.keys(input)) { const property = key.trim().toLowerCase(); const value = input[key]; switch (property) { case 'tap': { card[property] = this.getCardAction(value); break; } case 'image': case 'images': { if (type === cardFactory_1.CardFactory.contentTypes.heroCard || type === cardFactory_1.CardFactory.contentTypes.thumbnailCard) { if (!('images' in card)) { card['images'] = []; } const imageList = this.normalizedToList(value); imageList.forEach((u) => card['images'].push(this.normalizedToMediaOrImage(u))); } else { card['image'] = this.normalizedToMediaOrImage(value); } break; } case 'media': { if (!('media' in card)) { card['media'] = []; } const mediaList = this.normalizedToList(value); mediaList.forEach((u) => card['media'].push(this.normalizedToMediaOrImage(u))); break; } case 'buttons': { if (!('buttons' in card)) { card['buttons'] = []; } const buttons = this.getButtons(value); buttons.forEach((u) => card[property].push(u)); break; } case 'autostart': case 'shareable': case 'autoloop': { const boolValue = this.getValidBooleanValue(value.toString()); if (boolValue !== undefined) { card[property] = boolValue; } else { card[property] = value; } break; } default: { card[this.realProperty(key.trim(), this.cardProperties)] = value; break; } } } const attachment = { contentType: type, content: card, }; return attachment; } /** * @private */ static realProperty(property, builtinProperties) { const properties = builtinProperties.map((u) => u.toLowerCase()); if (properties.includes(property.toLowerCase())) { return builtinProperties[properties.indexOf(property.toLowerCase())]; } else { return property; } } /** * @private */ static normalizedToList(item) { if (item === undefined) { return []; } else if (Array.isArray(item)) { return item; } else { return [item]; } } /** * @private */ static getStructureType(input) { let result = ''; if (input && typeof input === 'object') { if (this.lgType in input) { result = input[this.lgType].toString(); } else if ('type' in input) { // Adaptive card type result = input['type'].toString(); } } return result.trim().toLowerCase(); } /** * @private */ static normalizedToMediaOrImage(item) { if (!item) { return {}; } else if (typeof item === 'string') { return { url: item }; } else return item; } /** * @private */ static getValidBooleanValue(boolValue) { if (typeof boolValue === 'boolean') { return boolValue; } if (boolValue.toLowerCase() === 'true') { return true; } else if (boolValue.toLowerCase() === 'false') { return false; } return undefined; } } exports.ActivityFactory = ActivityFactory; ActivityFactory.lgType = 'lgType'; ActivityFactory.adaptiveCardType = cardFactory_1.CardFactory.contentTypes.adaptiveCard; ActivityFactory.genericCardTypeMapping = new Map([ ['herocard', cardFactory_1.CardFactory.contentTypes.heroCard], ['thumbnailcard', cardFactory_1.CardFactory.contentTypes.thumbnailCard], ['audiocard', cardFactory_1.CardFactory.contentTypes.audioCard], ['videocard', cardFactory_1.CardFactory.contentTypes.videoCard], ['animationcard', cardFactory_1.CardFactory.contentTypes.animationCard], ['signincard', cardFactory_1.CardFactory.contentTypes.signinCard], ['oauthcard', cardFactory_1.CardFactory.contentTypes.oauthCard], ['receiptcard', cardFactory_1.CardFactory.contentTypes.receiptCard], ]); ActivityFactory.activityProperties = [ 'type', 'id', 'timestamp', 'localTimestamp', 'localTimezone', 'callerId', 'serviceUrl', 'channelId', 'from', 'conversation', 'recipient', 'textFormat', 'attachmentLayout', 'membersAdded', 'membersRemoved', 'reactionsAdded', 'reactionsRemoved', 'topicName', 'historyDisclosed', 'locale', 'text', 'speak', 'inputHint', 'summary', 'suggestedActions', 'attachments', 'entities', 'channelData', 'action', 'replyToId', 'label', 'valueType', 'value', 'name', 'typrelatesToe', 'code', 'expiration', 'importance', 'deliveryMode', 'listenFor', 'textHighlights', 'semanticAction', ]; ActivityFactory.cardActionProperties = [ 'type', 'title', 'image', 'text', 'displayText', 'value', 'channelData', ]; ActivityFactory.attachmentProperties = [ 'contentType', 'contentUrl', 'content', 'name', 'thumbnailUrl', ]; ActivityFactory.cardProperties = [ 'title', 'subtitle', 'text', 'images', 'image', 'buttons', 'tap', 'media', 'shareable', 'autoloop', 'autostart', 'aspect', 'duration', 'value', 'connectionName', 'tokenExchangeResource', 'facts', 'items', 'total', 'tax', 'vat', ]; //# sourceMappingURL=activityFactory.js.map