1
votes

I'm brand new to programming in Python, but I am trying to make an Alexa skill using it and it appears I am getting an error with the Lambda handler. I really have no idea what any of this function means so I would appreciate help in figuring out what the problem is. My skill works perfectly fine when I test it in the Amazon Developer Console, but it fails when I test it on my Echo Dot. On my Dot I get the welcome message, but when I try to set my first variable, the dot says "There was a problem with the requested skills response." My code is based on the Favorite Color sample that Amazon published and I incorporated the code from this post: Adding session attributes in Python for Alexa skills .

Here is the code for function that is called when you are setting the first variable (this is where my Echo Dot fails):

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)) """

Here is the code for my Lambda handler:

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'])

When I test my code in Amazon's AWS Lambda I get the following error message:

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

In fact, I get the very same error in the very same place when I test out unaltered code from Amazon's Favorite Color sample. But the Favorite Color skill does work on my Echo Dot.

2

2 Answers

0
votes

Looks like you're trying to concatenate an int with a string.

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."

convert invoice_amount to a string using the str() method to prevent 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
votes

I figured out the solution to my problem. I had to initiate all of my session variables and then the program started working on my Echo Dot. So in my get_welcome_response function I added:

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

and then it started working.

However, when I test in the AWS Lambda console I still get the same error that I got before, I'm not sure how to solve that problem.