如何从参数获取参数值? [英] How do I get parameter value from arguments?

查看:234
本文介绍了如何从参数获取参数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Google动作的参数值传递给我的履约. 但是,我无法仅获取参数值.如何从参数中仅在newName字段下获得"Asda"?我是否必须从conv(如conv.inputs.arguments[1].rawText)中提取它?如果是这样,那么为参数命名的目的是什么?

I am trying to pass parameter value from google actions to my fulfillment. However, I am not able to get only the parameter value. How should I able to get only "Asda" under newName field from the argument? Do I have to extract it from conv (like conv.inputs.arguments[1].rawText)? if like this, then what is the purpose of having a name for the parameter?

从Google Actions请求JSON:

Request JSON from Google Actions:

{
  "user": {
    "userId": "ABwppHEAPgcgb2yFUFURYFEJGg4VdAVcL9UKO9cS7a7rVfAMr9ht67LzgrmMseTvb5mmJjbjj7UV",
    "locale": "en-US",
    "lastSeen": "2018-05-15T01:08:55Z",
    "userStorage": "{\"data\":{}}"
  },
  "conversation": {
    "conversationId": "1526346570079",
    "type": "NEW"
  },
  "inputs": [
    {
      "intent": "com.example.test.RENAME",
      "rawInputs": [
        {
          "inputType": "KEYBOARD",
          "query": "Talk to GoTestApp to rename Asda"
        }
      ],
      "arguments": [
        {
          "name": "trigger_query",
          "rawText": "rename Asda",
          "textValue": "rename Asda"
        },
        {
          "name": "newName",
          "rawText": "Asda",
          "textValue": "Asda"
        }
      ]
    }
  ],
  "surface": {
    "capabilities": [
      {
        "name": "actions.capability.MEDIA_RESPONSE_AUDIO"
      },
      {
        "name": "actions.capability.SCREEN_OUTPUT"
      },
      {
        "name": "actions.capability.AUDIO_OUTPUT"
      },
      {
        "name": "actions.capability.WEB_BROWSER"
      }
    ]
  },
  "isInSandbox": true,
  "availableSurfaces": [
    {
      "capabilities": [
        {
          "name": "actions.capability.SCREEN_OUTPUT"
        },
        {
          "name": "actions.capability.AUDIO_OUTPUT"
        }
      ]
    }
  ]
}

我的实现代码:

app.intent('com.example.test.RENAME', (conv, input, arg) => {
  console.log(input); //print Talk to GoTestApp to rename Asda
  console.log(arg); //only print "rename Asda"
  console.log(arg[1]) //only print "e"
}

动作包动作:

  "name": "RENAME",
  "intent": {
    "name": "com.example.test.RENAME",
    "parameters": [{
      "name": "newName",
      "type": "SchemaOrg_Text"
    }],
    "trigger": {
      "queryPatterns": [
        "rename $SchemaOrg_Text:newName"
      ]
    }
  },
  "fulfillment": {
    "conversationName": "example"
  }
}

推荐答案

好像您想获得

Looks like you want to get an Actions SDK argument by name, which then you can use:

const newName = conv.arguments.get('newName')

这将返回参数的rawText.

参数检索非常复杂,因此实际上有4种方法可以按名称或按列表检索参数.

Argument retrieval is pretty complicated so there's actually 4 ways to retrieve arguments either by name or by list.

conv.arguments.get (这是conv.arguments.parsed.get的快捷方式)将通过名称返回自定义属性(不包括namestatus)并返回具有最后优先级的textValue来获取参数.

conv.arguments.get (which is a shortcut for conv.arguments.parsed.get) will get an argument by name returning the inferred property excluding name and status and returning textValue with last priority.

conv.arguments.status.get可用于按名称获取status自变量.

conv.arguments.status.get can be used to get a status argument by name.

最常见的用例是只有一个参数时,因此有一种捷径,可以通过您提到的意图处理程序来检索单个参数.

The most common use case is when there is only one argument, so there's a shortcut for retrieving the single argument via the intent handler like you mentioned.

app.intent('intent.name', (conv, input, arg, status) => {
  console.log(arg); // print the parsed single argument like `rawText` if exists
  console.log(status) // print the single argument status if exists
}

要按名称获取原始参数而不进行任何推断的解析,可以使用conv.arguments.raw.get.然后,您可以自己手动提取诸如rawTexttextValue的属性.

To get the raw argument by name without doing any inferred parsing, you can use conv.arguments.raw.get. Then you can manually extract out the properties like rawText or textValue yourself.

对于这些参数的每个获取器,都有一种相应的方法可以按顺序使用数组进行检索:conv.arguments.parsed.listconv.arguments.status.listconv.arguments.raw.list.

With each of these getters for arguments, there's a corresponding way to retrieve by order with an array: conv.arguments.parsed.list, conv.arguments.status.list, and conv.arguments.raw.list.

conv.arguments还可对原始值进行迭代,因此,如果要遍历原始参数,只需执行以下操作:

conv.arguments is also iterable for raw values, so if you want to loop through the raw arguments, just do:

for (const arg of conv.arguments) {
  const value = arg.rawText;
}

这篇关于如何从参数获取参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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