当前位置: 首页 > 面试题库 >

错误:未设置任何响应。适用于Google Assistant的操作的云功能

蒙华翰
2023-03-14
问题内容

我正在使用 Dialogflow*Cloud Functions 和新的 NodeJS Client Library V2
for Google on Actions 为Google Home 构建一个 助手
应用程序。实际上,我正在将用V1构建的旧代码迁移到V2。

***

上下文

我正在尝试使用两个单独的意图来获取用户的位置:(Request Permission触发/向用户发送许可请求的User Info意图)和(检查用户是否已授予许可,然后返回助手请求继续的数据的意图。

问题

问题在于,在V1上正常工作的同一代码在V2上无效。所以我不得不做一些重构。当我部署云功能时,我能够成功请求用户的许可,获取他的位置,然后使用外部库(geocode),我可以将latlong转换为人类可读的形式。但是由于某些原因(我认为它的承诺),我无法解析承诺对象并将其显示给用户

错误

我收到以下错误:

在此处输入图片说明

代码

以下是我的Cloud功能代码。我使用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);

很感谢任何形式的帮助。谢谢


问题答案:

您最后的猜测是正确的-问题是您没有使用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 );
  }
}


 类似资料:
  • 我发现Http 207可以用于批量插入/删除/。。。等等。从业务角度来看,我们可能会插入多个项目,并且部分成功。 我想知道如果我们有API Post调用一些外部服务,是否同样适用? 业务场景示例: 我们有正在创建订单的HttpPost终结点。 现在我们假设,除了创建订单之外,这个endpoint还将调用外部服务来打印该订单的标签,另一个endpoint将通过REST向外部第三方系统发送某种确认。

  • 问题内容: 在Java中是否存在用于集合操作的API,例如联合,交集,差,笛卡尔乘积,从一个集合到另一个集合的函数,这些函数的域限制和范围限制,.... Java? 请评论(操作)的覆盖范围和性能。 谢谢 问题答案: 是的,java 类。 通过Java SE教程: —如果s2是s1的子集,则返回true。(如果集合s1包含s2中的所有元素,则s2是s1的子集。) —将s1转换为s1和s2的并集。(

  • 我将按照此答案中的说明生成以下S3桶策略: 我返回以下错误: 操作不适用于语句中的任何资源 我的政策遗漏了什么?

  • 问题内容: 错误信息: 而我的代码: 并且my-form.html是: 如果我在其中放置一个while循环,它将永远加载。 然后输入比当前页面永久加载的更高的温度。如果我使用上面列出的当前代码,则会出现错误。 问题答案: 以下内容不返回响应: 你必须返回类似或的内容。 这样可以解决问题

  • java.lang.IllegalStateException:检测到回退配置错误:ERROR inistener.initialize: 21-没有适用于[max历史]的操作,当前ElementPath是[[配置][appender][max历史]]ERROR inistener.java:306: 21-没有适用于[max历史]的操作,当前ElementPath是[[配置][appender]

  • 我正在尝试将我的Spring启动应用程序“配置测试”连接到Springframework的云配置服务器。它不会选择应用程序的配置,而是回答一个错误并中止应用程序(由于fail fast选项): 连接到配置服务器时,会显示: 我已经在“引导”中定义了云配置服务器的URI。yml的属性文件。引导程序。yml是: 我有一个“application.properties”文件,用于在运行时启用配置客户端的