How to fetch device location using API AI?

后端 未结 5 1031
情歌与酒
情歌与酒 2021-01-05 00:56

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 doc

5条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 01:28

    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. To do so you need to click on the "Add follow-up intent" link on your intent. Note that the link will only appear when you hover it. Select the fallback option from the dropdown list and a child fallback intent will be created for you.

    Screen shot for how to create a child fallback intent

    1. Click into the child fallback intent and enable "Use webhook".

    Screen shot for how to configure the child fallback intent

    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.

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

提交回复
热议问题