Skip to content

Commit cfc0ea8

Browse files
author
Ranieri Abreu Silva Junior
committed
Fix response
1 parent cecde95 commit cfc0ea8

File tree

3 files changed

+52
-43
lines changed

3 files changed

+52
-43
lines changed

Diff for: ml/tensorflowjs.js

+49-41
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const intentsModels = require('../models/intents');
1111
const arr = require('../Libs/ExtraFunctions');
1212
const BotConfig = require('../Libs/BotConfig');
1313

14+
const BotName = BotConfig.BotName
15+
1416
class Agent {
1517
constructor(Language, debug = false) {
1618
this.isAgentBuilding = false;
@@ -93,7 +95,7 @@ class Agent {
9395

9496
_configResponse(sentence) {
9597
var resp = this._replaceAll(arr.random(sentence), '{botname}', BotName);
96-
resp = this._replaceAll(resp, '{botversion}', '1.0.0');
98+
resp = this._replaceAll(resp, '{botversion}', '2.5.3');
9799
return resp;
98100
}
99101

@@ -196,9 +198,9 @@ class Agent {
196198
this.isAgentBuilding = true;
197199
intentsModels.find({}).lean().exec((err, intentsList) => {
198200
if (err) throw err
199-
200201
var i = 0;
201202
const iMax = intentsList.length
203+
this.intents = intentsList
202204
for (; i < iMax; i++) {
203205
var j = 0;
204206
const jMax = intentsList[i].patterns.length
@@ -354,51 +356,57 @@ class Agent {
354356
}
355357
}
356358

357-
async response(sentence, userID, show_details) {
358-
try {
359-
var i = 0;
360-
var reply = arr.random(await this._getFallBack());
361-
var results = await this.classify(sentence);
362-
//if we have a classification then find the matching intent tag
363-
if (results && results.length > 0) {
364-
//loop as long as there are matches to process
365-
while (results[i]) {
366-
var j = 0;
367-
const jMax = this.intents.length;
368-
for (; j < jMax; j++) {
369-
//set context for this intent if necessary
370-
if (this.intents[j].tag == results[0][0]) {
371-
if (arr.inArray('context_set', this.intents[j])) {
372-
//set context
373-
this._setContext(userID, this.intents[j]['context_set']);
374-
if (show_details) {
375-
console.log('context: ' + this.intents[j]['context_set'])
359+
response(sentence, userID, show_details) {
360+
return new Promise(async (resolve, reject) => {
361+
try {
362+
var i = 0;
363+
var results = await this.classify(sentence);
364+
//if we have a classification then find the matching intent tag
365+
if (results && results.length > 0) {
366+
//loop as long as there are matches to process
367+
while (results[i]) {
368+
var j = 0;
369+
const jMax = this.intents.length;
370+
for (; j < jMax; j++) {
371+
//set context for this intent if necessary
372+
if (this.intents[j].tag == results[0][0]) {
373+
if (arr.inArray('context_set', this.intents[j])) {
374+
//set context
375+
this._setContext(userID, this.intents[j]['context_set']);
376+
if (show_details) {
377+
console.log('context: ' + this.intents[j]['context_set'])
378+
}
376379
}
377-
}
378-
//check if this intent is contextual and applies to this user's conversation
379-
if (!arr.inArray('context_filter', s) || arr.UserFilter(this.context, userID) && arr.inArray('context_filter', s) && this.intents[j]['context_filter'] == this.context[this.context.findIndex(x => x.uID == userID)].ctx) {
380-
if (show_details) {
381-
console.log('tag: ' + this.intents[j]['tag']);
380+
//check if this intent is contextual and applies to this user's conversation
381+
if (!arr.inArray('context_filter', this.intents[j])
382+
|| arr.UserFilter(this.context, userID)
383+
&& arr.inArray('context_filter', this.intents[j])
384+
&& this.intents[j]['context_filter'] == this.context[this.context.findIndex(x => x.uID == userID)].ctx) {
385+
if (show_details) {
386+
console.log('tag: ' + this.intents[j]['tag']);
387+
}
388+
//remove user context
389+
this.context.slice(this.context.findIndex(x => x.uID == userID), 1)
390+
//a random response from the intent
391+
return resolve(this._configResponse(this.intents[j]['responses']));
392+
} else {
393+
//a random response from the intent
394+
return resolve(this._configResponse(this.intents[j]['responses']));
382395
}
383-
//remove user context
384-
this.context.slice(this.context.findIndex(x => x.uID == userID), 1)
385-
//a random response from the intent
386-
reply = this._configResponse(this.intents[j]['responses']);
387-
} else {
388-
//a random response from the intent
389-
reply = this._configResponse(this.intents[j]['responses']);
390396
}
391397
}
398+
results.shift();
399+
i++;
392400
}
393-
results.shift();
394-
i++;
395401
}
396-
}
397-
return reply;
398-
} catch (error) {
399-
console.log(error)
400-
return "Internal error >X("
401-
}
402+
resolve(arr.random(await this._getFallBack()))
403+
} catch (error) {
404+
console.log(error)
405+
resolve("Internal error >X(")
406+
}
407+
})
408+
409+
402410
}
403411
}
404412

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "chatbottensorflow",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"private": true,
55
"scripts": {
66
"start": "node ./bin/www",

Diff for: routes/controllers/indexController.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const buildAgent = async (req, res, next) => {
1616

1717
const ask = async (req, res, next) => {
1818
try {
19-
res.end(Agent.response(req.body.say, req.body.uID, true))
19+
let resp = await Agent.response(req.body.say, req.body.uID, true)
20+
res.end(resp)
2021
} catch (error) {
2122
res.end("Ops, internal error X(")
2223
}

0 commit comments

Comments
 (0)