How do I get parameter value from arguments?

早过忘川 提交于 2019-12-11 16:59:22

问题


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?

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"
        }
      ]
    }
  ]
}

My code of Fulfillment side:

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"
}

Action Package action:

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

回答1:


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

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

This will return the rawText of the argument.

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

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

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.

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 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;
}



回答2:


If your 'newName' is a parameter defined in dialogflow, then you can easily access it with conv like this:

let newN = conv.parameters['newName'];


来源:https://stackoverflow.com/questions/50341054/how-do-i-get-parameter-value-from-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!