All*_*lJs 8 javascript asynchronous node.js google-cloud-functions actions-on-google
我正在为Google Home 构建一个助手应用程序,使用Dialogflow,云功能和新的NodeJS Client Library V2 for Google.事实上,我正在将使用V1构建的旧代码迁移到V2.
上下文
我试图使用两个单独的意图来获取用户的位置:( Request Permission向用户触发/发送权限请求的User Info意图)和(检查用户是否授予权限的Intent,然后返回助手请求的数据以继续.
问题
问题是在V1上运行良好的相同代码不适用于V2.所以我不得不做一些重构.当我部署我的云功能时,我能够成功请求用户的权限,获取他的位置,然后使用外部库(geocode),我可以将latlong转换为人类可读的形式.但由于某些原因(我认为它的承诺)我无法解决promise对象并将其显示给用户
错误
我收到以下错误:
代码
以下是我的云功能代码.我已经尝试过这个代码的多个版本,使用request库,https库等.没有运气......没有运气
const {dialogflow, Suggestions,SimpleResponse,Permission} = require('actions-on-google')
const functions = require('firebase-functions');
const geocoder = require('geocoder');
const app = dialogflow({ debug: true });
app.middleware((conv) => {
conv.hasScreen =
conv.surface.capabilities.has('actions.capability.SCREEN_OUTPUT');
conv.hasAudioPlayback =
conv.surface.capabilities.has('actions.capability.AUDIO_OUTPUT');
});
function requestPermission(conv) {
conv.ask(new Permission({
context: 'To know who and where you are',
permissions: ['NAME','DEVICE_PRECISE_LOCATION']
}));
}
function userInfo ( conv, params, granted) {
if (!conv.arguments.get('PERMISSION')) {
// Note: Currently, precise locaton only returns lat/lng coordinates on phones and lat/lng coordinates
// and a geocoded address on voice-activated speakers.
// Coarse location only works on voice-activated speakers.
conv.ask(new SimpleResponse({
speech:'Sorry, I could not find you',
text: 'Sorry, I could not find you'
}))
conv.ask(new Suggestions(['Locate Me', 'Back to Menu',' Quit']))
}
if (conv.arguments.get('PERMISSION')) {
const permission = conv.arguments.get('PERMISSION'); // also retrievable with explicit arguments.get
console.log('User: ' + conv.user)
console.log('PERMISSION: ' + permission)
const location = conv.device.location.coordinates
console.log('Location ' + JSON.stringify(location))
// Reverse Geocoding
geocoder.reverseGeocode(location.latitude,location.longitude,(err,data) => {
if (err) {
console.log(err)
}
// console.log('geocoded: ' + JSON.stringify(data))
console.log('geocoded: ' + JSON.stringify(data.results[0].formatted_address))
conv.ask(new SimpleResponse({
speech:'You currently at ' + data.results[0].formatted_address + '. What would you like to do now?',
text: 'You currently at ' + data.results[0].formatted_address + '.'
}))
conv.ask(new Suggestions(['Back to Menu', 'Learn More', 'Quit']))
})
}
}
app.intent('Request Permission', requestPermission);
app.intent('User Info', userInfo);
exports.myCloudFunction = functions.https.onRequest(app);
Run Code Online (Sandbox Code Playgroud)
很感谢任何形式的帮助.谢谢
Pri*_*ner 10
你的最后一次猜测是正确的 - 你的问题是你没有使用Promises.
app.intent()期望处理函数(userInfo在您的情况下)如果使用异步调用则返回Promise.(如果你不是,你就可以逃避任何回报.)
正常的行动方式是使用返回Promise的东西.但是,这在您的情况下是棘手的,因为地理编码库尚未更新为使用Promises,并且您在userInfo函数中还有其他代码没有返回任何内容.
在这种情况下重写可能看起来像这样(但我没有测试过代码).在其中,我将这两个条件userInfo分解为另外两个函数,因此可以返回一个Promise.
function userInfoNotFound( conv, params, granted ){
// Note: Currently, precise locaton only returns lat/lng coordinates on phones and lat/lng coordinates
// and a geocoded address on voice-activated speakers.
// Coarse location only works on voice-activated speakers.
conv.ask(new SimpleResponse({
speech:'Sorry, I could not find you',
text: 'Sorry, I could not find you'
}))
conv.ask(new Suggestions(['Locate Me', 'Back to Menu',' Quit']))
}
function userInfoFound( conv, params, granted ){
const permission = conv.arguments.get('PERMISSION'); // also retrievable with explicit arguments.get
console.log('User: ' + conv.user)
console.log('PERMISSION: ' + permission)
const location = conv.device.location.coordinates
console.log('Location ' + JSON.stringify(location))
return new Promise( function( resolve, reject ){
// Reverse Geocoding
geocoder.reverseGeocode(location.latitude,location.longitude,(err,data) => {
if (err) {
console.log(err)
reject( err );
} else {
// console.log('geocoded: ' + JSON.stringify(data))
console.log('geocoded: ' + JSON.stringify(data.results[0].formatted_address))
conv.ask(new SimpleResponse({
speech:'You currently at ' + data.results[0].formatted_address + '. What would you like to do now?',
text: 'You currently at ' + data.results[0].formatted_address + '.'
}))
conv.ask(new Suggestions(['Back to Menu', 'Learn More', 'Quit']))
resolve()
}
})
});
}
function userInfo ( conv, params, granted) {
if (conv.arguments.get('PERMISSION')) {
return userInfoFound( conv, params, granted );
} else {
return userInfoNotFound( conv, params, granted );
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3421 次 |
| 最近记录: |