Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

javascript - How do I get parameter value from arguments?

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"
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...