detectIntent method

Future<DetectIntentResponse> detectIntent({
  1. QueryParameters? queryParams,
  2. required QueryInput queryInput,
  3. OutputAudioConfig? audioConfig,
})

Processes a natural language query and returns structured, actionable data as a result.

This method is not idempotent, because it may cause contexts and session entity types to be updated, which in turn might affect results of future queries.

Implementation

Future<DetectIntentResponse> detectIntent({
  QueryParameters? queryParams,
  required QueryInput queryInput,
  OutputAudioConfig? audioConfig,
}) async {
  if (_client == null) await _updateHttpClient();

  final body = HttpUtil.getBody(
    queryParams: queryParams,
    queryInput: queryInput,
    audioConfig: audioConfig,
  );

  final uri = Uri.parse(
    '$kDialogFlowUrl/$kDialogFlowApiVersion/'
    '${HttpUtil.getFormatedSession(projectId, sessionId)}:detectIntent',
  );

  final Response response = await _client!.post(
    uri,
    body: jsonEncode(body),
  );

  if (!HttpUtil.isValidStatusCode(response.statusCode)) {
    final _json = jsonDecode(response.body)["error"];
    throw Exception(
      "${_json['status']}: ${_json['message']}, (${_json['code']})",
    );
  }

  Map<String, dynamic> json = await jsonDecode(response.body);
  return DetectIntentResponse.fromJson(json);
}