forked from sudharshan-govindan/DevConnectTelcoChatBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·180 lines (152 loc) · 4.8 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the 'License'); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
/*eslint-env browser, node, node*/
"use strict";
require("dotenv").config({
silent : true
});
var express = require("express"); // app server
var bodyParser = require("body-parser"); // parser for post requests
var watson = require("watson-developer-cloud"); // watson sdk
var Cloudant = require("cloudant");
var vcapServices = require("vcap_services");
//var WORKSPACE_ID = vcapServices.getCredentials('WORKSPACE_ID') || "<workspace-id>";
var WORKSPACE_ID = '6b3d260f-14ff-4a8a-b9ea-c68db4ff6030';
var app = express();
// Bootstrap application settings
app.use(express.static("./public")); // load UI from public folder
app.use(bodyParser.json());
//credentials
var conversation_credentials = vcapServices.getCredentials("conversation");
var cloudant_credentials = vcapServices.getCredentials("cloudantNoSQLDB");
// Create the service wrapper
var conversation = watson.conversation({
url : "https://gateway.watsonplatform.net/conversation/api",
username : conversation_credentials.username || '',
password : conversation_credentials.password || '',
version_date : "2016-07-11",
version : "v1"
});
var usersMap;
var userDetails;
if (usersMap === undefined || usersMap === null) {
usersMap = new Map();
var cloudant = Cloudant({account:cloudant_credentials.username, password:cloudant_credentials.password});
var db = cloudant.db.use('telco-users');
db.list({include_docs:true}, function (err, data) {
if (err) {
throw err;
}
for (var i = 0; i < data.rows.length; i++) {
userDetails = [data.rows[i].doc.name, data.rows[i].doc.mobileNumber, data.rows[i].doc.emailId, data.rows[i].doc.address];
usersMap.set(data.rows[i].doc.userName, userDetails);
}
});
}
// Endpoint called from the client side
app.post("/api/message", function(req, res) {
var workspace = WORKSPACE_ID;
if (!workspace || workspace === "<workspace-id>") {
return res.json({
"output": {
"text": "Your app is running but it is yet to be configured with a <b>WORKSPACE_ID</b> environment variable."
}
});
}
var userName = req.body.context.userName;
getPerson(userName, function(err, person) {
if(err){
console.log("Error occurred while getting person data ::", err);
return res.status(err.code || 500).json(err);
}
var payload = {
workspace_id : workspace,
context : {
"name" : person.name,
"userName" : person.userName,
"emailId" : person.emailId,
"address" : person.address,
"mobileNumber" : person.mobileNumber,
},
input : {}
};
if (req.body) {
if (req.body.input) {
payload.input = req.body.input;
}
if (req.body.context) {
payload.context = req.body.context;
payload.context.name = person.name;
payload.context.userName = person.userName;
payload.context.emailId = person.emailId;
payload.context.address = person.address;
payload.context.mobileNumber = person.mobileNumber;
}
}
callconversation(payload);
});
// Send the input to conversation service
function callconversation(payload) {
conversation.message(payload, function(err, data) {
if (err) {
console.log("Error occurred while invoking Conversation. ::", err);
return res.status(err.code || 500).json(err);
}
return res.json(data);
});
}
});
// Endpoint called from the client side to validate the entered username
app.post("/api/validate", function(req, res) {
var userName = req.body.input.userName;
var output = {};
getPerson(userName, function(err, person) {
if (err) {
console.log("Error occurred while getting person data ::", err);
return res.status(err.code || 500).json(err);
}
if (person) {
output = {
"valid": "yes"
};
} else {
output = {
"valid": "no"
};
}
return res.json(output);
});
});
//Get the details from Cloudant db
function getPerson(userName, callback) {
var person = {};
if (usersMap !== undefined && usersMap !== null && usersMap.has(userName)) {
var userDetails = usersMap.get(userName);
person = {
"userName": userName,
"name": userDetails[0],
"mobileNumber": userDetails[1],
"emailId": userDetails[2],
"address": userDetails[3]
};
} else {
person = null;
}
callback(null, person);
return;
}
// To be implemented - update profile details to db
module.exports = app;