0

我在Alexa中創建了一項技能,可以執行以下操作。Alexa會話技巧錯誤

  1. 用戶:您好
  2. Alexa的,你好,請給我你的名字
  3. 用戶:約翰
  4. Alexa的:你好約翰,很高興見到你。你多大了
  5. 用戶:25

下面是我的意圖

{ 
    "intents": [ 
    { 
     "intent": "StartTheFlow", 
     "slots": [ 
     { 
      "name": "custName", 
      "type": "list_of_userNames" 
     }, 
     { 
      "name": "age", 
      "type": "AMAZON.NUMBER" 
     } 
     ] 
    }, 
    { 
     "intent": "AMAZON.HelpIntent" 
    },{ 
     "intent": "Welcome" 
    }, 
    { 
     "intent": "AMAZON.StopIntent" 
    }, 
    { 
     "intent": "AMAZON.CancelIntent" 
    } 
    ] 
} 

而且下面是我的話語

StartTheFlow Hi 
StartTheFlow {custName} 
StartTheFlow {age} 

下面是我onIntent()

@Override 
    public SpeechletResponse onIntent(final IntentRequest request, final Session session) throws SpeechletException { 
     log.info("onIntent requestId={}, sessionId={}", request.getRequestId(), session.getSessionId()); 

     Intent intent = request.getIntent(); 
     String intentName = (intent != null) ? intent.getName() : null; 

     if ("StartTheFlow".equals(intentName)) { 
      return getTheFlow(intent, session); 
     } else if ("AMAZON.HelpIntent".equals(intentName)) { 
      return getHelpResponse(); 
     } else if ("WelcomeChubb".equals(intentName)) { 
      return getWelcomeResponse(); 
     } else { 
      throw new SpeechletException("Invalid Intent"); 
     } 
    } 

我正在嘗試如下處理這個問題

private SpeechletResponse getTheFlow(Intent intent, Session session) { 

     boolean isAskResponse = true; 
     String responseText = ""; 
     String nameFromSession = (String) session.getAttribute("name"); 
     if (StringUtils.isNullOrEmpty(nameFromSession)) { 
      responseText = "please give me your name"; 
      getTheNameText(intent, session); 
     } else { 
      System.out.println(session.getAttribute("nameFromSession")); 
      responseText = "please give me your date of birth"; 
     } 

     return getSpeechletResponse(responseText, "", isAskResponse); 

    } 

    private String getTheNameText(Intent intent, Session session) { 
     String userNameFrmIntent = getNameFromSlot(intent).toString(); 
     session.setAttribute("nameFromSession", userNameFrmIntent); 
     return getNameFromSlot(intent).toString(); 

    } 

    private String getNameFromSlot(Intent intent) { 
     Slot userName = intent.getSlot(Slot_Name); 
     return userName.getValue(); 
    } 

另外,我已經在頂部定義了一個插槽,如下所示。

private static final String Slot_Name = "custName"; 

但在這裏,當我鍵入Hi,不要問我的名字,這是給我在日誌中的錯誤它顯示的Java空指針異常。我輸入Hi時得到的回覆如下。

{ 
    "session": { 
    "sessionId": "SessionId.a2740ca4-73ff-4a15-856d-6461b3c7b2e1", 
    "application": { 
     "applicationId": "amzn1.ask.skill.e3dfb30e-0089-423c-a325-30ad28dd2e2b" 
    }, 
    "attributes": {}, 
    "user": { 
     "userId": "amzn1.ask.account.AEQYTT5HFHEGGDSUCT3NW45HKR7O3FBL5YCBSZIS7P5LNP5BXFEMUR7AUYOZVKC2FT5V6RKJC7RNA5VMZVREBAXAQP3NFNTQSFSSKSEXIYT4FQYMS5JCI2CCAOPUF4FN4C6DHEU6ONNY3D6GN5AWK75KOQNJH2IWROIIXTPNXSNI6FLQYRBBMP7TRSOWVNCY73WJUT2VLHDACWA" 
    }, 
    "new": true 
    }, 
    "request": { 
    "type": "IntentRequest", 
    "requestId": "EdwRequestId.cf686fc0-cbfd-4496-bb09-c41714563507", 
    "locale": "en-US", 
    "timestamp": "2017-02-15T20:12:44Z", 
    "intent": { 
     "name": "StartTheFlow", 
     "slots": { 
     "custName": { 
      "name": "custName", 
      "value": "hi" 
     } 
     } 
    } 
    }, 
    "version": "1.0" 
} 

有人可以請讓我知道我要去哪裏錯了,我怎麼能解決這個問題,我已經很被掛了一些問題,比如25,可以有人請讓我知道,如果有一個更好的方法來在java中做到這一點。

謝謝

回答

0

我會建議爲用戶說的每件事情創建一個單獨的意圖。例如,HelloIntent,NameIntent和AgeIntent。

然後一定要將這些信息傳遞給會話中所有後續的意圖。因此,每個意圖可以在開始時使用一個通用函數來從會話中讀取每個字符串(如果存在),向其中添加新的插槽數據,然後在完成之前將所有字符串寫回響應會話。

由於您將有單獨的意圖,並且用戶可以想象地將它們按順序說出來,您可能需要檢查是否輸入了所有需要的字符串,否則會提示用戶輸入任何缺少的字符串。

在會話中保存數據的問題在於下次用戶啓動技能時數據將會消失。要解決這個問題,您可以使用數據庫來保存用戶數據,並將其保存爲userId。有很多關於如何做到這一點的例子。請注意,有些數據庫基本上是免費的,但其他數據庫將根據每月使用的次數向您收費。