My question is where will i put the codes of the 1st method from the
above link?
When the intent-A is called and you are responding back to user, at that time you will use that code. Basically instead of dialogAction
type Close
we are using ConfirmIntent
.
You can read more about response format here.
Complete code:
def build_response(message):
return {
"dialogAction":{
"type":"Close",
"fulfillmentState":"Fulfilled",
"message":{
"contentType":"PlainText",
"content":message
}
}
}
def delegate(session_attributes, slots):
return {
'sessionAttributes': session_attributes,
'dialogAction': {
'type': 'Delegate',
'slots': slots
}
}
def confirm_intent(session_attributes, intent_name, slots, message):
return {
'sessionAttributes': session_attributes,
'dialogAction': {
'type': 'ConfirmIntent',
'intentName': intent_name,
'slots': slots,
'message': {
'contentType': 'PlainText',
'content': message
}
}
}
def perform_action_A(intent_request):
source = intent_request['invocationSource']
output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
slots = intent_request['currentIntent']['slots']
# whatever you want to do
if source == 'DialogCodeHook':
# Perform basic validation on the supplied input slots.
return delegate(output_session_attributes, slots)
if source == 'FulfillmentCodeHook':
# action fulfillment code
msg = "Hi, I am a xxx-BOT. i can help you with following: A B C"
return confirm_intent(output_session_attributes, 'intent-B', slots, msg)
def perform_action_B(intent_request):
# some code
if source == 'DialogCodeHook':
# Perform basic validation on the supplied input slots.
return delegate(output_session_attributes, slots)
if source == 'FulfillmentCodeHook':
# action fulfillment code
build_response('Final close message')
def dispatch(intent_request):
intent_name = intent_request['currentIntent']['name']
# Dispatch to your bot's intent handlers
if intent_name == 'intent-A':
return perform_action_A(intent_request)
if intent_name == 'intent-B':
return perform_action_B(intent_request)
raise Exception('Intent with name ' + intent_name + ' not supported')
def lambda_handler(event, context):
logger.debug(event)
return dispatch(event)
How to call a lambda function from a intent and what is the code
format in javascript?
I have not coded any lex bot in javascript, maybe this link might help you.
Test Event Code:
{
"currentIntent": {
"name": "intent-A",
"slots": {
}
},
"invocationSource": "DialogCodeHook",
"sessionAttributes": {},
"bot": {
"name": "Your_Bot_Name"
},
"userId": "Some_User_Id"
}
For Fulfillment change the value of invocationSource
to FulfillmentCodeHook
. Also, give the slots if there are any.
Just to clarify, configure test events
is used for testing the Lambda code by simulating a request. You can directly integrate the Lambda function with Lex and test using Lex console.
Hope it helps.
Edit 1: Updated the answer with code.
Edit 2: Updated test events code.