Search code examples
php

Reply Depend on current and previous Status


I have this code tend to achieve this

If the user has previously asked about Nrat and then says "Thank you": The reply should respond with a specific Nrat-related response. If the user just says "Thank you" without previously asking about Nrat: The reply should respond with a general "Thank you" response.

but reply always triggers to General thanks even user ask previous about Nrat

function handleDialogyManagement( ) {
        global $lastIntent,$currentIntent,$checkcurrentStatus;
        $checkcurrentStatus=false;
        if($checkcurrentStatus){
            if($lastIntent === 'Nrat_Identity' || $lastIntent === 'Nrat_Office_Location'){
                $checkcurrentStatus=true;
                }else{
                    $checkcurrentStatus=false;
            }
         
        }
        
    
         if (($lastIntent === 'Nrat_Identity' || $lastIntent === 'Nrat_Office_Location') && $currentIntent === 'Thank_You') {
           
         
            // Generate a random index to select one of the predefined responses
            $randomIndex = rand(0, 2); // Adjust the range based on the number of responses
    
            // Predefined responses based on your requirements
            $responses = [
                "I'm happy to help about Nrat",
                
                "I'm grateful that my answer about Nrat"
        
               
            ];
    
            
            // Assign the selected response to the answer field in the response array
            $response['answer'] = $responses[$randomIndex];

            return $response;
        }

     
        if ( $currentIntent === 'Thank_You' && $checkcurrentStatus===false) {
            // Generate a random index to select one of the predefined responses
            $randomIndex = rand(0, 2); // Adjust the range based on the number of responses
    
          
            $responses = [
              "your welcome.",
"My pleasure",


            ];
    
            
            $response['answer'] = $responses[$randomIndex];

            return $response;
        }

    
            
    
    }
    
    

1-If the user has previously asked about Nrat and then says "Thank you": The reply should respond with a specific Nrat-related response.

2-If the user just says "Thank you" without previously asking about Nrat: The reply should respond with a general "Thank you" response.


Solution

  • Set last intent to array 1 credit to -(https://stackoverflow.com/users/6196568/shingo) comment

    if ($result->num_rows > 0) {
            $intents = [];
            while ($row = $result->fetch_assoc()) {
                $intents[] = $row['last_intent'];
            }
    
    
            $currentIntent = $intents[0];
            $lastIntent=$intents[1];
            
        } 
            
    }