Skip to content

Commit 8cecdad

Browse files
dialogflow: fix detect-intent-stream losing the query result (#2336)
The response stream doesn't always end with the query result. With output audio enabled the server sends the result first and then keeps streaming audio chunks, so looking at the last response alone died with "Call to a member function getQueryText() on null" and threw away the whole answer the caller asked for. Collect the result as the responses come in instead, and stop reading $response after the loop since it's undefined when the stream is empty.
1 parent 7476d33 commit 8cecdad

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

‎dialogflow/src/detect_intent_stream.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,23 @@ function detect_intent_stream($projectId, $path, $sessionId, $languageCode = 'en
7878
foreach ($requests as $request) {
7979
$stream->write($request);
8080
}
81+
82+
// The query result is not always on the last response. When output audio is
83+
// requested the server keeps streaming audio after it, so keep the last one
84+
// we see instead of only looking at the final response.
85+
$queryResult = null;
8186
foreach ($stream->closeWriteAndReadAll() as $response) {
8287
$recognitionResult = $response->getRecognitionResult();
8388
if ($recognitionResult) {
8489
$transcript = $recognitionResult->getTranscript();
8590
printf('Intermediate transcript: %s' . PHP_EOL, $transcript);
8691
}
92+
$queryResult = $response->getQueryResult() ?: $queryResult;
8793
}
8894

8995
// get final response and relevant info
90-
if ($response) {
96+
if ($queryResult) {
9197
print(str_repeat('=', 20) . PHP_EOL);
92-
$queryResult = $response->getQueryResult();
9398
$queryText = $queryResult->getQueryText();
9499
$intent = $queryResult->getIntent();
95100
$displayName = $intent->getDisplayName();

0 commit comments

Comments
 (0)