首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用API AI获取设备位置?

如何使用API AI获取设备位置?
EN

Stack Overflow用户
提问于 2016-12-23 09:07:31
回答 5查看 4.3K关注 0票数 8

我正在使用API AI提供的GUI工具来创建操作。是否可以获取设备位置?我听说这可以通过请求权限来实现。这有记录在案吗?示例/代码片段将非常有用。

EN

回答 5

Stack Overflow用户

发布于 2017-07-01 04:21:38

文档有点不清楚。我希望这能帮助到一些人。

您所要做的就是为您向其请求权限的意图创建子备用意图。

要添加后续意图,您需要单击您的意图上的“-

  1. intent”链接。请注意,该链接仅在您将鼠标悬停时才会显示。从下拉列表中选择回退选项,系统将为您创建子回退意图。

Screen shot for how to create a child fallback intent

  1. 单击子回退意图并启用"Use webhook“。

Screen shot for how to configure the child fallback intent

就这样。现在,一旦请求了权限,用户响应将通过在您的子回退意图中配置的操作回发给您。

请参阅下面的webhook示例代码。

代码语言:javascript
复制
'use strict';

const express = require('express')();
const router = require('express').Router();
const bodyParser = require('body-parser');
const ActionsSdkApp = require('actions-on-google').ActionsSdkApp;
const ApiAiApp = require('actions-on-google').ApiAiApp;

express.use(bodyParser.json({type: 'application/json'}));

// In aip.ai console, under Fulfillment set webhook url to
// https://[YOUR DOMAIN]/example/location
// don't forget to select "Enable webhook for all domains" for the DOMAIN field
router.post('/location', (req, res) => {
	const app = new ApiAiApp({request: req, response: res});
	const intent = app.getIntent();
	
	switch(intent){
		case 'input.welcome':
			// you are able to request for multiple permissions at once
			const permissions = [
				app.SupportedPermissions.NAME,
				app.SupportedPermissions.DEVICE_PRECISE_LOCATION
			];
			app.askForPermissions('Your own reason', permissions);
		break;
		case 'DefaultWelcomeIntent.DefaultWelcomeIntent-fallback':
			if (app.isPermissionGranted()) {
				// permissions granted.
				let displayName = app.getUserName().displayName;
				
				//NOTE: app.getDeviceLocation().address always return undefined for me. not sure if it is a bug.
				// 			app.getDeviceLocation().coordinates seems to return a correct values
				//			so i have to use node-geocoder to get the address out of the coordinates
				let coordinates = app.getDeviceLocation().address;
				
				app.tell('Hi ' + app.getUserName().givenName + '! Your address is ' + address);
			}else{
				// permissions are not granted. ask them one by one manually
				app.ask('Alright. Can you tell me you address please?');
			}
		break;
	}
});

express.use('/example', router);

express.listen('8081', function () {
  console.log('Example app is running')
})

票数 5
EN

Stack Overflow用户

发布于 2017-08-15 22:17:57

这包括三个部分:

请求permission

  • Telling API.AI处理API.AI

转发的信息的response

  • Handling

请求权限的

很多人都没有提到JSON body,它首先需要用来请求权限。这在actions > assistant > helpers > User information上是(很差的)文档,因为如果你使用的是API.AI,它并没有真正涵盖需要发送的内容。

简而言之,您的JSON需要如下所示:

代码语言:javascript
复制
{
    "speech": "PLACEHOLDER_FOR_PERMISSION",
    "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"
                    ]
                }
            }
        }
    }
}

告诉API.AI处理响应

API.AI需要知道哪个意图处理权限信息。您可以通过将事件设置为actions_intent_PERMISSION来完成此操作。

这看起来像这样:

您可以将Action设置为对您的webhook有意义的任何内容,并确保为意图启用webhook实现。

如果您需要跟踪权限请求的发起位置,并通过不同的Action进行处理,您可以设置上下文,并根据不同的上下文设置具有不同的处理意图。

回退意图方法之所以有效,是因为在这一点上没有更好的匹配,因为您没有使用actions_intent_PERMISSION指定常规意图。然而,这不是最好的选择,因为它可能会与用户的其他情况相匹配。

在您的webhook中获取信息

您的webhook将在正文中获得一个JSON对象。

用户名和相关信息将位于对象路径originalRequest.data.user.profile

他们的位置在originalRequest.data.device.location上提供。

有关参考,请参阅 Overview > Actions on Google lets you extend the functionality

票数 2
EN

Stack Overflow用户

发布于 2016-12-23 13:11:55

是的,在用户允许后,可以获取设备位置和其他一些信息(名称)。

by google已经很好地记录了它,所以我认为我没有必要在这里重复它。

如果问题是是否可以在没有通过a的实现调用的自定义webhook实现的情况下获取位置信息,那么答案是否定的: API.AI本身本身并不提供此功能,您需要使用a的扩展机制,即webhook。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41293884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档