2017-08-03 18 views
0

我開發一個亞馬遜萊克斯聊天機器人在AWS LAMBDA在Python這將使一個API後調用並獲得JSON字符串如下響應反序列化Python的AWS LAMBDA字符串參數的構造函數/ factory方法字符串值問題

'{"_id":"598045d12e1f98980a00001e","unique_id":"ed7e4e17c7db499caee576a7761512","cerebro":{"_id":"59451b239db9fa8b0a000004","acc_id":"533a9f0d2eda783019000002","name":"cerebro","access_id":"g01n0XTwoYfEWSIP","access_token":"3Yxw8ZiUlfSPsbEVLI6Z93vZyKyBFFIV"},"bot":{"_id":"59452f42dbd13ad867000001","name":"helloword"},"rundata":{"arguments":"","target":"local"},"state":"created","queue_id":null,"s_ts":null,"e_ts":null,"response":{},"responses":[],"summary":null,"resolve_incident":false,"err":null}' 

但我感興趣的id值僅所以我轉換JSON到字典中,如下並獲得價值

res = requests.post(botrun_api, json=botrun_payload, headers=headers) 
data = json.loads(res.content) 
new_id=json_data.get('_id', None) 
return new_id 

的ID,如果我在LAMBDA控制檯測試代碼我得到的輸出

Output in AWS Lambda console

但我在我的聊天機器人,如下獲得輸出

I was unable to process your message. DependencyFailedException: Invalid Lambda Response: Received invalid response from Lambda: Can not construct instance of IntentResponse: no String-argument constructor/factory method to deserialize from String value ('59832ba22e1f98980a00009b') at [Source: "59832ba22e1f98980a00009b"; line: 1, column: 1] 

我的資料來源如下代碼:

import math 
import dateutil.parser 
import datetime 
import time 
import os 
import logging 
import requests 
import uuid 

logger = logging.getLogger() 
logger.setLevel(logging.DEBUG) 


""" --- Helpers to build responses which match the structure of the necessary dialog actions --- """ 


def get_slots(intent_request): 
    return intent_request['currentIntent']['slots'] 


def elicit_slot(session_attributes, intent_name, slots, slot_to_elicit, message): 
    return { 
     'sessionAttributes': session_attributes, 
     'dialogAction': { 
      'type': 'ElicitSlot', 
      'intentName': intent_name, 
      'slots': slots, 
      'slotToElicit': slot_to_elicit, 
      'message': message 
     } 
    } 


def close(session_attributes, fulfillment_state, message): 
    response = { 
     'sessionAttributes': session_attributes, 
     'dialogAction': { 
      'type': 'Close', 
      'fulfillmentState': fulfillment_state, 
      'message': message 
     } 
    } 

    return response 


def delegate(session_attributes, slots): 
    return { 
     'sessionAttributes': session_attributes, 
     'dialogAction': { 
      'type': 'Delegate', 
      'slots': slots 
     } 
    } 


""" --- Helper Functions --- """ 


def parse_int(n): 
    try: 
     return int(n) 
    except ValueError: 
     return float('nan') 


def build_validation_result(is_valid, violated_slot, message_content): 
    if message_content is None: 
     return { 
      "isValid": is_valid, 
      "violatedSlot": violated_slot, 
     } 

    return { 
     'isValid': is_valid, 
     'violatedSlot': violated_slot, 
     'message': {'contentType': 'PlainText', 'content': message_content} 
    } 



def APIbot(intent_request): 
    """ 
    Performs dialog management and fulfillment for cluster configuration input arguments. 
    Beyond fulfillment, the implementation of this intent demonstrates the use of the elicitSlot dialog action 
    in slot validation and re-prompting. 
    """ 

    value1 = get_slots(intent_request)["myval1"] 
    value2 = get_slots(intent_request)["myval2"] 
    intense_type = get_slots(intent_request)["Instance"] 
    source = intent_request['invocationSource'] 
    api_endpoint = 'url' 
    api_creds = { 
    'apiuser': 'user', 
    'apikey': 'key' 
    } 

    #trigger a bot run 
    botrun_api = api_endpoint + '/botruns' 

    botrun_payload = { 
     "botname":"helloword", 
     "arguments":"", 
     "target":"local", 
     "unique_id": uuid.uuid4().hex[:30] #unique run id - 30 chars max 
    } 

    headers = { 
     'Content-Type': 'application/json', 
     'Authorization': 'Key apiuser=%(apiuser)s apikey=%(apikey)s' % api_creds 
    } 

    res = requests.post(botrun_api, json=botrun_payload, headers=headers) 
    data = json.loads(res.content) 
    new_id=json_data.get('_id', None) 
    return new_id 



    # Instiate a cluster setup, and rely on the goodbye message of the bot to define the message to the end user. 
    # In a real bot, this would likely involve a call to a backend service. 
    return close(intent_request['sessionAttributes'], 
       'Fulfilled', 
       {'contentType': 'PlainText', 
        'content': 'Thanks, your values are {} and {} '.format(value1, value2)}) 


""" --- Intents --- """ 


def dispatch(intent_request): 
    """ 
    Called when the user specifies an intent for this bot. 
    """ 

    logger.debug('dispatch userId={}, intentName={}'.format(intent_request['userId'], intent_request['currentIntent']['name'])) 

    intent_name = intent_request['currentIntent']['name'] 

    # Dispatch to your bot's intent handlers 
    if intent_name == 'my_Values': 
     return APIbot(intent_request) 

    raise Exception('Intent with name ' + intent_name + ' not supported') 


""" --- Main handler --- """ 


def lambda_handler(event, context): 
    """ 
    Route the incoming request based on intent. 
    The JSON body of the request is provided in the event slot. 
    """ 
    # By default, treat the user request as coming from the America/New_York time zone. 
    os.environ['TZ'] = 'America/New_York' 
    time.tzset() 
    logger.debug('event.bot.name={}'.format(event['bot']['name'])) 

    return dispatch(event) 

請幫我在提前解決這一謝謝:)

+0

爲什麼你的API函數有兩個返回語句? – user602525

+0

[獲得AWS Lambda函數對AWS Lex bot的響應是否給出錯誤?](https://stackoverflow.com/questions/44696761/getting-response-from-aws-lambda-function-to-aws-lex -bot-is-giving-error) – user602525

+0

嗨,謝謝你的回覆,我只是爲了測試目的而返回聲明,但這個問題與你在評論中給出的問題有點不同 – Rahul

回答

1

當僅返回new_id時,Lambda顯示您的功能成功因爲它不關心響應的格式。

連接到AWS Lex時,響應必須位於AWS定義的response format中。

在你上面的例子中,你可以通過在close方法通過萊克斯傳遞new_id,輸出響應:

return close(intent_request['sessionAttributes'], 
      'Fulfilled', 
      {'contentType': 'PlainText', 
       'content': str(new_id)} 

,您還需要去掉return new_id聲明。

相關問題