forked from Watson-Personal-Assistant/SkillBoilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.js
101 lines (83 loc) · 2.46 KB
/
actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
© Copyright IBM Corp. 2017
*/
'use strict';
// The expertise handler
const {handler} = require('./expertise-sdk');
const Promise = require('bluebird');
const Conversation = require('watson-developer-cloud/conversation/v1');
// Expertise translations map
const languageResource = {
'en-US': {
'translation': {
'HELLO_WORLD': 'Hello world',
'TRY_AGAIN': 'Sorry, please try again later'
}
},
'de-DE': {
'translation': {
'HELLO_WORLD': 'Hallo Welt',
'TRY_AGAIN': 'Sorry, bitte versuchen Sie es später noch einmal'
}
}
};
// Expertise states
const STATES = {
};
let conversation;
try {
conversation = Promise.promisifyAll(
new Conversation({
url: 'your wcs url',
username: 'your wcs username',
password: 'your wcs password',
version_date: Conversation.VERSION_DATE_2017_04_21
})
);
} catch (err) {
console.error('Conversation service failure or missing credentials.');
console.log(err);
process.exit(0);
}
// Helper function
function converse(request) {
console.log(handler.attributes.context);
// Start conversation, empty context
const payload = {
workspace_id: ' your expertise workspace id',
context: handler.attributes.context,
input: {text: request.retext}
};
// Send the input to the conversation service
return conversation.messageAsync(payload);
}
function processIntent(request, response) {
converse(request).then(result => {
let endSession = false;
if(result.output.end_convo !== null) {
endSession = result.output.end_convo;
}
console.log(JSON.stringify(result));
// Save the context of the watson conversation service
handler.attributes.context = result.context;
response.say(result.output.text, 'random').shouldEndSession(endSession).send();
}).catch(err => {
handler.attributes.context = {};
response.say(handler.t('TRY_AGAIN'));
});
};
// Actions for DEFAULT state
const stateDefaultActions = handler.createActionsHandler({
'hello-world': (request, response) => {
response.say(handler.t('HELLO_WORLD')).send();
},
'unhandled': (request, response) => {
response.say(handler.t('TRY_AGAIN')).send();
}
});
module.exports = () => {
// Register language translations.
handler.registerLanguages(languageResource);
// Register state actions
handler.registerActionsHandler(stateDefaultActions);
};