-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path10-wit.js
58 lines (57 loc) · 1.93 KB
/
10-wit.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
module.exports = function(RED) {
var request = require('request'),
rec = require('node-record-lpcm16');
function witaiText(config) {
RED.nodes.createNode(this,config);
var node = this;
this.on('input', function(msg) {
var options = {
url: 'https://api.wit.ai/message',
qs: {'q': msg.payload},
json: true,
headers: {
'Authorization': 'Bearer ' + config.access_token,
'Accept': 'application/vnd.wit.' + config.version
}
};
request(options, function (error, response, body) {
if (response && response.statusCode != 200) {
error = "Invalid response received from server: " + response.statusCode + " - " + body;
node.error(error);
} else {
msg.payload = body;
node.send(msg);
};
});
});
}
function witaiVoice(config){
RED.nodes.createNode(this,config);
var node = this;
this.on('input', function(msg){
rec.start().pipe(request.post({
'url' : 'https://api.wit.ai/speech?client=chromium&lang=en-us&output=json',
'headers' : {
'Accept' : 'application/vnd.wit.20160202+json',
'Authorization' : 'Bearer ' + config.access_token,
'Content-Type' : 'audio/wav',
'Transfer-encoding' : 'chunked'
}
}, function (error, response, body) {
if (response && response.statusCode != 200) {
error = "Invalid response received from server: " + response.statusCode + " - " + body;
node.error(error);
} else {
msg.payload = body;
msg.error = error
node.send(msg);
};
}));
setTimeout(function () {
rec.stop();
}, config.timeout);
})
}
RED.nodes.registerType("wit.ai text",witaiText);
RED.nodes.registerType("wit.ai voice",witaiVoice);
}