2017-07-29 125 views
0

因此,我设法创建了自己的第一个Alexa技能,并在我的Echo上测试了它,它可以很好地处理返回的响应。在Alexa技巧中使用Python 2.7获取邮编

我现在正在寻找扩大我的设备的功能,我想获得与设备设置相关的邮编。我已经将其作为技能创建的一部分进行访问,并且我已经读过,您无法将此功能作为技能创建的一部分进行测试,您必须直接在设备上进行测试。

下面是我的代码,以获得邮编: -

def get_postcode(intent, session): 
    session_attributes = {} 
    reprompt_text = None 

    URL = "https://api.eu.amazonalexa.com/v1/devices/{}/settings" \ 
      "/address".format(context.System.device.deviceId) 
    TOKEN = context.System.user.permissions.consentToken 
    HEADER = {'Accept': 'application/json', 
      'Authorization': 'Bearer {}'.format(TOKEN)} 
    response = urllib2.urlopen(URL, headers=HEADER) 
    data = json.load(response) 

    speech_output = "Your postcode is " + data['postalCode'] 
    should_end_session = True 

    return build_response(session_attributes, build_speechlet_response(
     intent['name'], speech_output, reprompt_text, should_end_session)) 

不幸的是,每当我测试,我收到我的回应的回应,“有与返回的响应问题”

有什么建议么?我觉得我的技能是调用正确的意图,但lambda没有正确处理函数。

回答

0

该函数返回与另外三个方法类似文件的对象:

geturl() — return the URL of the resource retrieved, commonly used to determine if a redirect was followed 
info() — return the meta-information of the page, such as headers, in the form of an mimetools.Message instance (see Quick Reference to HTTP Headers) 
getcode() — return the HTTP status code of the response. 

编辑你的问题,并显示以下的输出:

response = urllib2.urlopen(URL, headers=HEADER) 
print(response.geturl()) 
print(response.info()) 
print(response.getcode()) 
print(data)