diff --git a/README.md b/README.md index e389819e..a8aebb9b 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ Node-RED Watson Nodes for IBM Bluemix CLA assistant ### New in version 0.4.14 +- The dialog for the Test to Speech service now loads available voices dynamically. This allows +new voices and languages to be identified without requiring a further code change. ### New in version 0.4.13 - Emergency fix for Watson Language Translation node. Bluemix credentials read too late. diff --git a/services/text_to_speech/v1.html b/services/text_to_speech/v1.html index 4e465b7c..bc5bce71 100644 --- a/services/text_to_speech/v1.html +++ b/services/text_to_speech/v1.html @@ -20,6 +20,11 @@ Please wait: Checking for bound service credentials... + +
+ +
+
@@ -35,28 +40,19 @@
+
+ +
+ +
+
+
@@ -71,14 +67,7 @@ diff --git a/services/text_to_speech/v1.js b/services/text_to_speech/v1.js index 2fdc0503..7ded73d0 100644 --- a/services/text_to_speech/v1.js +++ b/services/text_to_speech/v1.js @@ -16,20 +16,53 @@ module.exports = function(RED) { var cfenv = require('cfenv'); + var watson = require('watson-developer-cloud'); - var username, password; + // Require the Cloud Foundry Module to pull credentials from bound service + // If they are found then the username and password will be stored in + // the variables sUsername and sPassword. + // + // This separation between sUsername and username is to allow + // the end user to modify the credentials when the service is not bound. + // Otherwise, once set credentials are never reset, resulting in a frustrated + // user who, when he errenously enters bad credentials, can't figure out why + // the edited ones are not being taken. + + var username, password, sUsername, sPassword; var service = cfenv.getAppEnv().getServiceCreds(/text to speech/i) if (service) { - username = service.username; - password = service.password; + sUsername = service.username; + sPassword = service.password; } + // Node RED Admin - fetch and set vcap services RED.httpAdmin.get('/watson-text-to-speech/vcap', function(req, res) { res.json(service ? {bound_service: true} : null); }); + // API used by widget to fetch available models + RED.httpAdmin.get('/watson-text-to-speech/voices', function (req, res) { + var tts = watson.text_to_speech({ + username: sUsername ? sUsername : req.query.un, + password: sPassword ? sPassword : req.query.pwd, + version: 'v1', + url: 'https://stream.watsonplatform.net/text-to-speech/api' + }); + + tts.voices({}, function(err, voices){ + if (err) { + if (!err.error) { + err.error = 'Error ' + err.code + ' in fetching voices'; + } + res.json(err); + } else { + res.json(voices); + } + }); + }); + function Node(config) { RED.nodes.createNode(this, config); var node = this; @@ -41,8 +74,8 @@ module.exports = function(RED) { return; } - username = username || this.credentials.username; - password = password || this.credentials.password; + username = sUsername || this.credentials.username; + password = sPassword || this.credentials.password || config.password; if (!username || !password) { var message = 'Missing Speech To Text service credentials'; @@ -50,8 +83,6 @@ module.exports = function(RED) { return; } - var watson = require('watson-developer-cloud'); - var text_to_speech = watson.text_to_speech({ username: username, password: password,