Skip to content

Instantly share code, notes, and snippets.

@dileephell
Last active May 2, 2018 13:04
Show Gist options
  • Save dileephell/21ac7163689225b48cc8f1e7fcc87b7b to your computer and use it in GitHub Desktop.
Save dileephell/21ac7163689225b48cc8f1e7fcc87b7b to your computer and use it in GitHub Desktop.
var express = require('express');
var app = express();
var path = require('path');
//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');
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 ;
var k = -1;
var prevMsg = DataTable[0][1];
// viewed at http://localhost:8080
app.get('/', function(req, res) {
//var nextMsgId = 0;
res.sendFile(path.join(__dirname + './public/index.html'));
});
app.get('/dat',function(req, res){
console.log("inside /dat "+ req );
var nextMsgId = 0;
//var prevMsg = DataTable[0][1];
var msg = "";
if (req.method == 'GET') {
msg = req.query.input1;
console.log("Inside req method " + msg );
intent = "";
console.log("DataTable " + chatFlow.DataTableArray[0][1]);
if ("text" != chatFlow.DataTableArray[nextMsgId][2]) {
console.log("0 & 1::::: "+chatFlow.DataTableArray[0][1]);
console.log("nextMsgId " + nextMsgId);
getWitResponse(msg,function(intentVal)
{
console.log(intentVal+":::: intentVal");
intent = String(intentVal).toLowerCase();
if (intent.length > 0 ){
console.log("Checking intent length inside if " + intent.length);
nextMsgId = getNextMsgId(intent,msg);
console.log("Inside nested req method " + intent );
console.log("Intent in getNextMsgId " + 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("inside prevmsg comparision to append file name with existed string---->>>>> ");
getResponseWithPython(prevMsg,function(bodyVal){;
console.log("bodyVal:::::: "+bodyVal);
prevMsg = "I see you gave me a "+bodyVal+" file today. Is this correct? Please say Yes or no ?";
});
}
console.log("Intent in getNextMsgId " + prevMsg);
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('Hi', {})
.then((data) => {
console.log('Yay, got Wit.ai response: ' + JSON.stringify(data));
const j = JSON.parse(JSON.stringify(data));
console.log("jis here "+ JSON.stringify(j));
if (JSON.stringify(data).toLowerCase().includes("intent")) {
console.log("inside if getWitResponse")
console.log(j['entities']['intent'][0]['value'] + " inside JSON obj");
return cb(j['entities']['intent'][0]['value']);
} else {
return '';
}
})
.catch(console.error);
}
function getNextMsgId(intent,msg) {
console.log("Inside getNextMsgId method " + chatFlow.DataTableArray.length );
for (var i=0;i<chatFlow.DataTableArray.length;i++) {
console.log("Inside For In Loop with prevMsg " + prevMsg );
console.log("Inside For In Loop with DT " + chatFlow.DataTableArray[i][1]);
if (String(prevMsg) === String(chatFlow.DataTableArray[i][1]) && String(intent)===String(chatFlow.DataTableArray[i][2])) {
chatFlow.DataTableArray[i][4] = msg;
console.log("msg inside the for in loop of getMsgId " + msg);
console.log("returning the index of the chatflow " + chatFlow.DataTableArray[i][3]);
k++;
console.log("my incremented value k:::: "+k);
return chatFlow.DataTableArray[k][3];
}
}
return 0;
}
function getResponseWithPython (prevMsg,cb) {
console.log("inside getResponseWithPython ::::::");
var pythonRes='';
var formData = {
// Pass data via Streams
input_file: fs.createReadStream(__dirname + '/AREASUSA_EmployeeElectionsHeader.csv')
// Pass multiple values /w an Array
// attachments: [
// fs.createReadStream(__dirname + '/myfile.txt'),
// fs.createReadStream(__dirname + '/myfile.txt')
// ],
};
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var req = request.post({url:'sbc: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;
console.log("pythonRes11111:::::: "+pythonRes);
return cb (pythonRes);
});
console.log("pythonRes222222:::::: "+pythonRes);
//return pythonRes;
};
app.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment