首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将Lambda响应发送回Lex插槽?

将Lambda响应发送回Lex插槽?
EN

Stack Overflow用户
提问于 2018-04-05 23:40:38
回答 2查看 2.8K关注 0票数 1

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

例如:在Lex中,slot Country我输入印度,然后City I输入Hyderabad.Here我正在调用lambda函数,并希望响应应该在lex插槽中包含温度详细信息。

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

EN

回答 2

Stack Overflow用户

发布于 2018-04-06 15:30:58

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

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

注意:您必须检查Initialization and validation code hook,这样它才会转到DialogCodeHook

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2018-07-15 20:07:44

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

代码语言:javascript
运行
复制
"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响应。在这里,JSON对象是使用dialogAction强制参数创建的,并添加了其他属性。将“要返回的数据”更改为要返回到AWS Lex的值。

代码语言:javascript
运行
复制
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)});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49676394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档