我将如何要求用户提供姓名列表?

Que*_*son 7 javascript alexa alexa-skills-kit

我正在创建自定义的Alexa技能,它需要收集用户所说的未知数量的名称。

我试图将名称存储在插槽中。我能够以这种方式获得一个名字,但不能多个。现在,我正在尝试向用户询问人数,然后询问用户名称。但是,我不知道如何使该解决方案起作用。另外,我试图将名称存储在会话属性中。

这是我到目前为止所拥有的

    // Api call wrapped into a promise. Returns the person's email.
    return findEmployee(sessionAttributes.client, givenName)
        .then(attendee => {
            let prompt = ''
            if (attendee.value.length === 1) {
            sessionAttributes.attendees = [...sessionAttributes.attendees, attendee.value[0]]
            prompt = `${attendee.value.displayName} added to the meeting.`
            return handlerInput.responseBuilder
                .speak(prompt)
                .reprompt(prompt)
                .getResponse()
            }
         })
         .catch(err => console.log(err))
Run Code Online (Sandbox Code Playgroud)

这个代码片段可以与一个人一起正常工作,但是我将如何重构它,以便Alexa会询问直到达到最终条件。

Que*_*son 1

经过一些研究后,我发现答案实际上很简单。为了收集我的名字,我需要循环一个意图,直到满足特定条件。我可以通过检查“canHandle”函数中的技能状态并在响应中使用 if 语句来做到这一点。

假设我有一个名为 number 的插槽,它被设置为随机数。

const AddNameHandler = {
  canHandle (handlerInput) {
    const request = handlerInput.requestEnvelope.request
    const attributesManager = handlerInput.attributesManager
    const sessionAttributes = attributesManager.getSessionAttributes()
    const slots = request.intent.slots

    return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
      (sessionAttributes.names < slots.number)

  },
  handle (handlerInput) {
    const request = handlerInput.requestEnvelope.request
    const attributesManager = handlerInput.attributesManager
    const sessionAttributes = attributesManager.getSessionAttributes()
    const slots = request.intent.slots
    // Collect the name
    sessionAttributes.names += 1
    if (sessionAttributes.names !== slots.number) {
      handlerInput.responseBuilder
        .speak('Say another name.')
        .reprompt('Say another name')
        .getResponse()
    } else {
      handlerInput.responseBuilder
        .speak('Got the names.')
        .getResponse()
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

此示例将收集名称列表,如果我想在达到名称限制时触发另一个处理程序,我只需要使用新条件创建另一个处理程序。