Skip to content

Commit e55a23c

Browse files
authored
Merge pull request #388 from kenime/master
Added Timeout and Optout learning for Conversation node
2 parents 32f72bf + 98dc5b3 commit e55a23c

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

services/conversation/v1.html

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
<label for="node-input-workspaceid"><i class="fa fa-tag"></i> Workspace ID</label>
5050
<input type="text" id="node-input-workspaceid" placeholder="WorkspaceID">
5151
</div>
52+
<div class="form-row">
53+
<label for="node-input-timeout"><i class="fa fa-tag"></i> Timeout Period</label>
54+
<input type="text" id="node-input-timeout" placeholder="Leave empty to disable">
55+
</div>
5256
<div class="form-row">
5357
<label>&nbsp;</label>
5458
<input type="checkbox" id="node-input-context" style="display: inline-block; width: auto; vertical-align: top;">
@@ -62,7 +66,12 @@
6266
<div class="form-row">
6367
<label>&nbsp;</label>
6468
<input type="checkbox" id="node-input-empty-payload" style="display: inline-block; width: auto; vertical-align: top;">
65-
<label for="node-input-empty-payload" style="width: 70%;"> Permit empty payload</label>
69+
<label for="node-input-empty-payload" style="width: 70%;"> Permit Empty Payload</label>
70+
</div>
71+
<div class="form-row">
72+
<label>&nbsp;</label>
73+
<input type="checkbox" id="node-input-optout-learning" style="display: inline-block; width: auto; vertical-align: top;">
74+
<label for="node-input-optout-learning" style="width: 70%;"> Opt Out Request Logging</label>
6675
</div>
6776

6877
<div class="form-tips" id="conversation-form-tips">
@@ -81,6 +90,7 @@
8190
<li><code>msg.payload</code> : the message of the conversation to analyse. Format: String </li>
8291
<li><code>msg.user</code> (optional): unique identifier of the user. This will be used to maintain the context of the conversation for each user when using with multiple users. Format: String </li>
8392
<li><code>msg.params.workspace_id</code> : unique identifier of the workspace to be used. Could be also configured in the node. Format: String </li>
93+
<li><code>msg.params.timeout</code> (optional): The timeout period (in millisecond) for Watson request. Leave empty or set to 0 to disable. </li>
8494
<li><code>msg.params.context</code> (optional): A context object that includes state information for the conversation. When you send multiple requests for the same conversation, include the context object from the response. (<a href="http://www.ibm.com/watson/developercloud/conversation/api/v1/#send_input" target="_blank">documentation</a>). This will overwrite any context saved in the node. Format: JSON </li>
8595
<li><code>msg.params.alternate_intents</code> (optional) : whether to return more than one intent. Default is false. Set to true to return all matching intents. For example, return all intents when the confidence is not high to allow users to choose their intent.</li>
8696
<li><code>msg.params.output</code> (optional) : see API documentation </li>
@@ -91,6 +101,7 @@
91101
<li><code>msg.params.username</code> : If provided will be used as the username credential for the Conversation service.</li>
92102
<li><code>msg.params.password</code> : If provided will be used as the password credential for the Conversation service.</li>
93103
<li><code>msg.params.endpoint</code> : If provided will be used as the url for the Conversation service.</li>
104+
<li><code>msg.params.optout_learning</code> : Set to true to opt out of request logging. Check the <a href="https://console.bluemix.net/docs/services/watson/getting-started-logging.html#controlling-request-logging-for-watson-services" target="_blank">documentation</a> for details.</li>
94105
</ul>
95106
<p>See <a href="http://www.ibm.com/watson/developercloud/conversation/api/v1/#send_input" target="_blank">Conversation API documentation</a> for details.</p>
96107
<p>All Results will made available at <code>msg.payload</code> in JSON format. Check the <a href="http://www.ibm.com/watson/developercloud/conversation/api/v1/#send_input" target="_blank">documentation</a> for details.</p>
@@ -108,7 +119,7 @@
108119
<script type="text/javascript">
109120
var oneditprepare = function() {
110121
$('input#node-input-context').change(function () {
111-
var checked = $('input#node-input-context').prop('checked')
122+
var checked = $('input#node-input-context').prop('checked');
112123
if (checked) {
113124
$('input#node-input-multiuser').parent().show();
114125
$('#conversation-form-tips').show();
@@ -118,7 +129,7 @@
118129
}
119130
});
120131
$('input#node-input-default-endpoint').change(function () {
121-
var checked = $('input#node-input-default-endpoint').prop('checked')
132+
var checked = $('input#node-input-default-endpoint').prop('checked');
122133
if (checked) {
123134
$('#node-input-service-endpoint').parent().hide();
124135
} else {
@@ -147,8 +158,10 @@
147158
multiuser: {value: false},
148159
context: {value: true},
149160
'empty-payload': {value: false},
150-
'default-endpoint' :{value: true},
151-
'service-endpoint' :{value: 'https://gateway.watsonplatform.net/conversation/api'}
161+
'default-endpoint' : {value: true},
162+
'service-endpoint' : {value: 'https://gateway.watsonplatform.net/conversation/api'},
163+
timeout: {value: '', validate: RED.validators.number(true)},
164+
'optout-learning': {value: false}
152165
},
153166
credentials: {
154167
username: {type:'text'},

services/conversation/v1.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ module.exports = function(RED) {
124124
if (msg.params && msg.params.alternate_intents) {
125125
params.alternate_intents = msg.params.alternate_intents;
126126
}
127+
127128
verifyOptionalInputs(node, msg, config, params);
128129
return true;
129130
}
@@ -143,7 +144,8 @@ module.exports = function(RED) {
143144

144145
var userName = sUsername || node.credentials.username,
145146
passWord = sPassword || node.credentials.password,
146-
endpoint = '';
147+
endpoint = '',
148+
optoutLearning = false;
147149

148150
if (!(userName || msg.params.username) ||
149151
!(passWord || msg.params.password)) {
@@ -165,6 +167,9 @@ module.exports = function(RED) {
165167
}
166168
}
167169

170+
serviceSettings.username = userName;
171+
serviceSettings.password = passWord;
172+
168173
if (service) {
169174
endpoint = service.url;
170175
}
@@ -179,15 +184,31 @@ module.exports = function(RED) {
179184
serviceSettings.url = endpoint;
180185
}
181186

182-
serviceSettings.username = userName;
183-
serviceSettings.password = passWord;
187+
if ((msg.params && msg.params['optout_learning'])){
188+
optoutLearning = true;
189+
} else if (config['optout-learning']){
190+
optoutLearning = true;
191+
}
192+
193+
if (optoutLearning){
194+
serviceSettings.headers = serviceSettings.headers || {};
195+
serviceSettings.headers['X-Watson-Learning-Opt-Out'] = '1';
196+
}
197+
198+
if (config['timeout'] && config['timeout'] !== '0' && isFinite(config['timeout'])){
199+
serviceSettings.timeout = parseInt(config['timeout']);
200+
}
201+
202+
if (msg.params && msg.params.timeout !== '0' && isFinite(msg.params.timeout)){
203+
serviceSettings.timeout = parseInt(msg.params.timeout);
204+
}
184205

185206
node.service = new ConversationV1(serviceSettings);
186207
return true;
187208
}
188209

189210
function processResponse(err, body, node, msg, config) {
190-
if (err !== null && body === null) {
211+
if (err !== null && (body === null || typeof (body) === 'undefined')) {
191212
node.error(err, msg);
192213
node.status({
193214
fill: 'red',

0 commit comments

Comments
 (0)