-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·115 lines (95 loc) · 3.41 KB
/
app.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
var express = require('express'),
app = express(),
request = require('request'),
bluemix = require('./config/bluemix'),
extend = require('util')._extend,
bodyParser = require('body-parser'),
nodemailer = require('nodemailer');
//conversationStore = require('./javascript/ConversationStore');
app.use(bodyParser.json()); // support json encoded bodies
// Bootstrap application settings
require('./config/express')(app);
// if bluemix credentials exists, then override local
var dialogCredentials = extend({
url: "https://gateway.watsonplatform.net/dialog/api",
username: "username",
password: "password"
}, bluemix.getServiceCreds('dialog')); // VCAP_SERVICES
// if bluemix credentials exists, then override local
var nlcCredentials = extend({
"url": "https://gateway.watsonplatform.net/natural-language-classifier/api",
username: "username",
password: "password"
}, bluemix.getServiceCreds('natural_language_classifier')); // VCAP_SERVICES
// HTTP proxy to the API
app.use('/proxy', function(req, res) {
var credentials = req.query.proxyType === "nlc" ? nlcCredentials : dialogCredentials;
// Remove api as it will be passed from the web client
if (credentials.url.indexOf('/api') > 0) {
credentials.url = credentials.url.substring(0, credentials.url.indexOf('/api'));
}
req.pipe(request({
url: credentials.url + req.url,
auth: {
user: credentials.username,
pass: credentials.password,
sendImmediately: true,
}
}, function(error){
if (error) {
res.status(500).json({code: 500, error: errorMessage});
}
})).pipe(res);
});
// Pass user text to NLC to identify intent
app.post('/saveConversation', function(req, res) {
//conversationStore.storeConversation(req.body);
res.status(200);
res.end();
});
app.post('/sendMail', function(req, res) {
var transporter = nodemailer.createTransport('smtps://uobithelpdesk%40gmail.com:[email protected]');
// setup e-mail data with unicode symbols
var mailOptions = {
from: 'email', // sender address
to: 'email', // list of receivers
subject: 'Problem info', // Subject line
text: req.body.description // plaintext body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
res.send(req.body);
//conversationStore.storeConversation(req.body);
res.status(200);
res.end();
});
// render index page
app.get('/', function(req, res) {
res.render('index');
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.code = 404;
err.message = 'Not Found';
next(err);
});
// 500 error message
var errorMessage = 'There was a problem with the request, please try again';
// non 404 error handler
app.use(function(err, req, res) {
var error = {
code: err.code || 500,
error: err.message || err.error || errorMessage
};
console.log('error: ' + JSON.stringify(error) + " for url: " + req.url);
res.status(error.code).json(error);
});
var port = process.env.VCAP_APP_PORT || 3000;
app.listen(port);
console.log('listening at:', port);