question

Eliav avatar image
Eliav asked

Obtain Postal Code in Python

I'm trying to obtain the user's postal code in Python 3.6. I think that the documentation is not written for python and I need help translating it. The error I'm receiving in CloudWatch is "NameError: name 'this' is not defined"

Here's my function for the retrieval:

def postalCodeRetriever():
    deviceId = this.event.context.System.device.deviceId
    TOKEN = context.System.user.permissions.consentToken
    URL = "https://api.amazonalexa.com/v1/devices/" + deviceId + "/settings/address/countryAndPostalCode"
    HEADER = {'Accept': 'application/json', 'Authorization': 'Bearer ' + TOKEN}
    response = urllib2.urlopen(URL, headers=HEADER)
    data = json.load(response)
    postalCode = data['postalCode']
    return build_response({}, build_speechlet_response(
                postalCode, postalCode, None, True))
alexa skills kitresponsepythondocumentationmobile apis and services
10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

KAIYIN@AMAZON.COM avatar image
KAIYIN@AMAZON.COM answered

First, you need to be able to get the request for Alexa skill, for example, from service simulator you can see the full request:

{ "session":{ "new":true, "sessionId":"SessionId.xxx", "application":{ "applicationId":"amzn1.ask.skill.xxx" }, "attributes":{

}, "user":{ "userId":"amzn1.ask.account.xxx" } }, "request":{ "type":"IntentRequest", "requestId":"EdwRequestId.xxx", "intent":{ "name":"sayFlowerAnswerIntent", "slots":{ "number":{ "name":"number" } } }, "locale":"en-US", "timestamp":"2018-01-02T20:22:37Z" }, "context":{ "AudioPlayer":{ "playerActivity":"IDLE" }, "System":{ "application":{ "applicationId":"amzn1.ask.skill.xxx" }, "user":{ "userId":"amzn1.ask.account.xxx" }, "device":{ "supportedInterfaces":{

} } } }, "version":"1.0" }

From the request, you will be able to get the value. For instance, if you would like to get the slot value from intent: sayFlowerAnswerIntent, the code is:

slotValue=event["request"]["intent"]["slots"]["number]["name"];

Basically, it's a different program language that's why you can use this.event like you used in Node.js. You can also search online to see the Python Alexa skill sample code that contributed by someone from open source.

10 |5000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.