首页 > 解决方案 > 如何使用 Google action conv.ask 获取用户的响应并转移到流程

问题描述

我想尝试使用 Google Actions 节点 js SDK 在响应用户之前询问用户更多信息,这样我就可以继续下一个过程。我尝试使用conv.ask等待用户响应,但是,当用户响应时,它只会返回actions.intent.TEXT而不是 intent com.example.test.DO。我应该如何实现这一目标?

app.intent('com.example.test.WHAT', (conv, input, arg) => {
  conv.ask('Sure! What do you want me to help with');
})

app.intent('com.example.test.DO', (conv, input, arg) => {
  //process the user response here from com.example.test.WHAT
  conv.close('I have finished it');
})

app.intent('actions.intent.TEXT', (conv, input) => {
  if (input === 'bye' || input === 'goodbye') {
    return conv.close('See you later!')
  }
  conv.ask(`I didn't understand. Can you tell me something else?`)
})

动作包 JSON:

{
      "name": "DO",
      "intent": {
        "name": "com.example.test.DO",
        "trigger": {
          "queryPatterns": [
            "stop streaming"
          ]
        }
      },
      "fulfillment": {
        "conversationName": "example-test"
      }
    },
    {
      "name": "WHAT",
      "intent": {
        "name": "com.example.test.WHAT",
        "trigger": {
          "queryPatterns": [
            "help me"
          ]
        }
      }

我在模拟器上的问题:

Me: Talk to myTestExample to help me

Google: Sure! What do you want me to help with?

ME: Play with me

Google: I didn't understand. Can you tell me something else?

我希望它去com.example.test.DO而不是actions.intent.TEXT. 或com.example.test.WHAT在得到用户响应后执行内部处理。

更新:

我试图在里面创建一个全局类型变量和 switch case action.intent.Text

var type;

app.intent('actions.intent.TEXT', (conv, input) => {
  if (input === 'bye' || input === 'goodbye' || input === 'stop') {
    return conv.close('See you later!')
  }
  switch (type){
          case 'WHAT':
             //use the new input data to process sth here
             return conv.close('I will help you to do this');
          case 'HOW':
             //use the new input data to process sth here
             return conv.close('I will use this method to help with you');
     }
   conv.ask(`I didn't understand. Can you tell me something else?`)
}

app.intent('com.example.test.WHAT', (conv, input, arg) => {
     type = 'WHAT';
     conv.ask('Sure! What do you want me to help with');
})

app.intent('com.example.test.HOW', (conv, input, arg) => {
    type = 'WHAT';
    conv.ask('Sure! How do you want me to help with');
})

我认为这不是一个理想的解决方案,因为当多个设备使用我的履行服务器时会导致问题。是否可以在同一意图功能上询问和使用用户响应而不是actions.intent.TEXT使用conv.ask

标签: javascriptnode.jsactions-on-google

解决方案


使用 actions.json 文件中的意图有两个原因:

  1. 设置欢迎意图和深层链接短语的模式。

  2. 稍后在对话中塑造可能的用户响应。

然而,在情况 (2) 中,意图仍被报告为actions.intent.TEXT。您将永远不会使用 Action SDK 向您报告您定义的其他意图。所以它永远不会发送com.example.test.WHATorHOW意图。

如果您使用自己的自然语言处理 (NLP) 系统,这正是您想要的 - 只是用户发送的文本。

如果您想要为您进行 NLP 处理并管理状态的东西,您可能希望查看类似 Dialogflow 的东西。Dialogflow 允许您设置将与意图匹配的短语,并在您的 webhook 实现中向您发送匹配的意图以及用户所说的参数。


推荐阅读