如何使用API​​ AI获取设备位置? [英] How to fetch device location using API AI?

查看:66
本文介绍了如何使用API​​ AI获取设备位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using GUI tools provided by API AI to create Actions. Is it possible to fetch device location? I have heard that this is possible by requesting permissions. Is this documented anywhere? An example/code snippet will be very useful.

推荐答案

文档有点不清楚。我希望这可以对某人有所帮助。

The documentation is a bit unclear. I hope this can help someone.

您要做的就是为您请求权限的意图创建一个子级后备意图。

All you have to do is create a child fallback intent for the intent you are requesting permissions from.


  1. 要执行此操作,您需要单击意图上的添加后续意图链接。请注意,该链接仅在您将其悬停时才会显示。从下拉列表中选择后备选项,将为您创建子后备意图。

该屏幕快照介绍了如何创建子级后备意图


  1. 点击子级后备意图并启用使用webhook。

如何配置子级后备广告意图的屏幕截图

就是这样。现在,一旦请求了权限,用户响应就会以您的孩子后备意图中配置的操作回发给您。

That's it. Now once the permission is requested, the user response will be post back to you with the action which is configured in your child fallback intent.

请参阅以下Webhook示例代码。

See below for the webhook sample code.

'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')
})

这篇关于如何使用API​​ AI获取设备位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆