From a6f7365769de35d8cd1fa75a6aeca753f5ddca98 Mon Sep 17 00:00:00 2001 From: chughts Date: Mon, 5 Jun 2017 20:04:00 +0100 Subject: [PATCH 01/20] Allow endpoint to be specified in Conversation Node --- services/conversation/v1.html | 49 ++++++++++++++++++++++++++--------- services/conversation/v1.js | 42 +++++++++++++++++++++--------- 2 files changed, 67 insertions(+), 24 deletions(-) diff --git a/services/conversation/v1.html b/services/conversation/v1.html index 1bc8c353..8867b43a 100644 --- a/services/conversation/v1.html +++ b/services/conversation/v1.html @@ -1,4 +1,4 @@ - diff --git a/services/language_translator_identify/v2.js b/services/language_translator_identify/v2.js index 13b9b317..4ef867ba 100644 --- a/services/language_translator_identify/v2.js +++ b/services/language_translator_identify/v2.js @@ -26,11 +26,13 @@ module.exports = function (RED) { password = null, sUsername = null, sPassword = null, - endpointUrl = 'https://gateway.watsonplatform.net/language-translator/api'; + endpoint = '', sEndpoint; + //endpointUrl = 'https://gateway.watsonplatform.net/language-translator/api'; if (service) { sUsername = service.username; sPassword = service.password; + sEndpoint = service.url; } RED.httpAdmin.get('/watson-language-translator-identify/vcap', function (req, res) { @@ -38,7 +40,7 @@ module.exports = function (RED) { }); function Node (config) { - var node = this; + var node = this, serviceSettings = {}; RED.nodes.createNode(this, config); this.on('input', function (msg) { @@ -57,15 +59,26 @@ module.exports = function (RED) { return; } - var language_translator = new LanguageTranslatorV2({ + endpoint = sEndpoint; + if ((!config['default-endpoint']) && config['service-endpoint']) { + endpoint = config['service-endpoint']; + } + + serviceSettings = { username: username, password: password, version: 'v2', - url: endpointUrl, + //url: endpointUrl, headers: { 'User-Agent': pkg.name + '-' + pkg.version } - }); + }; + + if (endpoint) { + serviceSettings.url = endpoint; + } + + var language_translator = new LanguageTranslatorV2(serviceSettings); node.status({fill:'blue', shape:'dot', text:'requesting'}); language_translator.identify({text: msg.payload}, function (err, response) { From 72097834ea7e38e214f6b1f6c43400f9df73e6f0 Mon Sep 17 00:00:00 2001 From: chughts Date: Tue, 6 Jun 2017 08:48:02 +0100 Subject: [PATCH 06/20] Migrate Language Indentifier Node to promises --- services/language_translator_identify/v2.js | 93 +++++++++++++-------- 1 file changed, 57 insertions(+), 36 deletions(-) diff --git a/services/language_translator_identify/v2.js b/services/language_translator_identify/v2.js index 4ef867ba..000ce0ff 100644 --- a/services/language_translator_identify/v2.js +++ b/services/language_translator_identify/v2.js @@ -18,9 +18,8 @@ module.exports = function (RED) { const SERVICE_IDENTIFIER = 'language-translator'; var pkg = require('../../package.json'), LanguageTranslatorV2 = require('watson-developer-cloud/language-translator/v2'), - //cfenv = require('cfenv'), + payloadutils = require('../../utilities/payload-utils'), serviceutils = require('../../utilities/service-utils'), - //service = cfenv.getAppEnv().getServiceCreds(/language translator/i), service = serviceutils.getServiceCreds(SERVICE_IDENTIFIER), username = null, password = null, @@ -35,36 +34,23 @@ module.exports = function (RED) { sEndpoint = service.url; } - RED.httpAdmin.get('/watson-language-translator-identify/vcap', function (req, res) { - res.json(service ? {bound_service: true} : null); - }); - - function Node (config) { - var node = this, serviceSettings = {}; - RED.nodes.createNode(this, config); - - this.on('input', function (msg) { - if (!msg.payload) { - var message = 'Missing property: msg.payload'; - node.error(message, msg); - return; - } - - username = sUsername || this.credentials.username; - password = sPassword || this.credentials.password; - - if (!username || !password) { - var message = 'Missing Watson Language Translator service credentials'; - node.error(message, msg); - return; - } + function initialCheck(u, p) { + if (!u || !p) { + return Promise.reject('Missing Watson Language Translator service credentials'); + } + return Promise.resolve(); + } - endpoint = sEndpoint; - if ((!config['default-endpoint']) && config['service-endpoint']) { - endpoint = config['service-endpoint']; - } + function payloadCheck(msg) { + if (!msg.payload) { + return Promise.reject('Missing property: msg.payload'); + } + return Promise.resolve(); + } - serviceSettings = { + function execute(node, msg) { + var p = new Promise(function resolver(resolve, reject){ + var serviceSettings = { username: username, password: password, version: 'v2', @@ -72,25 +58,60 @@ module.exports = function (RED) { headers: { 'User-Agent': pkg.name + '-' + pkg.version } - }; - + };; if (endpoint) { serviceSettings.url = endpoint; } var language_translator = new LanguageTranslatorV2(serviceSettings); - node.status({fill:'blue', shape:'dot', text:'requesting'}); language_translator.identify({text: msg.payload}, function (err, response) { - node.status({}) if (err) { - node.error(err, msg); + reject(err); } else { msg.languages = response.languages msg.lang = response.languages[0]; + resolve(); } - node.send(msg); }); + + }); + return p; + } + + RED.httpAdmin.get('/watson-language-translator-identify/vcap', function (req, res) { + res.json(service ? {bound_service: true} : null); + }); + + function Node (config) { + var node = this; + RED.nodes.createNode(this, config); + + this.on('input', function (msg) { + username = sUsername || this.credentials.username; + password = sPassword || this.credentials.password; + + endpoint = sEndpoint; + if ((!config['default-endpoint']) && config['service-endpoint']) { + endpoint = config['service-endpoint']; + } + + node.status({}); + initialCheck(username, password) + .then(function(){ + return payloadCheck(msg); + }) + .then(function(){ + node.status({fill:'blue', shape:'dot', text:'requesting'}); + return execute(node, msg); + }) + .then(function(){ + node.status({}); + node.send(msg); + }) + .catch(function(err){ + payloadutils.reportError(node,msg,err); + }); }); } RED.nodes.registerType('watson-language-translator-identify', Node, { From 9b2f2c1ebd602218ae2d372f104c2d6bbe734ea4 Mon Sep 17 00:00:00 2001 From: chughts Date: Tue, 6 Jun 2017 10:09:55 +0100 Subject: [PATCH 07/20] Allow Endpoint to be specified in Language Translator Node --- README.md | 2 +- services/language_translator/v2.html | 27 +++++++++++++++-- services/language_translator/v2.js | 32 +++++++++++++++------ services/language_translator_identify/v2.js | 2 +- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1947e888..3ae6533a 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Node-RED Watson Nodes for IBM Bluemix ### New in version 0.5.9 - Endpoint can now be specified in Conversation, -Conversation Workspace Manager and Language Identify nodes. +Conversation Workspace Manager Language Identify and Language Translation nodes. ### New in version 0.5.8 - Visual Reconition Node, now accepts readstream on msg.payload diff --git a/services/language_translator/v2.html b/services/language_translator/v2.html index d045630b..2cbd70f4 100644 --- a/services/language_translator/v2.html +++ b/services/language_translator/v2.html @@ -39,6 +39,16 @@ +
+ + + +
+
+ + +
+
+ +
+ + + +
+
+ + +
+ @@ -485,6 +490,7 @@ band: {value: ''}, bandhidden: {value: ''}, password: {value: ''}, + 'payload-response' :{value: false}, 'default-endpoint' :{value: true}, 'service-endpoint' :{value: 'https://stream.watsonplatform.net/speech-to-text/api'} }, diff --git a/services/speech_to_text/v1.js b/services/speech_to_text/v1.js index 36c79ad2..2b9b81ef 100644 --- a/services/speech_to_text/v1.js +++ b/services/speech_to_text/v1.js @@ -290,6 +290,9 @@ module.exports = function (RED) { }); }); } + if (config['payload-response']) { + msg.payload = msg.transcription; + } return Promise.resolve(); } diff --git a/services/text_to_speech/v1.html b/services/text_to_speech/v1.html index 15e7acaf..1435ad11 100644 --- a/services/text_to_speech/v1.html +++ b/services/text_to_speech/v1.html @@ -84,6 +84,12 @@ +
+ + + +
+