UNPKG

recime-bot-runtime

Version:

This runtime is intended to run inside a micro-service container with platform specific integration and module interpreter.

79 lines (78 loc) 3.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var request = require("request"); var DefaultBlock = 'default'; var NaturalLanguageProviderClient = /** @class */ (function () { function NaturalLanguageProviderClient() { } NaturalLanguageProviderClient.request = function (context) { var args = context.args; var env = context.process.env; var body = this.processBody(args); console.log('nlp', body); return new Promise(function (resolve, reject) { request({ uri: "https://api.recime.io/v1/bots/" + env.UID + "/nlp/requests", method: 'POST', json: true, body: body, headers: { 'authorization': "Token " + env.SYSTEM_RECIME_API_KEY, 'user-agent': 'recime.io/1.0 bot' } }, function (error, response, result) { if (error) { return reject(error); } if (response.statusCode < 200 || response.statusCode > 299) { var httpError = new Error((result.length && result[0].message) || result.message || response.statusMessage); return reject(httpError); } return resolve(result); }); }); }; NaturalLanguageProviderClient.processBody = function (args) { if (args.event && args.event.name) { return { event: args.event.name }; } var rawBody = args.rawBody || {}; if (process.env['SYSTEM_RECIME_NLP'] === 'wit') { if (rawBody.message && rawBody.message.nlp) { var confidenceThreshold_1 = 0.8; var nlp = rawBody.message.nlp; var bestMatch = void 0; if (nlp.entities && nlp.entities["intent"]) { bestMatch = nlp.entities.intent .filter(function (x) { return x.confidence > confidenceThreshold_1; }) .sort(function (x, y) { if (x.confidence > y.confidence) { return 1; } if (x.confidence < y.confidence) { return -1; } return 0; }) .map(function (intent) { return intent.value; }); console.log('wit', bestMatch); return { event: bestMatch.length ? bestMatch[0] : DefaultBlock }; } if (!bestMatch) { return { event: DefaultBlock }; } } } return { text: args.text }; }; return NaturalLanguageProviderClient; }()); exports.NaturalLanguageProviderClient = NaturalLanguageProviderClient;