Hiện tại, chat bot sẽ trả lại các vị trí được lấy ra từ người dùng, trở lại cho caller dưới dạng JSON payload. Thay vào đó, chúng ta muốn bot của mình thực hiện tìm kiếm thực tế trong cơ sở dữ liệu các chuyến đi và hiển thị các tùy chọn trở lại cho người dùng, nếu không, khách hàng của chúng ta (trình duyệt) sẽ cần thực hiện thêm một cuộc gọi đến back-end để tìm kiếm các chuyến đi. Mặc dù đó có thể là những gì bạn muốn làm trong một số tình huống, nhưng trong trường hợp này, chúng ta sẽ triển khai một hàm Lambda để thực hiện tất cả công việc đáp ứng yêu cầu và trả lại kết quả trong một lần truy cập.
Trong bài tập này, chúng ta sẽ tạo một hàm Lambda được viết bằng NodeJS để thực hiện đầy đủ chức năng cho bot của chúng ta.
{
"sessionId": "885078239936701",
"inputTranscript": "Yes",
"interpretations": [
{
"intent": {
"slots": {
"date": {
"shape": "Scalar",
"value": {
"originalValue": "Tomorrow",
"resolvedValues": [
"2023-02-10"
],
"interpretedValue": "2023-02-10"
}
},
"destinationCity": {
"shape": "Scalar",
"value": {
"originalValue": "Sydney",
"resolvedValues": [
"melbourne"
],
"interpretedValue": "Sydney"
}
},
"originCity": {
"shape": "Scalar",
"value": {
"originalValue": "Melbourne",
"resolvedValues": [
"sydney"
],
"interpretedValue": "Melbourne"
}
}
},
"confirmationState": "Confirmed",
"name": "TravelBuddyCheckFlightsIntent",
"state": "ReadyForFulfillment"
},
"nluConfidence": 1
},
{
"intent": {
"slots": {},
"confirmationState": "None",
"name": "FallbackIntent",
"state": "ReadyForFulfillment"
}
}
],
"sessionState": {
"sessionAttributes": {},
"intent": {
"slots": {
"date": {
"shape": "Scalar",
"value": {
"originalValue": "Tomorrow",
"resolvedValues": [
"2023-02-10"
],
"interpretedValue": "2023-02-10"
}
},
"destinationCity": {
"shape": "Scalar",
"value": {
"originalValue": "Sydney",
"resolvedValues": [
"melbourne"
],
"interpretedValue": "Sydney"
}
},
"originCity": {
"shape": "Scalar",
"value": {
"originalValue": "Melbourne",
"resolvedValues": [
"sydney"
],
"interpretedValue": "Melbourne"
}
}
},
"confirmationState": "Confirmed",
"name": "TravelBuddyCheckFlightsIntent",
"state": "ReadyForFulfillment"
},
"originatingRequestId": "033310c3-eb7e-44e7-8e44-db2b62acc310"
},
"responseContentType": "text/plain; charset=utf-8",
"invocationSource": "FulfillmentCodeHook",
"messageVersion": "1.0",
"transcriptions": [
{
"resolvedContext": {
"intent": "TravelBuddyCheckFlightsIntent"
},
"transcription": "Yes",
"resolvedSlots": {},
"transcriptionConfidence": 1
}
],
"inputMode": "Text",
"bot": {
"aliasId": "TSTALIASID",
"aliasName": "TestBotAlias",
"name": "TravelBuddyChatBot",
"version": "DRAFT",
"localeId": "en_US",
"id": "0BTWXR7UVD"
}
}
Điều này cho thấy rằng hàm Lambda có thể đọc chính xác dữ liệu đầu vào và truy vấn bảng DynamoDB cho các chuyến bay từ Melbourne đến Sydney và trả về các kết quả phù hợp.