Receiving notifications from VoiceAI Connect to Lex V2 bot
VoiceAI Connect sends notifications to Lex V2 bot in the format of: <EventName>
You can create an intent that uses the <EventName> as the sample utterance in order to activate an intent.
Event data is currently not supported for AWS.
Let's create an intent with the event name and <EventName> as the sample utterance. We will use noUserInput in this demo.
In this demo, the lambda sends a confirmation message with the event name.
import json
import re
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def get_session_attributes(intent_request):
return intent_request['sessionState'].get('sessionAttributes', {})
def get_intent(intent_request):
return intent_request['sessionState'].get('intent', {})
def confirm(intent_request, event_name):
intent = get_intent(intent_request)
intent['confirmationState'] = 'Confirmed'
intent['state'] = 'Fulfilled'
return_message = {
'sessionState': {
'sessionAttributes': get_session_attributes(intent_request),
'dialogAction': {
'type': 'Close'
},
'intent': intent
},
'sessionId': intent_request['sessionId'],
'requestAttributes': intent_request.get('requestAttributes'),
'messages': [{
'contentType': 'PlainText',
'content': 'Lambda received event ' + event_name
}]
}
logger.debug(f'sending confirm intent: {json.dumps(return_message)}')
return return_message
def delegate(intent_request):
logger.debug(f'delegating back to bot. request: ${json.dumps(intent_request)}')
return {
'sessionState': {
'dialogAction': {
'type': 'Delegate'
},
'intent': intent_request['sessionState']['intent'],
'sessionAttributes': get_session_attributes(intent_request)
},
'sessionId': intent_request['sessionId'],
'requestAttributes': intent_request.get('requestAttributes')
}
# --- Intents ---
def dispatch(intent_request):
user_input = intent_request['inputTranscript']
match = re.match(r'^<(\w+)>$', user_input)
if match:
event_name = match[1]
return confirm(intent_request, event_name)
return delegate(intent_request)
# --- Main handler ---
def lambda_handler(event, context):
logger.debug('event.bot.name={}'.format(event['bot']['name']))
logger.debug(json.dumps(event))
return dispatch(event)
Use a lambda for processing notification
-
To activate the lambda function check the code hook option at the end of the intent.
Activate the Fulfillment section of the intent and fill the messages for success and failure.
-
Add a closing response with the value in the sentence.