|
| 1 | +/** |
| 2 | + * Copyright 2015 IBM Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + **/ |
| 16 | + |
| 17 | +module.exports = function (RED) { |
| 18 | + var PersonalityInsightsV3 = require('watson-developer-cloud/personality-insights/v3'), |
| 19 | + cfenv = require('cfenv'), |
| 20 | + payloadutils = require('../../utilities/payload-utils'), |
| 21 | + service = cfenv.getAppEnv().getServiceCreds(/personality insights/i), |
| 22 | + username = null, |
| 23 | + password = null, |
| 24 | + sUsername = null, |
| 25 | + sPassword = null, |
| 26 | + |
| 27 | + VALID_INPUT_LANGUAGES = ['ar','en','es','ja'], |
| 28 | + VALID_RESPONSE_LANGUAGES = ['ar','de','en','es','fr','it','ja','ko','pt-br','zh-cn','zh-tw']; |
| 29 | + |
| 30 | + if (service) { |
| 31 | + sUsername = service.username; |
| 32 | + sPassword = service.password; |
| 33 | + } |
| 34 | + |
| 35 | + // This HTTP GET REST request is used by the browser side of the node to |
| 36 | + // determine if credentials are found. |
| 37 | + RED.httpAdmin.get('/watson-personality-insights-v3/vcap', function (req, res) { |
| 38 | + res.json(service ? {bound_service: true} : null); |
| 39 | + }); |
| 40 | + |
| 41 | + // This function prepares the params object for the |
| 42 | + // call to Personality Insights |
| 43 | + function prepareParams(msg, config) { |
| 44 | + var params = {}, |
| 45 | + inputlang = config.inputlang ? config.inputlang : 'en', |
| 46 | + outputlang = config.outputlang ? config.outputlang : 'en'; |
| 47 | + |
| 48 | + if (msg.piparams) { |
| 49 | + if (msg.piparams.inputlanguage && |
| 50 | + -1 < VALID_INPUT_LANGUAGES.indexOf(msg.piparams.inputlanguage)) { |
| 51 | + inputlang = msg.piparams.inputlanguage; |
| 52 | + } |
| 53 | + if (msg.piparams.responselanguage && |
| 54 | + -1 < VALID_RESPONSE_LANGUAGES.indexOf(msg.piparams.responselanguage)) { |
| 55 | + outputlang = msg.piparams.responselanguage; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + params = { |
| 60 | + text: msg.payload, |
| 61 | + consumption_preferences: config.consumption ? config.consumption : false, |
| 62 | + raw_scores: config.rawscores ? config.rawscores : false, |
| 63 | + headers: { |
| 64 | + 'content-language': inputlang, |
| 65 | + 'accept-language': outputlang, |
| 66 | + 'accept': 'application/json' |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + return params; |
| 71 | + } |
| 72 | + |
| 73 | + // This is the start of the Node Code. In this case only on input |
| 74 | + // is being processed. |
| 75 | + function Node(config) { |
| 76 | + RED.nodes.createNode(this,config); |
| 77 | + var node = this, |
| 78 | + wc = payloadutils.word_count(config.inputlang), |
| 79 | + message = ''; |
| 80 | + |
| 81 | + this.on('input', function (msg) { |
| 82 | + //var self = this; |
| 83 | + |
| 84 | + if (!msg.payload) { |
| 85 | + message = 'Missing property: msg.payload'; |
| 86 | + node.status({fill:'red', shape:'ring', text:'missing payload'}); |
| 87 | + node.error(message, msg); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + if ('string' !== typeof(msg.payload)) { |
| 92 | + message = 'submitted msg.payload is not text'; |
| 93 | + node.status({fill:'red', shape:'ring', text:'payload is not text'}); |
| 94 | + node.error(message, msg); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + wc(msg.payload, function (length) { |
| 99 | + if (length < 100) { |
| 100 | + message = 'Personality insights requires a minimum of one hundred words.' + |
| 101 | + ' Only ' + length + ' submitted'; |
| 102 | + node.status({fill:'red', shape:'ring', text:'insufficient words submitted'}); |
| 103 | + node.error(message, msg); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + username = sUsername || node.credentials.username; |
| 108 | + password = sPassword || node.credentials.password; |
| 109 | + |
| 110 | + if (!username || !password) { |
| 111 | + message = 'Missing Personality Insights service credentials'; |
| 112 | + node.status({fill:'red', shape:'ring', text:'missing credentials'}); |
| 113 | + node.error(message, msg); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + var params = prepareParams(msg, config), |
| 118 | + personality_insights = new PersonalityInsightsV3({ |
| 119 | + username: username, |
| 120 | + password: password, |
| 121 | + version_date: '2016-10-20' |
| 122 | + }); |
| 123 | + |
| 124 | + node.status({fill:'blue', shape:'dot', text:'requesting'}); |
| 125 | + personality_insights.profile(params, function(err, response){ |
| 126 | + node.status({}); |
| 127 | + if (err) { |
| 128 | + node.status({fill:'red', shape:'ring', text:'processing error'}); |
| 129 | + node.error(err, msg); |
| 130 | + } else{ |
| 131 | + msg.insights = response; |
| 132 | + } |
| 133 | + |
| 134 | + node.send(msg); |
| 135 | + }); |
| 136 | + |
| 137 | + }); |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + RED.nodes.registerType('watson-personality-insights-v3',Node,{ |
| 142 | + credentials: { |
| 143 | + username: {type:'text'}, |
| 144 | + password: {type:'password'} |
| 145 | + } |
| 146 | + }); |
| 147 | +}; |
0 commit comments