2017-07-29 92 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)