node-red-node-watson
Version:
A collection of Node-RED nodes for IBM Watson services
237 lines (212 loc) • 8.73 kB
HTML
<!--
Copyright 2013,2022 IBM Corp.
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.
-->
<script type="text/x-red" data-template-name="watson-tone-analyzer-v3">
<div id="credentials-check" class="form-row">
<div class="form-tips">
<i class="fa fa-question-circle"></i><b> Please wait: </b> Checking for bound service credentials...
</div>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row credentials" style="display: none;">
<label for="node-input-apikey"><i class="fa fa-key"></i> API Key</label>
<input type="password" id="node-input-apikey" placeholder="API Key"></input>
</div>
<div class="form-row credentials">
<label for="node-input-service-endpoint"><i class="fa fa-tag"></i> Service Endpoint</label>
<input type="text" id="node-input-service-endpoint" placeholder="https://gateway.watsonplatform.net/tone-analyzer/api">
</div>
<div class="form-row">
<label for="node-input-tone-method"><i class="fa fa-book"></i> Method: </label>
<select type="text" id="node-input-tone-method" style="display: inline-block; width: 70%;">
<option value="generalTone">General Tone</option>
<option value="customerEngagementTone">Customer Engagement Tone</option>
</select>
</div>
<div class="form-row">
<label for="node-input-interface-version"><i class="fa fa-history"></i> version_date: </label>
<select type="text" id="node-input-interface-version" style="display: inline-block; width: 70%;">
<option value="2016-05-19">Multiple Tones</option>
<option value="2017-09-21">Dominant Tone</option>
</select>
</div>
<div class="form-row">
<label for="node-input-tones"><i class="fa fa-comments-o"></i> Tones</label>
<select type="text" id="node-input-tones" style="display: inline-block; width: 70%;" >
<option value="all">All</option>
<option value="emotion">Emotion</option>
<option value="language">Language</option>
<option value="social">Social</option>
</select>
</div>
<div class="form-row">
<label for="node-input-sentences"><i class="fa fa-language"></i> Sentences</label>
<select type="text" id="node-input-sentences" style="display: inline-block; width: 70%;" >
<option value="true">True</option>
<option value="false">False</option>
</select>
</div>
<div class="form-row">
<label for="node-input-contentType"><i class="fa fa-comments-o"></i> Content type</label>
<select type="text" id="node-input-contentType" style="display: inline-block; width: 70%;" >
<option value="false">Text</option>
<option value="true">HTML</option>
</select>
</div>
<div class="form-row">
<label for="node-input-inputlang"><i class="fa fa-language"></i> Input Text Language</label>
<select type="text" id="node-input-inputlang" style="display: inline-block; width: 70%;" >
<option value="en">English</option>
<option value="fr">French</option>
</select>
</div>
</script>
<script type="text/x-red" data-help-name="watson-tone-analyzer-v3">
<p>The Tone Analyzer service uses linguistic analysis to detect emotional tones, social propensities,
and writing styles in written communication.</p>
<p>The text to analyze should be passed in on <b>msg.payload</b>.</p>
<p>The service response will be returned on <b>msg.response</b>.</p>
<p>The tone and sentances can be programmaticaly set in <code>msg.tones</code>
and <code>msg.sentences</code></p>
<p>Using the node editor dialog users can filter the results by tone (emotion, language or social) and
whether to include sentence-level analysis.</p>
<p>When running the Conversational Chat Tone, the input needs to follow the
chat json format for utterences. </p>
<code>
msg.payload = [
{
text: "Hello, I'm having a problem with your product.",
user: "customer",
},
{
text: "OK, let me know what's going on, please.",
user: "agent",
},
{
text: "Well, nothing is working :(",
user: "customer",
}
];
</code>
<p>For more information about the Tone Analyzer service, read the <a href="https://www.ibm.com/watson/services/tone-analyzer/">documentation</a>.</p>
</script>
<script type="text/javascript">
// Need to simulate a namespace, so that some of the variables don't leak across nodes
function ToneAnalyzerV3 () {}
// This is the namespace for this version of this Node.
var ToneV3 = new ToneAnalyzerV3();
ToneV3.showSelectedFields = function(fields) {
for (i = 0; i < fields.length; i++) {
$(fields[i]).parent().show();
}
}
ToneV3.hideSelectedFields = function(fields) {
for (i = 0; i < fields.length; i++) {
$(fields[i]).parent().hide();
}
}
ToneV3.hideAll = function() {
fields = [];
fields.push('#node-input-tones');
fields.push('#node-input-sentences');
fields.push('#node-input-contentType');
fields.push('#node-input-inputlang');
ToneV3.hideSelectedFields(fields);
};
ToneV3.processSelectedMethod = function(method) {
ToneV3.hideAll();
fields = [];
switch (method) {
case 'generalTone':
fields.push('#node-input-tones');
fields.push('#node-input-sentences');
fields.push('#node-input-contentType');
fields.push('#node-input-inputlang');
break;
}
ToneV3.showSelectedFields(fields);
}
ToneV3.processSelectedVersion = function(method, version) {
if ('generalTone' == method)
{
fields = [];
fields.push('#node-input-tones');
if ('2016-05-19' == version) {
ToneV3.showSelectedFields(fields);
} else {
ToneV3.hideSelectedFields(fields);
}
}
}
ToneV3.UIListeners = function () {
$('#node-input-tone-method').change(function(val){
var method = $('#node-input-tone-method').val();
ToneV3.processSelectedMethod(method);
});
$('#node-input-interface-version').change(function(val){
var version = $('#node-input-interface-version').val();
var method = $('#node-input-tone-method').val();
ToneV3.processSelectedVersion(method, version);
});
}
ToneV3.checkForPrepare = function () {
ToneV3.hideAll();
ToneV3.UIListeners();
};
// This is the on edit prepare function, which will be invoked everytime the dialog is shown.
function oneditprepare() {
ToneV3.checkForPrepare();
$.getJSON('watson-tone-analyzer/vcap/')
.done(function (service) {
$('.credentials').toggle(!service);
})
.fail(function () {
$('.credentials').show();
}).always(function () {
$('#credentials-check').hide();
})
}
(function() {
RED.nodes.registerType('watson-tone-analyzer-v3', {
category: 'IBM Watson',
defaults: {
name: {value: ""},
tones: {value: "all"},
sentences: {value: "true"},
contentType: {value: "false"},
"tone-method": {value:"generalTone"},
"interface-version": {value:"generalTone"},
name: {value: ""},
inputlang: {value: "en"},
'service-endpoint' :{value: ""}
},
credentials: {
apikey: {type: 'password'}
},
color: "rgb(84,149,230)",
inputs: 1,
outputs: 1,
icon: "tone_analyzer.png",
paletteLabel: "tone analyzer v3",
label: function() {
return this.name || "tone analyzer v3";
},
labelStyle: function() {
return this.name ? "node_label_italic" : "";
},
oneditprepare: oneditprepare
});
})();
</script>