All files index.js

61.64% Statements 45/73
62.22% Branches 28/45
60% Functions 12/20
65.15% Lines 43/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153                                  2x 10x 30x   40x 10x     2x 2x 2x 2x 2x     2x   2x 2x 2x                                   6x     6x     6x 6x 2x   2x   6x                       6x 6x 6x 6x       6x   6x 6x       6x 5x   6x 2x         2x     6x       1x   6x 6x 6x 6x 6x                 4x 4x                                                     2x  
"use strict";
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
 * Copyright 2016-2019 IBM Corp. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const debug = require('debug')('watson-middleware:index');
const AssistantV1 = require("ibm-watson/assistant/v1");
const utils_1 = require("./utils");
const deepMerge = require("deepmerge");
class WatsonMiddleware {
    constructor(config) {
        this.minimumConfidence = 0.75;
        // These are initiated by Slack itself and not from the end-user. Won't send these to WCS.
        this.ignoreType = ['presence_change', 'reconnect_url'];
        this.config = config;
        Iif (config.minimum_confidence) {
            this.minimumConfidence = config.minimum_confidence;
        }
    }
    hear(patterns, message) {
        if (message.watsonData && message.watsonData.intents) {
            for (let p = 0; p < patterns.length; p++) {
                for (let i = 0; i < message.watsonData.intents.length; i++) {
                    if (message.watsonData.intents[i].intent === patterns[p] &&
                        message.watsonData.intents[i].confidence >= this.minimumConfidence) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    before(message, payload) {
        return Promise.resolve(payload);
    }
    after(message, response) {
        return Promise.resolve(response);
    }
    sendToWatson(bot, message, contextDelta) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.conversation) {
                debug('Creating Assistant object with parameters: ' +
                    JSON.stringify(this.config, null, 2));
                this.conversation = new AssistantV1(this.config);
            }
            Iif ((!message.text && message.type !== 'welcome') ||
                this.ignoreType.indexOf(message.type) !== -1 ||
                message.reply_to ||
                message.bot_id) {
                // Ignore messages initiated by Slack. Reply with dummy output object
                message.watsonData = {
                    output: {
                        text: [],
                    },
                };
                return;
            }
            this.storage = bot.controller.storage;
            try {
                const userContext = yield utils_1.readContext(message.user, this.storage);
                const payload = {
                    // eslint-disable-next-line @typescript-eslint/camelcase
                    workspace_id: this.config.workspace_id,
                };
                Eif (message.text) {
                    // text can not contain the following characters: tab, new line, carriage return.
                    const sanitizedText = message.text.replace(/[\r\n\t]/g, ' ');
                    payload.input = {
                        text: sanitizedText,
                    };
                }
                if (userContext) {
                    payload.context = userContext;
                }
                if (contextDelta) {
                    Iif (!userContext) {
                        //nothing to merge, this is the first context
                        payload.context = contextDelta;
                    }
                    else {
                        payload.context = deepMerge(payload.context, contextDelta);
                    }
                }
                if (payload.context &&
                    payload.context.workspace_id &&
                    payload.context.workspace_id.length === 36) {
                    // eslint-disable-next-line @typescript-eslint/camelcase
                    payload.workspace_id = payload.context.workspace_id;
                }
                const watsonRequest = yield this.before(message, payload);
                let watsonResponse = yield utils_1.postMessage(this.conversation, watsonRequest);
                watsonResponse = yield this.after(message, watsonResponse);
                message.watsonData = watsonResponse;
                yield utils_1.updateContext(message.user, this.storage, watsonResponse);
            }
            catch (error) {
                message.watsonError = error;
                debug('Error: %s', JSON.stringify(error, Object.getOwnPropertyNames(error), 2));
            }
        });
    }
    receive(bot, message) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.sendToWatson(bot, message, null);
        });
    }
    interpret(bot, message) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.sendToWatson(bot, message, null);
        });
    }
    readContext(user) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.storage) {
                throw new Error('readContext is called before the first this.receive call');
            }
            return utils_1.readContext(user, this.storage);
        });
    }
    updateContext(user, contextDelta) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!this.storage) {
                throw new Error('updateContext is called before the first this.receive call');
            }
            return utils_1.updateContext(user, this.storage, {
                context: contextDelta,
            });
        });
    }
}
exports.WatsonMiddleware = WatsonMiddleware;
//# sourceMappingURL=index.js.map