简体   繁体   English

Dialogflow V2 API - 如何传递上下文和/或有效负载 [PHP]

[英]Dialogflow V2 API - How to pass context and/or payload [PHP]

I am trying to send context and payload to the Dialogflow V2 API.我正在尝试将上下文和有效负载发送到 Dialogflow V2 API。 I am able to successfully send a queryString and get a response from my agent.我能够成功发送 queryString 并从我的代理那里获得响应。 However, I need to pass context and payload parameters with this query and I cannot seem to find ANY help on this for PHP.但是,我需要使用此查询传递上下文和有效负载参数,我似乎无法找到有关 PHP 的任何帮助。 Please see my code below.请在下面查看我的代码。 I am able to create the context object and the payload object (atleast I think its created), but how do I pass it to the API?我能够创建上下文 object 和有效负载 object(至少我认为它已创建),但是如何将其传递给 API?

Any help would be appreciated as I am very new to dialogflow and have been struggling with this for a few days now.任何帮助将不胜感激,因为我对 dialogflow 非常陌生,并且已经为此苦苦挣扎了几天。

function detect_intent_texts($projectId, $text, $sessionId, $context, $parameters, $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);

    $contextStruct = new Struct();
    $contextStruct->setFields($context['parameters']);
    $paramStruct = new Struct();
    $paramStruct->setFields($parameters['parameters']);

    $contextInput = new Context();
    $contextInput->setLifespanCount($context['lifespan']);
    $contextInput->setName($context['name']);
    $contextInput->setParameters($contextStruct);

    $queryParams = new QueryParameters();
    $queryParams->setPayload($paramStruct);

    // create query input
    $queryInput = new QueryInput();
    $queryInput->setText($textInput);

    // get response and relevant info
    $response = $sessionsClient->detectIntent($session, $queryInput); // 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;
}

Just as it happens, the moment I post my question, I get a result.正如它发生的那样,我发布问题的那一刻,我得到了一个结果。

Thanks to this post How to set query parameters dialogflow php sdk .感谢这篇文章如何设置查询参数对话框流 php sdk

I added the following to my code and it worked.我将以下内容添加到我的代码中并且它有效。

Added添加

$optionalsParams = ['queryParams' => $queryParams];

Changed改变了

$response = $sessionsClient->detectIntent($session, $queryInput, $optionalsParams);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM