没有nodejs库请求用户位置

Request user location without nodejs library

在 action-on-google nodejs 库的帮助下,我成功地能够请求用户位置,但我更像是 java 人,我需要在 [=42] 中执行此操作=].

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

从上面的链接中,我发现只需要 json 个回复是可能的。

我在 api.ai 中创建了我的 poc 应用程序,return 在 json 下回复

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"
                        ]
                    }
                }
            ]
        }
    ]
}

这个 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"
}

这个return: PLACEHOLDER TEXT

我想知道我这样做是否可行。如果是,我做错了什么?

请帮忙。

对于初学者 - 是的,这是可能的。您可以使用 JSON 来请求权限。所有 node.js 库所做的就是帮助格式化 JSON.

大部分看起来都是正确的,但我认为这里有两种不同类型的错误。

首先,我怀疑在您的第一个示例中 API.AI/Google 处有一些字段导致了问题。 conversationToken.stateexpectedInputs.inputPrompt.noInputPrompts 这两个空字段可能会导致问题。 state 字段应该只是一个字符串(任何字符串都可以),noInputPrompts 应该是一个空数组。

此外,您表示您正在使用 API.AI,这需要发送回其他信息,并且您在 data.google 部分中表示的大部分内容(您有在你的第二个例子中,但不是你的第一个)。有关 Google 助手使用的其他字段,请参阅 https://api.ai/docs/fulfillment#response for the base layout of an API.AI response and https://developers.google.com/actions/apiai/webhook#response

这是我生成的一些有效输出:


{
    "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"
                    ]
                }
            }
        }
    }
}