-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
82 lines (61 loc) · 2.65 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
/*************************************************************************************************
Define global variables for NPM packages and Cloud Foundry environment
*************************************************************************************************/
"use strict";
var express = require('express'),
cfenv = require("cfenv"),
appEnv = cfenv.getAppEnv(),
app = express(),
bodyParser = require('body-parser'),
watson = require('watson-developer-cloud');
/*************************************************************************************************
Start the server
*************************************************************************************************/
app.use(bodyParser());
app.use(express.static(__dirname + '/public'));
var appEnv = cfenv.getAppEnv();
app.listen(appEnv.port, '0.0.0.0', function() {
console.log("server starting on " + appEnv.url);
});
/*************************************************************************************************
Watson Conversation
*************************************************************************************************/
var conversation = watson.conversation({
url: 'https://gateway.watsonplatform.net/conversation/api',
username: '6833c33e-5b12-4d5f-86c5-83c7f5d59e4f', // Set to your conversation username
password: 'sqZWc6LB7TPJ', // Set to your conversation password
version_date: '2016-07-11',
version: 'v1'
});
// Allow clients to interact with the bot
app.post('/api/bot', function(req, res) {
console.log("Got request for Le Bot");
console.log("Request is: ", req);
var workspace = 'cf5ad460-14f3-4abd-b282-ab1a31c8a52d'; // Set to your Conversation workspace ID
if (!workspace) {
console.log("No workspace detected. Cannot run the Watson Conversation service.");
}
var params = {
workspace_id: workspace,
context: {}, // Null context indicates new conversation
input: {} // Holder for message
};
// Update options to send to conversation service with the user input and a context if one exists
if (req.body) {
if (req.body.input) {
params.input = req.body.input;
}
if (req.body.context) {
params.context = req.body.context;
}
}
// Send message to the conversation service with the current context
conversation.message(params, function(err, data) {
if (err) {
console.log("Error in sending message: ", err);
return res.status(err.code || 500).json(err);
}
console.log("Response: ", data);
return res.json(data);
});
}); // End app.post '/api/bot'