Created
May 16, 2018 06:31
-
-
Save dileephell/501a7f1e665a1114b27ae9bc7e9617c5 to your computer and use it in GitHub Desktop.
index of bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var app = express(); | |
var path = require('path'); | |
var Busboy = require('busboy'); | |
//require('./chatflow.js').DataTable; | |
var chatFlow=require('./chatflow.js'); | |
const fs = require('fs'); | |
const request = require('request'); | |
const url = require('url'); | |
const https = require('https'); | |
const http = require('http'); | |
const formidable = require('formidable'); | |
var bodyParser = require('body-parser'); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(bodyParser.json()); | |
app.use(express.static('public')) | |
var rootCas = require('ssl-root-cas').create(); | |
require('https').globalAgent.options.ca = rootCas; | |
var html = ''; | |
var newlyAddedTags = ''; | |
var nextMsgId = 0; | |
var incrementIntent = 0 ; | |
//global._ = require('uploadedFileName') | |
global.uploadedFileName=''; | |
//var k = -1; | |
var prevMsg = DataTable[0][1]; | |
// viewed at http://localhost:8080 | |
app.get('/', function(req, res) { | |
res.sendFile(path.join(__dirname + './public/index.html')); | |
}); | |
app.get('/dat',function(req, res){ | |
var msg = ""; | |
if (req.method == 'GET') { | |
msg = req.query.input1; | |
intent = ""; | |
if ("text" != chatFlow.DataTableArray[nextMsgId][2]) { | |
getWitResponse(msg,function(intentVal) | |
{ | |
console.log(intentVal + " --> intent from wit"); | |
intent = String(intentVal).toLowerCase(); | |
if (intent.length > 0 ){ | |
console.log("Prev msg before calling nextmsgid: "+prevMsg); | |
nextMsgId = getNextMsgId(intent,msg); | |
console.log("nextMsgId" + nextMsgId); | |
prevMsg = chatFlow.DataTableArray[nextMsgId][1]; | |
console.log("prevMsg:------>>>>>>> "+prevMsg); | |
if(prevMsg == "I see you gave me a classification file today. Is this correct? Please say Yes or No?") | |
{ | |
console.log("before getrespwithpython function invoke:::: "+global.uploadedFileName); | |
getResponseWithPython(prevMsg,function(bodyVal){ | |
prevMsg = "I see you gave me a "+bodyVal+" file today. Is this correct? Please say Yes or No?"; | |
console.log("prevMsg in the bodyVal" + prevMsg); | |
console.log("bodyVal " + bodyVal); | |
res.send(prevMsg); | |
}); | |
} else { | |
res.send(prevMsg); | |
} | |
} | |
}); | |
} else if ('text' in chatFlow.DataTableArray[nextMsgId][2]) { | |
intent='text'; | |
} | |
if (nextMsgId == 0) { | |
prevMsg=chatFlow.DataTableArray[0][1]; | |
} | |
} | |
}); | |
var getWitResponse= function(msg,cb) { | |
const {Wit, log} = require('node-wit'); | |
const client = new Wit({accessToken: '7KPQXSZ4NSMMYTCCGWKC37XHLBPO25XA'}); | |
client.message(msg, {}) | |
.then((data) => { | |
const j = JSON.parse(JSON.stringify(data)); | |
if (JSON.stringify(data).toLowerCase().includes("intent")) { | |
return cb(j['entities']['intent'][0]['value']); | |
} else { | |
return ''; | |
} | |
}) | |
.catch(console.error); | |
} | |
function getNextMsgId(intent,msg) { | |
for (var i = 0 ; i<chatFlow.DataTableArray.length; i++) { | |
console.log("Inside For In Loop with prevMsg " + prevMsg ); | |
if (String(prevMsg) === String(chatFlow.DataTableArray[i][1]) && String(intent)===String(chatFlow.DataTableArray[i][2])) { | |
console.log("Inside IF Loop with prevMsg " + prevMsg ); | |
chatFlow.DataTableArray[i][4] = msg; | |
console.log("Inside the loop printing the message " + msg); | |
//k++; | |
return chatFlow.DataTableArray[i][3]; | |
} | |
} | |
return 0; | |
} | |
function getResponseWithPython (prevMsg,cb) { | |
console.log("uploadedFileName:----------->>>>> "+global.uploadedFileName); | |
var pythonRes=''; | |
var formData = { | |
// Pass data via Streams | |
input_file: fs.createReadStream(__dirname +'/AREASUSA_EmployeeElectionsHeader.csv') | |
}; | |
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; | |
var req = request.post({url:'https://google.com:443/classification/', formData: formData}, function optionalCallback(err, httpResponse, body) { | |
if (err) { | |
return console.error('upload failed:', err); | |
} | |
console.log('Upload successful! Server responded with:', body); | |
pythonRes=body; | |
return cb (pythonRes); | |
}); | |
//console.log("pythonRes222222:::::: "+pythonRes); | |
//return pythonRes; | |
}; | |
app.get('/uploadfile', function(req,res){ | |
//renders a multipart/form-data form | |
res.render("upload file", { title : 'Upload File' }); | |
}); | |
//upload route | |
app.post('/upload', function (req, res) { | |
console.log("Context path1=" + __dirname); | |
var busboy = new Busboy({ headers: req.headers }); | |
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { | |
console.log("Context path=" + __dirname); | |
var saveTo = path.join(__dirname +"/uploads" ,filename); | |
console.log("inside upload get request111111::: "+filename); | |
console.log('Uploading: ' + saveTo); | |
file.pipe(fs.createWriteStream(saveTo)); | |
global.uploadedFileName=filename; | |
console.log("inside upload get request222222::: "+global.uploadedFileName); | |
}); | |
busboy.on('finish', function() { | |
console.log('Upload complete'); | |
res.writeHead(200, { 'Connection': 'close' }); | |
res.end("That's all folks!"); | |
}); | |
console.log("global.uploadedFileName in callback:::: "+global.uploadedFileName); | |
return req.pipe(busboy); | |
}); | |
app.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment