Skip to content

Commit

Permalink
Issue TarentoTechnologies#1 Add redis to node
Browse files Browse the repository at this point in the history
  • Loading branch information
“your committed Apr 3, 2020
1 parent abe96dd commit 14b1f64
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions router/appRest.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ var RasaCoreController = require('./controllers/rasaCoreController')
var EDB = require('./api/elastic/connection')
const appBot = express()

//configure redis client on port 6379
const port_redis = config.REDIS_PORT || 6379;
const redis_client = redis.createClient(port_redis);

//// IVR is best done outside the bot...as no NLU interpretation is required


Expand All @@ -35,22 +31,32 @@ appBot.post('/bot', function (req, res) {
LOG.info('req.body:')
LOG.info(req.body)
data = { message: body, customData: { userId: req.body.From } }
sessionID = req.body.From
sessionID = req.body.From;
LOG.info('context for: ' + sessionID)
LOG.info(memory[sessionID])
res.set('Content-Type', 'text/plain')
redis_client = createRedisClient();

//persisting incoming data to EDB
dataPersist = {'message': body, 'channel' : 'rest'}
//EDB.saveToEDB(dataPersist, 'user', sessionID,(err,response)=>{})

if (req.body.From) {
redis_client.setx(req.body.From, 3600, req.body.From);
}

if (body == '0') {
memory = {}
}

if (req.body.From) {
userData = {};
redis_client.get(req.body.From, (err, data) => {
if (data != null) {
userData = data;
} else {
obj = { sessionId: req.body.From, role: '', educationLvl: '', board: '', boardType: ''};
setRedisKeyValue(redis_client, req.body.From, JSON.stringify(obj), 3600);
}
});
}

// all non numeric user messages go to bot
if (isNaN(body)) {
///Bot interaction flow
Expand Down Expand Up @@ -139,10 +145,16 @@ appBot.post('/bot', function (req, res) {
else {
LOG.info('setting up role:' + req.body.Body)
memory[sessionID]['role'] = req.body.Body
//setRedisKeyValue();
obj = { sessionId: req.body.From, role: req.body.Body, educationLvl: '', board: '', boardType: ''};
setRedisKeyValue(redis_client, req.body.From, JSON.stringify(obj), 3600);
emitToUser(sessionID, res, literals.message.EDUCATION_LEVEL)
}
} else {
memory[sessionID] = {}
obj = { };
setRedisKeyValue(redis_client, req.body.From, JSON.stringify(obj), 3600);
// setRedisKeyValue(redis_client, req.body.From, {}, 3600);
emitToUser(sessionID, res, literals.message.MENU)
}
})
Expand Down Expand Up @@ -183,3 +195,13 @@ if (config.HTTPS_PATH_KEY) {
});

}

function createRedisClient() {
//configure redis client on port 6379
const port_redis = config.REDIS_PORT || 6379;
return redis.createClient(port_redis);
}

function setRedisKeyValue(client, key, value, expireTime) {
client.setex(key, expireTime, value);
}

0 comments on commit 14b1f64

Please sign in to comment.