Search code examples
phpdialogflow-es

Dialogflow V2 API - How to pass context and/or payload


I am trying to send context and payload to the Dialogflow V2 API. I am able to successfully send a queryString and get a response from my agent. However, I need to pass context and payload parameters with this query and I cannot seem to find ANY help on this for PHP. Please see my code below. I have used this solution but not working Dialogflow V2 API - How to pass context and/or payload [PHP]

function detect_intent_texts($projectId, $text, $sessionId, $context, $queryParams, $languageCode = 'en-US') {
        // new session
        $test = array('credentials' => 'client-secret.json');
        $sessionsClient = new SessionsClient($test);
        $session = $sessionsClient->sessionName($projectId, $sessionId ?: uniqid());
        //printf('Session path: %s' . PHP_EOL, $session);
    
        // create text input
        $textInput = new TextInput();
        $textInput->setText($text);
        $textInput->setLanguageCode($languageCode);
    
        // create query input
        $queryInput = new QueryInput();
        $queryInput->setText($textInput);
    
    $optionalsParams = ['queryParams' => $queryParams];
        // get response and relevant info
        $response = $sessionsClient->detectIntent($session, $queryInput, $optionalsParams); // Here I don't know how to send the context and payload
        $responseId = $response->getResponseId();
        $queryResult = $response->getQueryResult();
        $queryText = $queryResult->getQueryText();
        $intent = $queryResult->getIntent();
        $displayName = $intent->getDisplayName();
        $confidence = $queryResult->getIntentDetectionConfidence();
        $fulfilmentText = $queryResult->getFulfillmentText();
    
        $returnResponse = array(
            'responseId' => $responseId,
            'fulfillmentText' => $fulfilmentText
        );
    
        $sessionsClient->close();
    
        return $returnResponse;
    }

Solution

  • I have found solution. According to this link.

    $contextkey = new Value();
    $contextkey->setStringValue($name);
    $resultData[‘name’] = $contextkey;
    
    $contextkey = new Value();
    $contextkey->setStringValue($mobile);
    $resultData[‘mobile’] = $contextkey;
    
    $contextStruct = new Struct();
    $contextStruct->setFields($resultData);
    $queryParam = new QueryParameters();
    $queryParam->setPayload($contextStruct);
    $optionalsParams = [‘queryParams’ => $queryParam];