【问题标题】:Dialogflow V2 API - How to pass context and/or payload [PHP]Dialogflow V2 API - 如何传递上下文和/或有效负载 [PHP]
【发布时间】:2020-02-21 17:31:36
【问题描述】:

我正在尝试将上下文和有效负载发送到 Dialogflow V2 API。我能够成功发送 queryString 并从我的代理那里获得响应。但是,我需要使用此查询传递上下文和有效负载参数,我似乎找不到任何关于 PHP 的帮助。请在下面查看我的代码。我能够创建上下文对象和有效负载对象(至少我认为它已创建),但是如何将它传递给 API?

任何帮助都将不胜感激,因为我对 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;
}

【问题讨论】:

    标签: php dialogflow-es


    【解决方案1】:

    正如它发生的那样,我发布问题的那一刻,我得到了一个结果。

    感谢这篇帖子How to set query parameters dialogflow php sdk

    我将以下内容添加到我的代码中并且它有效。

    已添加

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

    已更改

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      相关资源
      最近更新 更多