@@ -8,6 +8,7 @@ const { upperCaseFirst } = require("upper-case-first");
88
99const cors = require ( "cors" ) ;
1010const morgan = require ( "morgan" ) ;
11+ const dotenv = require ( "dotenv" ) ;
1112const express = require ( "express" ) ;
1213const compression = require ( "compression" ) ;
1314
@@ -16,14 +17,16 @@ const welcomeChat = require("./intents/Default_Welcome.json");
1617const fallbackChat = require ( "./intents/Default_Fallback.json" ) ;
1718const unitConverterChat = require ( "./intents/unit_converter.json" ) ;
1819
20+ dotenv . config ( ) ;
21+
1922const app = express ( ) ;
2023const port = process . env . PORT || 3000 ;
2124
2225const allQustions = [ ] ;
2326
2427for ( let i = 0 ; i < mainChat . length ; i ++ ) {
25- for ( let j = 0 ; j < mainChat [ i ] . qus . length ; j ++ ) {
26- allQustions . push ( mainChat [ i ] . qus [ j ] ) ;
28+ for ( let j = 0 ; j < mainChat [ i ] . questions . length ; j ++ ) {
29+ allQustions . push ( mainChat [ i ] . questions [ j ] ) ;
2730 }
2831}
2932
@@ -82,7 +85,8 @@ const sendAnswer = (req, res) => {
8285
8386 if ( regExforUnitConverter . test ( humanInput ) ) {
8487 const similarQuestionObj = stringSimilarity . findBestMatch ( humanInput , unitConverterChat ) . bestMatch ;
85- const valuesObj = extractValues ( humanInput , similarQuestionObj . target , {
88+ similarQuestion = similarQuestionObj . target ;
89+ const valuesObj = extractValues ( humanInput , similarQuestion , {
8690 delimiters : [ "{" , "}" ] ,
8791 } ) ;
8892
@@ -109,15 +113,16 @@ const sendAnswer = (req, res) => {
109113
110114 if ( similarQuestionRating > 0.5 ) {
111115 for ( let i = 0 ; i < mainChat . length ; i ++ ) {
112- for ( let j = 0 ; j < mainChat [ i ] . qus . length ; j ++ ) {
113- if ( similarQuestion == mainChat [ i ] . qus [ j ] ) {
114- responseText = _ . sample ( mainChat [ i ] . ans ) ;
116+ for ( let j = 0 ; j < mainChat [ i ] . questions . length ; j ++ ) {
117+ if ( similarQuestion == mainChat [ i ] . questions [ j ] ) {
118+ responseText = _ . sample ( mainChat [ i ] . answers ) ;
115119 rating = similarQuestionRating ;
116120 }
117121 }
118122 }
119123 } else {
120124 isFallback = true ;
125+ action = "Default_Fallback" ;
121126 if ( humanInput . length <= 20 && ! / ( \s { 1 , } ) / gmi. test ( humanInput ) ) {
122127 responseText = "You are probably hitting random keys :D" ;
123128 } else {
@@ -135,8 +140,12 @@ const sendAnswer = (req, res) => {
135140 similarQuestion,
136141 } ) ;
137142 } catch ( error ) {
138- res . status ( 500 ) . send ( { error : "Internal Server Error!" , code : 500 } ) ;
139143 console . log ( error ) ;
144+ if ( ( error . message ) . includes ( "URI" ) ) {
145+ res . status ( 500 ) . send ( { error : error . message , code : 500 } ) ;
146+ } else {
147+ res . status ( 500 ) . send ( { error : "Internal Server Error!" , code : 500 } ) ;
148+ }
140149 }
141150} ;
142151
0 commit comments