-
Notifications
You must be signed in to change notification settings - Fork 63
/
actions.js
executable file
·121 lines (109 loc) · 4.28 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
© Copyright IBM Corp. 2017
*/
'use strict';
// The expertise handler
const {handler} = require('skill-sdk-nodejs');
const evaluation = 'evaluation';
const entities = 'entities';
// 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'
}
}
};
/**
* example callback function sent to the handler.converse function, change this function to suit your needs
* @param result - request result from WCS
* @param response - the response variable
* @param context - variable holding the utterance, session and updated skill context
* @param err - error description in case of an error, otherwise undefined
*/
let converseCallback = function (result, response, context, err) {
// this variable would preferably come from your wcs and decide whether the session has ended
let deleteSkillSession = false;
if (err) {
response.say(handler.t('TRY_AGAIN')).send();
console.error(err);
}
else {
// example of adding a card
// example of a card sent to the application, the action and the json most of the time will come from WCS
response.card('some action', {"some": "json"});
response.say(result.output.text).deleteSkillSession(deleteSkillSession).send();
}
};
/**
* example callback call for the handler.evaluationRequest function.
* this callback is called after the nlu evaluation process.
*
* @param result - response from the nlu engine (could be undefined)
* @param evaluationResponse - an evaluationResponse object containing
* {
* response: {
* responseCode - status of the evaluation request
* requestResult - the response of the nlu engine
* entities - the intentity object which holds the confidence for each intent/entity
* handleUtterance - a boolean which tells WA whether the skill would like to handle this request
* context - the context after the evaluation request
* }
*
* }
* @param context - context object containing, application, skill and session context
* @param err
*/
let evaluationCallback = function(result, evaluationResponse, context, err) {
if(err) {
console.error(err);
}
else {
if(!result) {
result = ['Nlu engine did not return an output'];
}
evaluationResponse.send(result[0]);
}
};
// Actions for DEFAULT state
const stateDefaultActions = handler.createActionsHandler({
//pre processing before the request evaluation
evaluation: (request, evaluationResponse, context) => {
handler.evaluateRequest(request, evaluationResponse, context, evaluationCallback);
},
// this is the entities action, routing by entity will lead here
entities: (request, response, context) => {
handler.converse(request, response, context, converseCallback);
},
// this is an example of an intent using a regex engine, the intent catches the phrase "hello"
'hello-world': (request, response, context) => {
response.say(handler.t('HELLO_WORLD')).send();
},
//this is an example of an intent using wcs - in order for this to work you need your own wcs workspace and intents
//and change the intents name with your own
'hello-world-wcs': (request, response, context) => {
// this is how you directly call WA using the handler
handler.converse(request, response, context, converseCallback);
// alternatively you can use the response from the evaluation
// first save the context from the evaluation
handler.saveEvaluationContext(context, request.evaluationResponse.context);
// then return the answer from the nlu engine
response.say(request.evaluationResponse.response).send();
},
'unhandled': (request, response, context) => {
response.say(handler.t('TRY_AGAIN')).send();
}
}, 'DEFAULT');
module.exports = () => {
// Register language translations.
handler.registerLanguages(languageResource);
// Register state actions
handler.registerActionsHandler(stateDefaultActions);
};