2017-08-02 34 views
0

我想創建一個抽獎技巧,它需要用戶輸入6個數字。 我目前正在通過樣本和開發人員指南進行學習,並且我可以閱讀指南並獲得一項工作技能,它將接受一個輸入並結束會話。但我相信我需要創建一個對話框,這是我卡住的地方。向Alexa提供一個數字列表

設計角度看,我想的對話框是這樣的: Alexa的:請提供第一個數字 用戶:1 Alexa的:現在的第二... 用戶:2 等等等等

但我認爲這將是確定的,如果它是這樣的: Alexa的:請調出6個號碼 用戶:1,2,3,4,5,6

這甚至可能嗎?我是否需要創建一個名爲「數字」的自定義插槽類型,然後輸入數字,例如1-50或任何限制?

最好的情況是,我現在可以請求一個號碼,所以它真的是我堅持的對話交互。有沒有人甚至做過這樣的事情?

謝謝。

回答

1

是的,這兩個問題。您可以將響應與6個不同的自定義插槽串聯起來。 「用戶:我的號碼是{num1},{num2},{num3},{num4},{num5},{num6}」,並使用技能測試開發人員完成所有必要的工作。然而,如果用戶沒有恰當地回答他們的答案,那麼這將是一個相當不好的用戶體驗,Alexa必須要求跟進問題以獲得每個號碼。最後一個問題是,雖然自定義廣告位可以定義爲包含數字1-50,但alexa通常會識別與自定義廣告位中提供的值類似的值,例如50-99之間的數字。然後由您來檢查您收到的值是否在1到50之間。如果不是,您會希望要求用戶在適當的範圍內提供不同的數字。

結論:您將希望進行單獨的交互,用戶一次只能提供一個號碼。

Alexa:"you will be prompted for 6 numbers between 1 and 50 please state them one at a time. Choose your first number." 
User:"50" 
Alexa:"Your First number is 50, Next number."... 

您可以使用單一意圖來實現此目的。讓我們將其命名爲GetNumberIntent。 GetNumberIntent將具有樣品uterances沿

{number} 
pick {number} 
choose {number} 

行其中{數}爲一個自定義的時隙類型或者簡單地AMAZON.NUMBER。然後由您來檢查數字是在1到50之間。

我使用SDK在Node.js中編程。您的實施可能因您的語言選擇而異。

我會做的是定義6個不同的狀態處理程序。每個處理程序應該有GetNumberIntent。如果槽值適當,則返回GetNumberIntent將該值存儲到會話數據和/或dynamodb並前進到下一個狀態。如果時隙值無效逗留例如在狀態「NumberInputFiveStateHandlers」直到接收到好的值,則改變狀態到下一個「NumberInputSixStateHandlers」

var NumberInputFiveStateHandlers = Alexa.CreateStateHandler(states.NUMFIVEMODE, { 
    'NewSession': function() { 
     this.emit('NewSession'); // Uses the handler in newSessionHandlers 
    }, 
    //Primary Intents 
    'GetNumberIntent': function() { 
     let message = ` `; 
     let reprompt = ` `; 
     let slotValue = this.event.request.intent.slots.number.value; 
     if(parseInt(slotValue) >= 1 && parseInt(slotValue) <= 50){ 
      this.handler.state = states.NUMSIXMODE; 
      this.attributes['NUMBERFIVE'] = this.event.request.intent.slots.number.value; 
      message = ` Your fifth number is `+slotValue+`. please select your sixth value. `; 
      reprompt = ` please select your sixth value. `; 
     }else{ 
      message = ` The number `+slotValue)+` is not in the desired range between 1 and 50. please select a valid fifth number. `; 
      reprompt = ` please select your fifth value. `; 
     } 
     this.emit(':ask',message,reprompt); 
    }, 
    //Help Intents 
    "InformationIntent": function() { 
     console.log("INFORMATION"); 
     var message = ` You've been asked to choose a lottery number between 1 and 50. Please say your selection.`; 
     this.emit(':ask', message, message); 
    }, 
    "AMAZON.StopIntent": function() { 
     console.log("STOPINTENT"); 
     this.emit(':tell', "Goodbye!"); 
    }, 
    "AMAZON.CancelIntent": function() { 
     console.log("CANCELINTENT"); 
     this.emit(':tell', "Goodbye!"); 
    }, 
    'AMAZON.HelpIntent': function() { 
     var message = `You're playing lottery. you'll be picking six numbers to play the game. For help with your current situation say Information. otherwise you may exit the game by saying quit.`; 
     this.emit(':ask', message, message); 
    }, 
    //Unhandled 
    'Unhandled': function() { 
     console.log("UNHANDLED"); 
     var reprompt = ' That was not an appropriate response. Please say a number between 1 and 50.'; 
     this.emit(':ask', reprompt, reprompt); 
    } 
}); 

這是第五請求的一個例子。你將有6個完全相同的狀態,就像這個串起來的狀態。最終你會得到6個會話值。

this.attributes['NUMBERONE'] 
this.attributes['NUMBERTWO'] 
this.attributes['NUMBERTHREE'] 
this.attributes['NUMBERFOUR'] 
this.attributes['NUMBERFIVE'] 
this.attributes['NUMBERSIX'] 

然後,您可以將這些值用於您的遊戲。


如果你還沒有使用Alexa的-SDK之前,你必須記住註冊您的狀態處理程序,並添加您的模式到states變量。

alexa.registerHandlers(newSessionHandlers, NumberInputOneStateHandlers, ... NumberInputSixStateHandlers); 

var states = { 
    NUMONEMODE: '_NUMONEMODE', 
    ... 
    ... 
    NUMSIXMODE: '_NUMSIXMODE', 
} 

此答案無意涵蓋使用Alexas-SDK進行編碼的基礎知識。還有其他資源可以爲這個主題提供更具體的問題。


可替換地,因爲您的目的是相同的[GetNumberIntent],則可以與該推新的有效的數字到一個陣列中的單個StateHandler獲得通過,直到陣列的期望的長度。這隻需要在Intent Handler中有更多的邏輯,並且一旦數組長度爲6,就有條件跳出狀態。 首先嚐試上面的代碼,因爲它更容易看到不同的狀態。

+0

難道你不能只做{number} {number} {number} {number} {number} {number}嗎?我使用類似的隨機單詞列表。這樣,一旦用戶說了6個單詞,它只發送一個意圖。然後你只要確保他們都在你正在尋找的範圍內。 – ialexander

0

我喜歡Caleb的回答。此外,如果您對錯誤(如輸入不是數字)提供反饋,並嘗試考慮來自用戶的額外響應(例如「我不知道」或「給我一個例如「或」我能說什麼「。