【问题标题】:How would I ask the user for a list of names?我将如何要求用户提供姓名列表?
【发布时间】:2019-09-14 03:24:00
【问题描述】:

我正在创建一个自定义 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))

这个 sn-p 与一个人一起工作很好,但我将如何重构它以便 Alexa 会询问直到达到结束条件。

【问题讨论】:

标签: javascript alexa alexa-skills-kit


【解决方案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()
    }
  }
}

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

【讨论】:

    猜你喜欢
    • 2019-05-27
    • 2022-08-03
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多