Dialogflow V2 API-如何传递上下文和/或有效负载[PHP] [英] Dialogflow V2 API - How to pass context and/or payload [PHP]
问题描述
我正在尝试将上下文和有效负载发送到Dialogflow V2 API。我能够成功发送queryString并从我的经纪人那里得到响应。但是,我需要在此查询中传递上下文和有效负载参数,因此似乎无法在PHP上找到任何帮助。请在下面查看我的代码。我能够创建上下文对象和有效负载对象(至少我认为已创建),但是如何将其传递给API?
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 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?
任何帮助都将不胜感激,因为我对dialogflow非常陌生,并且已经为此奋斗了几天。
Any help would be appreciated as I am very new to dialogflow and have been struggling with this for a few days now.
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.
感谢这篇文章如何设置查询参数dialogflow php sdk 。
我在代码中添加了以下内容,并且有效。
I added the following to my code and it worked.
已添加
$optionalsParams = ['queryParams' => $queryParams];
已更改
$response = $sessionsClient->detectIntent($session, $queryInput, $optionalsParams);
这篇关于Dialogflow V2 API-如何传递上下文和/或有效负载[PHP]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!