@@ -11,6 +11,8 @@ const intentsModels = require('../models/intents');
11
11
const arr = require ( '../Libs/ExtraFunctions' ) ;
12
12
const BotConfig = require ( '../Libs/BotConfig' ) ;
13
13
14
+ const BotName = BotConfig . BotName
15
+
14
16
class Agent {
15
17
constructor ( Language , debug = false ) {
16
18
this . isAgentBuilding = false ;
@@ -93,7 +95,7 @@ class Agent {
93
95
94
96
_configResponse ( sentence ) {
95
97
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 ' ) ;
97
99
return resp ;
98
100
}
99
101
@@ -196,9 +198,9 @@ class Agent {
196
198
this . isAgentBuilding = true ;
197
199
intentsModels . find ( { } ) . lean ( ) . exec ( ( err , intentsList ) => {
198
200
if ( err ) throw err
199
-
200
201
var i = 0 ;
201
202
const iMax = intentsList . length
203
+ this . intents = intentsList
202
204
for ( ; i < iMax ; i ++ ) {
203
205
var j = 0 ;
204
206
const jMax = intentsList [ i ] . patterns . length
@@ -354,51 +356,57 @@ class Agent {
354
356
}
355
357
}
356
358
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
+ }
376
379
}
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' ] ) ) ;
382
395
}
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' ] ) ;
390
396
}
391
397
}
398
+ results . shift ( ) ;
399
+ i ++ ;
392
400
}
393
- results . shift ( ) ;
394
- i ++ ;
395
401
}
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
+
402
410
}
403
411
}
404
412
0 commit comments