'Dialogflow v2 android sdk - Add QueryParameters to QueryInput

i'm actually trying to use Dialogflow v2 with java sdk in android.

I can already make queries to dialogflow but I wanted to send some payload with my query. Because of this, I discovered the QueryParameters object and I already set it with my desired payload.

The problem is, I have both QueryInput and QueryParameters defined but there is no documentation (or code references in the source code) about how to apply the parameters to the input before trying to detect the intent.

SessionsClient.create().use { sessionsClient ->
            // Set the session name using the sessionId (UUID) and projectID
            val session = SessionName.of(PROJECT_ID, UUID)

            System.out.println("Session Path: $session")

            // Set the text (input) and language code (en) for the query
            val textInput = TextInput.newBuilder().setText(text).setLanguageCode(LANGUAGE_CODE)

            // Build the query with the TextInput
            val queryInput = QueryInput.newBuilder().setText(textInput).build()

            // Set payload
            val payload = "{someid: $someid}"
            val queryParameters = QueryParameters.newBuilder().setPayload(Struct.parseFrom(payload.toByteArray())).build()

            // (HERE I NEED TO ADD THE PARAMETERS TO INPUT)


            // Performs the detect intent request
            val response = sessionsClient.detectIntent(session, queryInput)

            // returns the query result
            return response.queryResult
        }


Solution 1:[1]

After hours of deep searching, I managed to find the DetectIntentRequest class.

            //Build the request
            val request = DetectIntentRequest.newBuilder()
                .setSession(session.toString())
                .setQueryInput(queryInput)
                .setQueryParams(queryParameters)
                .build()

            // Performs the detect intent request
            val response = sessionsClient.detectIntent(request)

Note: if you are using other languages probably you 'll have a third parameter in detectIntent function to place your queryParameters

Solution 2:[2]

For Kotlin ,

val queryInput1211 = QueryInput.newBuilder()
            .setText(TextInput.newBuilder().setText(msg).setLanguageCode("en-US"))
            .setEvent(com.google.cloud.dialogflow.v2beta1.EventInput.newBuilder()
                    .setName("Welcome")
                    .setLanguageCode("en-US")
                    .setParameters(
                            Struct.newBuilder().putFields("user_id",
                                    Value.newBuilder().setStringValue("1234").build())
                                    .build())
                    .build())
            .build()

For java

QueryInput queryInput1211 = QueryInput.newBuilder()
                    .setText(TextInput.newBuilder().setText(msg).setLanguageCode("en-US"))
                    .setEvent(com.google.cloud.dialogflow.v2beta1.EventInput.newBuilder()
                            .setName("Welcome")
                            .setLanguageCode("en-US")
                            .setParameters(
                                    Struct.newBuilder().putFields("user_id",
                                            Value.newBuilder().setStringValue("1234").build())
                                            .build())
                            .build())
                    .build();

Where Welcome is intent event name and user_id as parameter name

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BLDD
Solution 2 Subin Babu