0

因此,我的Alexa技能要求提供發票金額。如何驗證插槽數據在alexa中是python的數字

用戶應該說「我的發票是一百美元。」

我的樣品話語是「我的發票是{} INVOICEAMOUNT美元」,其中{} INVOICEAMOUNT是Amazon.NUMBER插槽

如果用戶說了許多,一切工作正常。但是,如果他們不這樣做,那麼代碼就會爆炸,Alexa將退出我的技能。例如,如果用戶說「我的發票是棒球美元」。

這裏是我的代碼處理這個意圖:

def set_amount_in_session(intent, session): 
    card_title = "Set Invoice Amount" 
    should_end_session = False 

    if 'invoiceAmount' in intent['slots']: 
     if intent['slots']['invoiceAmount']['value'] is not None: 
      invoice_amount = intent['slots']['invoiceAmount']['value'] 
      try: 
       val = int(invoice_amount) 
      except ValueError: 
       val = 0 
      if val != 0: 
       session['attributes']['invoiceAmount'] = int(invoice_amount) 
       speech_output = "The invoice amount is " + str(invoice_amount) + " dollars. " 
       card_output = "Invoice Amount $" + str(invoice_amount) 
       reprompt_text = "Please tell me the terms of the invoice." 
      else: 
       speech_output = "I'm not sure what the invoice amount is. " \ 
           "Please try again." 
       card_output = "I'm not sure what the invoice amount is. " \ 
           "Please try again." 
       reprompt_text = "I'm not sure what the invoice amount is. " \ 
           "Please tell me the amount of the invoice you wish to factor by saying, for example, " \ 
           "my invoice is for one hundred and fifty dollars." 
     else: 
      speech_output = "I'm not sure what the invoice amount is. " \ 
          "Please try again." 
      card_output = "I'm not sure what the invoice amount is. " \ 
          "Please try again." 
      reprompt_text = "I'm not sure what the invoice amount is. " \ 
          "Please tell me the amount of the invoice you wish to factor by saying, for example, " \ 
          "my invoice is for one hundred and fifty dollars." 
    else: 
     speech_output = "I'm not sure what the invoice amount is. " \ 
         "Please try again." 
     card_output = "I'm not sure what the invoice amount is. " \ 
         "Please try again." 
     reprompt_text = "I'm not sure what the invoice amount is. " \ 
         "Please tell me the amount of the invoice you wish to factor by saying, for example, " \ 
         "my invoice is for one hundred and fifty dollars." 
    return build_response(session['attributes'], build_speechlet_response(
     card_title, speech_output, card_output, reprompt_text, should_end_session)) 

此代碼工作完全正常,當我在亞馬遜開發者控制檯測試,但它在我的回聲點失敗。

如果我說「我的發票是一百美元棒球」,它能夠處理回覆,並說它不確定發票金額是多少。

如果我說「我的發票是棒球一百美元」,它實際上忽略了「棒球」並將發票金額設置爲$ 100,我很好。

但是,如果我說「我的發票是棒球美元」,它會失敗,並說「請求的技能響應出現問題」並關閉技能。

+0

我剛剛在這篇文章中看到類似的問題:https://stackoverflow.com/questions/42721603/alexa-custom-slot-type-no-value-in-intent?rq=1 但是,答案是在JavaScript中給出。我如何在Python中做到這一點?我是Python新手,所以我真的不知道該怎麼做。 –

+0

嘿,本,這看起來像你正在處理邊緣情況。您能否嘗試在AWS內的服務下拉列表中打開CloudWatch,並檢查日誌以獲取您的lambda以縮小錯誤的位置? –

+0

@JosepValls我打開了CloudWatch,但我不知道如何閱讀這些日誌,我到底應該找什麼?謝謝 –

回答

0

所以我想通了。

當我說「我的發票是一百元」我得到:

"request": { 
    "type": "IntentRequest", 
    "requestId": "EdwRequestId.386d4fe9-dc04-4376-8e30-dec3205484fe", 
    "locale": "en-US", 
    "timestamp": "2017-08-08T14:59:43Z", 
    "intent": { 
    "name": "WhatIsInvoiceAmount", 
    "slots": { 
     "invoiceAmount": { 
     "name": "invoiceAmount", 
     "value": "100" 
     } 
    } 
    } 
} 

這當然是有效的值,因此它被正確處理。

當我說「我的發票是每百個棒球元」我得到:

"request": { 
    "type": "IntentRequest", 
    "requestId": "EdwRequestId.c1b4a1d0-4949-4247-b326-a582ce4826b4", 
    "locale": "en-US", 
    "timestamp": "2017-08-08T15:01:09Z", 
    "intent": { 
    "name": "WhatIsInvoiceAmount", 
    "slots": { 
     "invoiceAmount": { 
     "name": "invoiceAmount", 
     "value": "?" 
     } 
    } 
    } 
} 

我不知道,如果該值爲空,或者如果它僅僅是不是整數,但因爲我查對於這兩種情況下,它是不是一個問題

的問題是,當我說「我的發票是棒球美元」,該invoiceAmoutn槽沒有值:

"request": { 
    "type": "IntentRequest", 
    "requestId": "EdwRequestId.a046d29b-581c-484a-8879-abe979ec6286", 
    "locale": "en-US", 
    "timestamp": "2017-08-08T14:51:36Z", 
    "intent": { 
    "name": "WhatIsInvoiceAmount", 
    "slots": { 
     "invoiceAmount": { 
     "name": "invoiceAmount" 
     } 
    } 
    } 
} 

顯然,沒有一個「VAL ue「與具有null的」值「不同。所以我不得不在我的代碼中添加一個檢查來查看是否有價值。所以這裏是我的新代碼現在也檢查是否有值:

if 'invoiceAmount' in intent['slots']: 
    if 'value' in intent['slots']['invoiceAmount']: 
     if intent['slots']['invoiceAmount']['value'] is not None: 
      invoice_amount = intent['slots']['invoiceAmount']['value'] 
      try: 
       val = int(invoice_amount) 
      except ValueError: 
       val = 0 
      if val != 0: 
       NOW I HAVE A VALID VALUE AND CAN EXECUTE MY CODE 

有了這個改變,我似乎解決了我的問題。