-
Notifications
You must be signed in to change notification settings - Fork 3
/
preprocess.js
173 lines (163 loc) · 6.04 KB
/
preprocess.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
/preprocess is triggered by /remember upon fetching the user's previously stored context or newly created record
pre-processess the input received from Facebook Messenger then sends it to Watson Covnersation
pre-processing includes:
- classifying images sent by the user using Watson Visual Recognition
- transcribing audio send by the user using Speech to text
- translation location pins sent by the user and appending coordinates to context
once processed, the input is sent to Watson conversation
- images are sent as text in the following format: "ADMINVISUAL" with context variables visual.score and visual.class
- audio is sent as regular text input
- images are sent as text in the following format: "ADMINVISUAL" with context variables coordinates.lat and coordinates.long
Watson Conversation handles the input accordingly, and sends back text output along with the updated context
the output is passed to /process as follows:
{
"userid": userid,
"text": output received from conversation,
"context": updated context
}
Default Parameters
visual_apikey
conversation_username
conversation_password
conversation_workspace
audio_url
*/
var http = require('http');
var request = require('request');
const promiseReq = require('request-promise')
var watson = require('watson-developer-cloud');
function main(params){
var preprocess = new Promise(function (resolve, reject){
if (params.attachment){
console.log("pre-processing attachment")
if (params.attachment.type=="image"){
console.log("image")
// if (params.attachment.payload.sticker_id)
// {
// console.log("it's a sticker")
// params.text = "ADMINVISUAL sticker"
// params.context.visual = {
// "type": "sticker"
// }
// }
// else {
//CLASSIFY IMAGE
var visual = new watson.VisualRecognitionV3({
api_key: params.visual_apikey,
version_date: '2015-05-19'
});
var input = {
url: params.attachment.payload.url
};
visual.classify(input, function(err, res) {
if (err) {
console.log(err);
reject(err)
}
else {
console.log(JSON.stringify(res, null, 2));
var payload = {
"userid": params.userid,
"text": "ADMINVISUAL " + res.images[0].classifiers[0].classes[0].score + ":" + res.images[0].classifiers[0].classes[0].class,
"context": {"visual": {
"type": "image",
"score": res.images[0].classifiers[0].classes[0].score,
"class": res.images[0].classifiers[0].classes[0].class
}}
}
resolve(payload)
}
});
}
else if (params.attachment.type=="audio"){
console.log("audio")
var transcribe = promiseReq({
method: 'POST',
url: params.audio_url,
json: {"url": params.attachment.payload.url}
})
return transcribe
.then(function(res){
console.log(res)
var payload = {
"userid": params.userid,
"context": params.context,
"text": res.response.result.text
}
resolve(payload)
})
.catch(function(err){
reject(err)
})
}
else if (params.attachment.type=="location"){
console.log("location")
params.context.coordinates = {
lat: params.attachment.payload.coordinates.lat,
long: params.attachment.payload.coordinates.long
}
var payload = {
"userid": params.userid,
"text": "ADMINLOCATION",
"context": params.context
// "context":
// {"coordinates": {
// "lat": params.attachment.payload.coordinates.lat,
// "long": params.attachment.payload.coordinates.long
// }
// }
}
resolve(payload)
}
}
else {
console.log("no attachment found")
resolve(params)
}
})
return preprocess
.then(function(a) {
console.log('preprocess ' + JSON.stringify(a));
return new Promise(function(resolve, reject) {
var conversation = watson.conversation({
url: "https://gateway.watsonplatform.net/conversation/api",
username: params.conversation_username,
password: params.conversation_password,
version: 'v1',
version_date: '2017-05-26'
});
if (params.context.selector){
params.context.selector = {};
}
if (params.context.fields){
params.context.fields = []
}
conversation.message({
workspace_id: params.conversation_workspace,
input: {'text': a.text},
context: a.context
}, function(err, response) {
if (err){
console.log('error:', err);
reject(err);
}
else {
console.log(JSON.stringify(response, null, 2));
resolve(response);
}
});
})
.then(function(b) {
console.log('process' + JSON.stringify(params));
return {
'userid': params.userid,
'text': b.output.text[0],
'context': b.context
}
})
.catch(function(b) { console.log('process: ', b); });
})
.catch(function(a) { console.log('preprocess: ', a); });
}
module.exports.main = main;