Skip to content

Commit f0ef68a

Browse files
Merge pull request #123 from Naktibalda/feature/dynamic-workspace-id
Set workspace_id dynamically
2 parents 017aadd + b8379e7 commit f0ef68a

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,30 @@ middleware.before = function(message, conversationPayload, callback) {
362362
}
363363
```
364364

365+
#### Dynamic workspace
366+
367+
If you need to make use of multiple workspaces in a single bot, workspace_id can be changed dynamically by setting workspace_id property in context.
368+
369+
Example of setting workspace_id to id provided as a property of hello message:
370+
```js
371+
function handleHelloEvent(bot, message) {
372+
message.type = 'welcome';
373+
var contextDelta = {};
374+
375+
if (message.workspaceId) {
376+
contextDelta.workspace_id = message.workspaceId;
377+
}
378+
379+
watsonMiddleware.sendToWatsonAsync(bot, message, contextDelta).catch(function (error) {
380+
message.watsonError = error;
381+
}).then(function () {
382+
bot.reply(message, message.watsonData.output.text.join('\n'));
383+
});
384+
}
385+
386+
controller.on('hello', handleHelloEvent);
387+
```
388+
365389
## License
366390

367391
This library is licensed under Apache 2.0. Full license text is available in [LICENSE](LICENSE).

lib/middleware/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ module.exports = function(config) {
108108
payload.context = deepMerge(payload.context, contextDelta);
109109
}
110110
}
111+
if (payload.context && payload.context.workspace_id && payload.context.workspace_id.length === 36) {
112+
payload.workspace_id = payload.context.workspace_id;
113+
}
111114
return payload;
112115
}).then(function(payload) {
113116
return before(message, payload);

test/test.middleware_send_to_watson.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,66 @@ describe('sendToWatson()', function() {
218218
});
219219
});
220220
});
221+
222+
it('should make request to different workspace, if workspace_id is set in context', function (done) {
223+
var newWorkspaceId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
224+
225+
var expectedPath = '/v1/workspaces/' + newWorkspaceId + '/message';
226+
227+
var storedContext = {
228+
workspace_id: newWorkspaceId
229+
};
230+
231+
var expectedContextInResponse = clone(storedContext);
232+
expectedContextInResponse.conversation_id = "8a79f4db-382c-4d56-bb88-1b320edf9eae",
233+
expectedContextInResponse.system = {
234+
dialog_stack: [
235+
"root"
236+
],
237+
dialog_turn_counter: 1,
238+
dialog_request_counter: 1
239+
};
240+
241+
var expectedRequest = {
242+
input: {
243+
text: message.text
244+
},
245+
context: storedContext
246+
};
247+
248+
var mockedWatsonResponse = {
249+
"intents": [],
250+
"entities": [],
251+
"input": {
252+
"text": "hi"
253+
},
254+
"output": {
255+
"log_messages": [],
256+
"text": [
257+
"Hello from Watson Conversation!"
258+
],
259+
"nodes_visited": [
260+
"node_1_1467221909631"
261+
]
262+
},
263+
"context": expectedContextInResponse
264+
};
265+
266+
//verify request and return mocked response
267+
var mockedRequest = nock(service.url)
268+
.post(expectedPath + '?version=' + service.version_date, expectedRequest)
269+
.reply(200, mockedWatsonResponse);
270+
271+
utils.updateContext(message.user, bot.botkit.storage, {context: storedContext}, function(err) {
272+
assert.ifError(err);
273+
274+
middleware.sendToWatson(bot, message, function(err, response) {
275+
assert.ifError(err);
276+
assert.ifError(message.watsonError);
277+
278+
mockedRequest.done();
279+
done();
280+
});
281+
});
282+
})
221283
});

0 commit comments

Comments
 (0)