Skip to content

Instantly share code, notes, and snippets.

@dileephell
Last active April 16, 2018 16:06
Show Gist options
  • Save dileephell/62234458260841f209e8b191eca246b6 to your computer and use it in GitHub Desktop.
Save dileephell/62234458260841f209e8b191eca246b6 to your computer and use it in GitHub Desktop.
wit
var express = require('express');
var app = express();
var path = require('path');
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 DataTable = [[]];
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 msg = "";
if (req.method == 'GET') {
msg = req.query.input1;
console.log("Inside req method" + msg );
intent = "";
if ("text" != DataTable[nextMsgId][2]) {
intent = String(getWitResponse(msg)).toLowerCase();
console.log("Inside nested req method" + intent );
} else if ('text' === DataTable[nextMsgId][2]) {
intent='text';
}
if (nextMsgId == 0) {
prevMsg=DataTable[0][1];
}
if (intent.length > 0 ){
nextMsgId = getNextMsgId(intent,msg)
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"
}
}
}
return prevMsg;
});
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,msg) {
for (c in DataTable) {
if (str(prevMsg).lower() in String(c[1]).lower() && (intent in String(c[2]).lower() || ((String(prevMsg).lower() in String(c[1]).lower() && str(c[2])=='text') || (String(prevMsg).lower() in String(c[1]).lower() && str(c[2])=='text')))) {
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