2017-08-03 90 views
1

我是使用Python進行編程的全新軟件,但我正在嘗試使用它來創建一項Alexa技能,而且看起來我收到了Lambda處理程序的錯誤。我真的不知道這個功能有什麼意義,所以我希望能幫助你弄清楚問題所在。當我在亞馬遜開發者控制檯中測試時,我的技能工作得很好,但是當我在Echo Dot上測試時,它失敗了。在我的網站上,我收到了歡迎信息,但是當我嘗試設置我的第一個變量時,該點表示「請求的技能響應存在問題。」我的代碼基於亞馬遜發佈的最喜愛的顏色樣本,並將本文中的代碼納入其中:Adding session attributes in Python for Alexa skills使用Python進行Alexa技巧的Lambda Handler中的錯誤

這裏是當你設定的第一個變量是被調用函數的代碼(這是我的回聲點失敗):

def set_amount_in_session(intent, session): 
    """ Sets the invoice amount in the session and prepares the speech to reply to the 
    user. 
    """ 

    card_title = intent['name'] 
    session_attributes = {} 
    should_end_session = False 

    if 'invoiceAmount' in intent['slots']: 
     invoice_amount = intent['slots']['invoiceAmount']['value'] 
     session['attributes']['invoiceAmount'] = int(invoice_amount) 
     """ session_attributes = create_invoice_amount_attributes(invoice_amount) """ 
     speech_output = "The invoice amount is " + \ 
         invoice_amount + \ 
         " dollars. Please tell me the terms of the invoice by saying, " \ 
         "my invoice terms are net thirty." 
     reprompt_text = "Please tell me the terms of the invoice by saying, " \ 
         "my invoice terms are net thirty." 
    else: 
     speech_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 by saying, " \ 
         "my invoice is for one hundred and fifty dollars." 
    return build_response(session['attributes'], build_speechlet_response(
     card_title, speech_output, reprompt_text, should_end_session)) 
    """ return build_response(session_attributes, build_speechlet_response(
     card_title, speech_output, reprompt_text, should_end_session)) """ 

這裏是我的LAMBDA處理程序的代碼:

def lambda_handler(event, context): 
    """ Route the incoming request based on type (LaunchRequest, IntentRequest, 
    etc.) The JSON body of the request is provided in the event parameter. 
    """ 
    print("event.session.application.applicationId=" + 
      event['session']['application']['applicationId']) 

    """ 
    Uncomment this if statement and populate with your skill's application ID to 
    prevent someone else from configuring a skill that sends requests to this 
    function. 
    """ 
    if (event['session']['application']['applicationId'] != "amzn1.ask.skill.28a97ae0-0a55-4cfb-96bb-a5fcf06e0f0b"): 
     raise ValueError("Invalid Application ID") 
    # if (event['session']['application']['applicationId'] != 
    #   "amzn1.echo-sdk-ams.app.[unique-value-here]"): 
    #  raise ValueError("Invalid Application ID") 

    if event['session']['new']: 
     event['session']['attributes'] = {} 
     on_session_started({'requestId': event['request']['requestId'] }, event['session']) 
    if event['request']['type'] == "LaunchRequest": 
     return on_launch(event['request'], event['session']) 
    elif event['request']['type'] == "IntentRequest": 
     return on_intent(event['request'], event['session']) 
    elif event['request']['type'] == "SessionEndedRequest": 
     return on_session_ended(event['request'], event['session']) 

當我測試在亞馬遜的AWS LAMBDA代碼中,我得到了以下錯誤消息:

{ 
    "stackTrace": [ 
    [ 
     "/var/task/lambda_function.py", 
     295, 
     "lambda_handler", 
     "event['session']['application']['applicationId'])" 
    ] 
    ], 
    "errorType": "KeyError", 
    "errorMessage": "'session'" 
} 

事實上,當我測試出亞馬遜最喜歡的顏色樣本中未改變的代碼時,我在非常相同的地方得到了非常相同的錯誤。但最喜歡的顏色技能在我的Echo Dot上運行。

回答

0

看起來你試圖連接一個int與一個字符串。

session['attributes']['invoiceAmount'] = int(invoice_amount) 
""" session_attributes = create_invoice_amount_attributes(invoice_amount) """ 
speech_output = "The invoice amount is " + \ 
         invoice_amount + \ 
         " dollars. Please tell me the terms of the invoice by saying, " \ 
         "my invoice terms are net thirty." 

使用str()方法,以防止TypeErrors

speech_output = "The invoice amount is " + \ 
          str(invoice_amount) + \ 
          " dollars. Please tell me the terms of the invoice by saying, " \ 
          "my invoice terms are net thirty." 
+0

沒有,嘗試過,並沒有工作。 invoice_amount實際上已經是一個字符串,它通過Amazon.NUMBER插槽來存儲,但它將值存儲爲一個字符串,例如「100」。 –

+0

你說得對。我看到你投了一個int(),但意識到你使用了以前的變量。哎呀 –

0

轉換invoice_amount爲字符串我想出解決我的問題。我必須啓動所有的會話變量,然後程序開始在我的Echo Dot上工作。所以在我的get_welcome_response函數中加入:

session['attributes']['invoiceAmount'] = 0 

然後它開始工作。

但是,當我在AWS Lambda控制檯中進行測試時,我仍然得到以前得到的相同錯誤,但我不確定如何解決該問題。