0

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;
    }
Sathesh Kumar
  • 359
  • 3
  • 8

1 Answers1

0

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];
Sathesh Kumar
  • 359
  • 3
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 08 '22 at 19:21