Reputation: 630
I have written 5-6 slots and have lambda function which invokes the third party weather api and i am getting the response from the api. Now how can i handle that response and send it back to lex slots with the response.
Eg: In Lex, slot Country I type India, then City I type Hyderabad.Here i am invoking the lambda function and want the response should come in the lex slot with the temperature details.
I am using the Lex console and the Lambda function as Inline code editor.
Upvotes: 1
Views: 3950
Reputation: 11
Lambda response to Lex should have following structure where dialogAction parameter is mandatory
"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"
}
]
}
]
}
You can use the following code as an example to create node.js response back in callback. Here JSON object is created using dialogAction mandatory parameter and other attributes are added. Change "Data to return" to the values which you want to return to 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)});
Upvotes: 1
Reputation: 6800
I will take 2 slots, and handle the empty slot in code (python).
So first you have to define 2 slots city
and country
in your intent, bot will check if the slots value is filled or not in DialogCodeHook
, if validation is successful it will call the FulfillmentCodeHook
for calling weather api to give result back to user.
Note: You have to check Initialization and validation code hook
so that it will go to 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)
Hope it helps.
Upvotes: 2