因此,我的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,我很好。
但是,如果我說「我的發票是棒球美元」,它會失敗,並說「請求的技能響應出現問題」並關閉技能。
我剛剛在這篇文章中看到類似的問題:https://stackoverflow.com/questions/42721603/alexa-custom-slot-type-no-value-in-intent?rq=1 但是,答案是在JavaScript中給出。我如何在Python中做到這一點?我是Python新手,所以我真的不知道該怎麼做。 –
嘿,本,這看起來像你正在處理邊緣情況。您能否嘗試在AWS內的服務下拉列表中打開CloudWatch,並檢查日誌以獲取您的lambda以縮小錯誤的位置? –
@JosepValls我打開了CloudWatch,但我不知道如何閱讀這些日誌,我到底應該找什麼?謝謝 –