-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatson.js
More file actions
91 lines (74 loc) · 3.54 KB
/
Copy pathwatson.js
File metadata and controls
91 lines (74 loc) · 3.54 KB
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
var watson = require('watson-developer-cloud');
var WebSocket = require('ws');
var speechAction = require('./speechAction');
var flowHandler = require('./flowHandler');
var authorization = new watson.AuthorizationV1({
"url": process.env.WATSON_SPEECH_RECOGNITION_URL,
"username": process.env.WATSON_USERNAME,
"password": process.env.WATSON_PASSWORD
});
const launchWatson = (conversation_id) => {
var conversation_uuid = conversation_id;
return new Promise((resolve, reject) => {
var ws;
console.log("LAUNCH WATSON");
//Create websocket instance with authenticated token.
authorization.getToken(function (err, token) {
if (!token) {
console.log('error:', err);
} else {
// Use your token here. Initialize socket.
console.log("Watson TOKEN: ", token);
var wsURI = 'wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize' +
'?watson-token=' + token +
'&model=en-US_BroadbandModel';
ws = new WebSocket(wsURI);
ws.on('open', function () {
//Sets Watson state to listening.
var message = {
'action': 'start',
'continuous': true,
'interim_results': true,
'content-type': 'audio/l16;rate=16000'
};
console.log("WS OPEN AND LISTENING");
ws.send(JSON.stringify(message));
//On Open and session start. Return Watson object.
resolve(ws);
});
//Receive text back from watson STT.
ws.on('message', function (watsonPackage) {
var message;
watsonPackage = JSON.parse(watsonPackage);
//Efficiently parse & map watson result.
if (watsonPackage.results) {
for (var i = 0; i < watsonPackage.results.length; i++) {
var result = watsonPackage.results[i]
if (result.final) {
if (result.transcript) {
//You can test confidence here with result.confidence.
message = result.transcript;
} else if (result.alternatives) {
for (var o = 0; o < result.alternatives.length; o++) {
message = result.alternatives[o].transcript;
}
}
console.log("WATSON MESSAGE: ", message);
//Map speech transcript to intent.
speechAction.handleSpeech(message).then((mappedIntent) => {
console.log("mappedIntent: ", mappedIntent);
flowHandler.returnSpeechDataToConversation(mappedIntent, conversation_uuid);
//Stop transmitting to watson.
ws.send(JSON.stringify({
"action": "stop"
}));
})
}
}
}
});
}
});
});
}
module.exports.launchWatson = launchWatson;