-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (65 loc) · 2.8 KB
/
server.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
var express = require('express');
var cfenv = require('cfenv');
var callbacks = [];
var ConversationV1 = require('watson-developer-cloud/conversation/v1');
//var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
var conversation = new ConversationV1({
username: '7b5357ef-869f-42eb-8cd4-e965f27d5c67',
password: 'DJUOdf2he1b1',
path: { workspace_id: 'def063cb-8588-4fdf-980e-561913dbd697' },
version_date: '2017-06-20'
});
var text_to_speech = new TextToSpeechV1 ({
username: 'e846beae-c826-4d6c-83e6-19573de86de2',
password: '4A8II1Ucw3CE'
});
var vContextoWatson = '';
function appendMessage(message){
var resp = {messages: [message]};
while (callbacks.length > 0) {
callbacks.shift()(resp);
}
}
function procesarRespuesta(err, response){
var vRespuestaWatson;
if (response.output.text.length != 0) {
vRespuestaWatson = response.output.text[0];
console.log(vRespuestaWatson)
vContextoWatson = response.context;
var respuesta = {
nickname : 'Tornillito',
text: vRespuestaWatson
}
appendMessage(respuesta);
}
}
var app = express();
app.use(express.bodyParser());
app.get('/', function(req, res){
res.sendfile('index.html');
});
app.post('/send', function(req, res){
var message = {
nickname: req.param('nickname', 'Anonymous'),
text: req.param('text', '')
};
appendMessage(message)
if (vContextoWatson == '') {
conversation.message(message.text, procesarRespuesta)
}
else{
conversation.message({input: {text: message.text}, context:vContextoWatson}, procesarRespuesta)
}
res.json({status: 'ok'});
});
app.get('/recv', function(req, res){
callbacks.push(function(message){
res.json(message);
});
});
var appEnv = cfenv.getAppEnv();
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
//app.listen(3000)