Sukh
Sukh

Reputation: 670

Request user location without nodejs library

I was successfully able to request user location with help of action-on-google nodejs library, but I am more of java guy, and I need to do this in java.

https://developers.google.com/actions/assistant/helpers#json

How can I request the user's location in API.ai?

Requesting User Location from Google Actions with Api.ai

From above links, I find out that it is possible with just json reponses.

I've created my poc app in api.ai, which returns below json reponses

1

{
    "conversationToken": {
        "state": null,
        "data": {}
    },
    "expectUserResponse": true,
    "expectedInputs": [
        {
            "inputPrompt": {
                "initialPrompts": [
                    {
                        "textToSpeech": "PLACEHOLDER_FOR_PERMISSION"
                    }
                ],
                "noInputPrompts": null
            },
            "possibleIntents": [
                {
                    "intent": "actions.intent.PERMISSION",
                    "inputValueData": {
                        "@type": "type.googleapis.com/google.actions.v2.PermissionValueSpec",
                        "optContext": "Requesting Location.",
                        "permissions": [
                            "DEVICE_COARSE_LOCATION"
                        ]
                    }
                }
            ]
        }
    ]
}

This returns : Unparseable Json Response

2

{
    "contextOut": [
        {
            "lifespan": 100,
            "name": "_actions_on_google_",
            "parameters": null
        },
        {
            "lifespan": 5,
            "name": "requesting_permission",
            "parameters": null
        }
    ],
    "data": {
        "google": {
            "expect_user_response": true,
            "is_ssml": false,
            "no_input_prompts": null,
            "permissions_request": {
                "opt_context": "Requesting Location.",
                "permissions": [
                    "DEVICE_COARSE_LOCATION"
                ]
            }
        }
    },
    "speech": "PLACEHOLDER_FOR_PERMISSION"
}

This return : PLACEHOLDER TEXT

I wanted to know that way I am doing it is possible or not. If yes, what I am doing wrong ?

Please help.

Upvotes: 3

Views: 317

Answers (1)

Prisoner
Prisoner

Reputation: 50701

For starters - yes, this is possible. You can use JSON to request the permission. All the node.js libraries do is help format the JSON.

Most of this looks correct, but I think there are two different types of errors here.

The first is that I suspect there are a few fields that are causing problems at API.AI/Google in your first example. The two fields that are null, conversationToken.state and expectedInputs.inputPrompt.noInputPrompts, are likely causing problems. The state field should just be a string (any string is fine) and the noInputPrompts should be an empty array.

Furthermore, you indicate that you're using API.AI, which requires additional information to be sent back and most of what you've indicated to be in a data.google section (which you have in your second example, but not your first). See https://api.ai/docs/fulfillment#response for the base layout of an API.AI response and https://developers.google.com/actions/apiai/webhook#response for the additional fields used by the Google Assistant.

Here is some output that I generated that works:


{
    "speech": "PLACEHOLDER_FOR_PERMISSION",
    "contextOut": [
        {
            "name": "_actions_on_google_",
            "lifespan": 100,
            "parameters": {}
        }
    ],
    "data": {
        "google": {
            "expectUserResponse": true,
            "isSsml": false,
            "noInputPrompts": [],
            "systemIntent": {
                "intent": "actions.intent.PERMISSION",
                "data": {
                    "@type": "type.googleapis.com/google.actions.v2.PermissionValueSpec",
                    "optContext": "To pick you up",
                    "permissions": [
                        "NAME",
                        "DEVICE_PRECISE_LOCATION"
                    ]
                }
            }
        }
    }
}

Upvotes: 1

Related Questions