如何从lambda函数中的Lex Bot中获取问题



我尝试检索Lex Bot要求的问题,并将其存储在DynamoDB中,我希望语法从Amazon Lex获取问题。我尝试了这个

" start Question1 = IntentRequest.CurrentIntent.Prompts.FlowerType"

但这给了我一个错误,所以大家试图帮助我

如果您正在寻找bot问的问题,那么您将不会在JSON中得到它。您唯一获得的是插槽和插槽值。请参阅带有插槽的输入JSON,您只能获得插槽值,而不是Lex Bot

的问题

{
  "messageVersion": "1.0",
  "invocationSource": "FulfillmentCodeHook",
  "userId": "897877979",
  "sessionAttributes": {},
  "requestAttributes": null,
  "bot": {
    "name": "Reasturant",
    "alias": "$LATEST",
    "version": "$LATEST"
  },
  "outputDialogMode": "Text",
  "currentIntent": {
    "name": "Intent",
    "slots": {
      "SlotType": "SlotValue"
    },
    "slotDetails": {
      "Location": {
        "resolutions": [],
        "originalValue": "SlotValue"
      }
    },
    "confirmationStatus": "None"
  },
  "inputTranscript": "Your question goes here"
}

如果您正在寻找用户提出的问题,那么这是可能的。JSON从Lex到Lambda看起来像下面,

{
  "messageVersion": "1.0",
  "invocationSource": "FulfillmentCodeHook",
  "userId": "876669698689967676",
  "sessionAttributes": {},
  "requestAttributes": null,
  "bot": {
    "name": "LexBotName",
    "alias": "$LATEST",
    "version": "$LATEST"
  },
  "outputDialogMode": "Text",
  "currentIntent": {
    "name": "WorkingHours",
    "slots": {},
    "slotDetails": {},
    "confirmationStatus": "None"
  },
  "inputTranscript": "The question you have asked"
}

键" inputTranscript "是您可以得到问题的地方。因此,您会得到问题,

var question = event.inputTranscript

其中"事件"是lambda处理程序函数的第一个参数。

您从lambda代码提供的响应(ElicitSlotElicitIntentClose等(是bot提出的问题。

如果您试图将对话保存在DynamoDB中,则可以制作一个函数,该函数将获得用户消息 bot响应 ,然后将其保存到DynamoDB表。您可以在机器人回复用户之前调用此功能。
我就是这样做的。

伪代码:

def dynamo_put_convo(user_message, bot_response):
    #your code to put items into dynamodb

def perform_action(intent_request):
    user_message = intent_request['inputTranscript']
    if source == 'DialogCodeHook':
        # your code
        validation_result = some validation code
        if validation_result == False:
            bot_response = elicit_slot_message
            dynamo_put_convo(user_message, bot_response)
            return elicit_slot()
        return delegate()
    if source == 'FulfillmentCodeHook':
        # your code
        bot_response = output_from_your_code_which_you_want_to_display_to_user
        dynamo_put_convo(user_message, bot_response)
        return close(bot_response)
def lambda_handler(event, context):
    intent_name = intent_request['currentIntent']['name']
    if intent_name == 'ABC':
        return perform_action(intent_request)
    raise Exception('Intent with name ' + intent_name + ' not supported')

这个想法是制作一个函数,将记录放在dynamodb表中 并在您的机器人将任何消息发送给 用户。

希望它有帮助。

最新更新