-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.js
executable file
·127 lines (111 loc) · 3.4 KB
/
message.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
122
123
124
125
126
127
/**
* Copyright 2017 IBM Corp. All Rights Reserved.
*
* 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.
*/
'use strict';
const Conversation = require('watson-developer-cloud/conversation/v1'); // watson sdk
const config = require('../util/config');
// Create a Service Wrapper
let conversation = new Conversation(config.conversation);
let getConversationResponse = (message, context) => {
let payload = {
workspace_id: process.env.WORKSPACE_ID,
context: context || {},
input: message || {}
};
payload = preProcess(payload);
return new Promise((resolved, rejected) => {
// Send the input to the conversation service
conversation.message(payload, function(err, data) {
if (err) {
reject(err);
}
resolved(postProcess(data));
});
})
}
let postMessage = (req, res) => {
let message = req.body.input || {};
let context = req.body.context || {};
getConversationResponse(message, context).then(data => {
return res.json(data);
}).catch(err => {
return res.status(err.code || 500).json(err);
});
}
/**
* 사용자의 메세지를 Watson Conversation 서비스에 전달하기 전에 처리할 코드
* @param {Object} user input
*/
let preProcess = payload => {
var inputText = payload.input.text;
console.log("User Input : " + inputText);
console.log("Processed Input : " + inputText);
console.log("--------------------------------------------------");
return payload;
}
/**
* Watson Conversation 서비스의 응답을 사용자에게 전달하기 전에 처리할 코드
* @param {Object} watson response
*/
let postProcess = response => {
console.log("Conversation Output : " + response.output.text);
console.log("--------------------------------------------------");
if(response.context && response.context.action){
return doAction(response, response.context.action);
}
return response;
}
/**
* 대화 도중 Action을 수행할 필요가 있을 때 처리되는 함수
* @param {Object} data : response object
* @param {Object} action
*/
let doAction = (data, action) => {
console.log("Action : " + action.command);
switch(action.command){
case "check-availability":
return checkAvailability(data, action);
break;
case "confirm-reservation":
return confirmReservation(data, action);
break;
default: console.log("Command not supported.")
}
return data;
}
/**
* 회의실의 예약 가능 여부를 체크하는 함수
* @param {Object} data : response object
* @param {Object} action
*/
let checkAvailability = (data, action) => {
//TODO
return data;
}
/**
* Make reservation
* @param {Object} data : response object
* @param {Object} action
*/
let confirmReservation = (data, action) =>{
//TODO
return data;
}
module.exports = {
'initialize': (app, options) => {
app.post('/api/message', postMessage);
},
'getConversationResponse' : getConversationResponse
};