Skip to content

Instantly share code, notes, and snippets.

@dileephell
Last active April 18, 2018 13:02
Show Gist options
  • Save dileephell/d61029d14839f829d2b46ac13875df1e to your computer and use it in GitHub Desktop.
Save dileephell/d61029d14839f829d2b46ac13875df1e to your computer and use it in GitHub Desktop.
app.js
var express = require('express');
var app = express();
var path = require('path');
require('./chatflow.js').DataTable;
var path = require("path");
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 DataTable = path; var prevMsg = path;
//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 " + DataTable[0][1]);
//console.log("DataTable " + typeof DataTable);
if ("text" != DataTable[nextMsgId][2]) {
//console.log("nextMsgId " + nextMsgId);
getWitResponse(msg,function(intentVal)
{
console.log(intentVal+":::: intentVal");
intent = String(intentVal).toLowerCase();
if (intent.length > 0 ){
console.log("Inside checking intent length method " + prevMsg );
nextMsgId = getNextMsgId(intent);
console.log("Intent in getNextMsgId " + getNextMsgId);
prevMsg = DataTable[nextMsgId][1]
// if (nextMsgId == 31){
// prevMsg = prevMsg+getExtractedValues()
// return prevMsg
// }
// else {
// return "Couldn't understand <b> "+msg+" </b>, I can help you with your insurance queries"
// }
}
});
console.log("outside nested req method" + intent );
//intent = String(getWitResponse(msg)).toLowerCase();
//console.log("Inside nested req method" + intent );
} else if ('text' in DataTable[nextMsgId][2]) {
intent='text';
}
if (nextMsgId == 0) {
prevMsg=DataTable[0][1];
}
console.log("before if cond::: " + intent );
}
});
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']);
return cb(j['entities']['intent'][0]['value']);
} else {
return '';
}
})
.catch(console.error);
}
// function getWitResponse(msg) {
// 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']);
// return j['entities']['intent'][0]['value'];
// } else {
// return '';
// }
// })
// .catch(console.error);
// }
function getNextMsgId(intent) {
console.log("Inside getNextMsgId method" + prevMsg );
for (var c in DataTable) {
console.log("1: "+String(c[1]).toLowerCase()+" 2: "+String(c[2]).toLowerCase())
if (String(prevMsg).toLowerCase() in String(c[1]).toLowerCase() && (intent in String(c[2]).toLowerCase() )) {
c[4]=msg;
return c[3];
return 0;
}
}
}
// function getExtractedValues() {
// st='';
// for (c in DataTable) {
// if (len(String(c[4]))>0) {
// st=st+str(c[1])+':'+str(c[4])+'\n';
// return st;
// }
// }}
app.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment