【问题标题】:Lambda response send back to Lex slot?Lambda 响应发送回 Lex 插槽?
【发布时间】:2018-09-15 12:20:17
【问题描述】:

我已经编写了 5-6 个插槽并具有调用第三方天气 api 的 lambda 函数,并且我正在从 api 获得响应。现在我该如何处理该响应并将其与响应一起发送回 lex 插槽。

例如:在 Lex 中,槽 Country 我输入 India,然后 City 我输入 Hyderabad。在这里我调用 lambda 函数并希望响应应该来自带有温度详细信息的 lex 槽。

我正在使用 Lex 控制台和 Lambda 函数作为内联代码编辑器。

【问题讨论】:

  • 为什么要response来填充一些slot,一般我们做的是获取slots详情(即参数值)并调用第三方api实现,然后显示response。您可以只显示带有温度详细信息的响应,为什么是插槽?
  • @sid8491 :你能解释一下我提到的上述场景(天气)吗?了解您解释的流程对我有好处。

标签: node.js amazon-web-services aws-lambda amazon-lex


【解决方案1】:

我将占用 2 个插槽,并在代码中处理空插槽(python)。

所以首先你必须在你的意图中定义2个插槽citycountry,机器人将检查DialogCodeHook中的插槽值是否已填充,如果验证成功,它将调用FulfillmentCodeHook调用天气 api 将结果返回给用户。

注意:您必须检查Initialization and validation code hook,以便它转到DialogCodeHook

def build_response(message):
    return {
        "dialogAction":{
            "type":"Close",
            "fulfillmentState":"Fulfilled",
            "message":{
                "contentType":"PlainText",
                "content":message
            }
        }
    }

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

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

def perform_action(intent_request):
    source = intent_request['invocationSource']   # DialogCodeHook or FulfillmentCodeHook
    slots = intent_request['currentIntent']['slots']  # your slots city and country
    if source == 'DialogCodeHook':
        # Perform basic validation on the supplied input slots.
        if slots['city'] is None:  # or any other validation that you want to perform
            return elicit_slot(
                intent_request['currentIntent']['name'], # current intent name
                slots, # current intent slots
                'city',  # slot name
                'Please enter city name'  # prompt the user to enter city name
            )
        if slots['country'] is None:
            return elicit_slot(
                intent_request['currentIntent']['name'], # current intent name
                slots, # current intent slots
                'country',  # slot name
                'Please enter country name' # prompt the user to enter country name
            )
        # delegate means all slot validation are done, we can move to Fulfillment
        return delegate(output_session_attributes, slots) 
    if source == 'FulfillmentCodeHook':
        result = your_api_call(slots['city'], slots['country'])
        return build_response(result)  # display the response back to user

def dispatch(intent_request):
    intent_name = intent_request['currentIntent']['name']
    # Dispatch to your bot's intent handlers
    if intent_name == 'GetWeather':
        return perform_action(intent_request)
    raise Exception('Intent with name ' + intent_name + ' not supported')

def lambda_handler(event, context):
    logger.debug(event)
    return dispatch(event)

希望对你有帮助。

【讨论】:

    【解决方案2】:

    对 Lex 的 Lambda 响应应具有以下结构,其中 dialogAction 参数是必需的

    "dialogAction": 
       {
           "type": "Close",
            "fulfillmentState": "Fulfilled or Failed",
            "message": {
             "contentType": "PlainText or SSML or CustomPayload",
             "content": "Message to convey to the user. For example, Thanks, your pizza has been 
     ordered."
         },
          "responseCard": {
             "version": integer-value,
             "contentType": "application/vnd.amazonaws.card.generic",
             "genericAttachments": [
                {
                 "title":"card-title",
                 "subTitle":"card-sub-title",
                 "imageUrl":"URL of the image to be shown",
                 "attachmentLinkUrl":"URL of the attachment to be associated with the card",
                 "buttons":[ 
                     {
                        "text":"button-text",
                        "value":"Value sent to server on button click"
                     }
                  ]
               } 
           ] 
         }
    

    您可以使用以下代码作为示例,在回调中创建 node.js 响应。这里使用 dialogAction 强制参数创建 JSON 对象并添加其他属性。将“要返回的数据”更改为您想要返回到 AWS Lex 的值。

    var obj = {};
    var dialogAction = {};
       dialogAction.type="Close";
       dialogAction.fulfillmentState="Fulfilled";
    
       var message = {};
       message.contentType="PlainText";
       message.content="Data to return";
    
       dialogAction.message = message;
       obj.dialogAction = dialogAction;
       connection.end(function (err) { callback(err, obj)});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      相关资源
      最近更新 更多