Skip to content

Instantly share code, notes, and snippets.

@borodedamie
Created October 30, 2021 07:39
Show Gist options
  • Save borodedamie/8e785c22b69cfceb04c271f6c72dc951 to your computer and use it in GitHub Desktop.
Save borodedamie/8e785c22b69cfceb04c271f6c72dc951 to your computer and use it in GitHub Desktop.
Lambda function for a cleaning service bot
import json
def get_slots(intent_request):
return intent_request['sessionState']['intent']['slots']
def get_slot(intent_request, slotName):
slots = get_slots(intent_request)
if slots is not None and slotName in slots and slots[slotName] is not None:
return slots[slotName]['value']['interpretedValue']
else:
return None
def get_session_attributes(intent_request):
sessionState = intent_request['sessionState']
if 'sessionAttributes' in sessionState:
return sessionState['sessionAttributes']
return {}
def close(intent_request, session_attributes, fulfillment_state, message):
intent_request['sessionState']['intent']['state'] = fulfillment_state
return {
'sessionState': {
'sessionAttributes': session_attributes,
'dialogAction': {
'type': 'Close'
},
'intent': intent_request['sessionState']['intent']
},
'messages': [message],
'sessionId': intent_request['sessionId'],
'requestAttributes': intent_request['requestAttributes'] if 'requestAttributes' in intent_request else None
}
def GetQuote(intent_request):
session_attributes = get_session_attributes(intent_request)
slots = get_slots(intent_request)
text = "Thank you. Our customer service agent would get back you."
message = {
'contentType': 'PlainText',
'content': text
}
fulfillment_state = "Fulfilled"
return close(intent_request, session_attributes, fulfillment_state, message)
def RescheduleAppointment(intent_request):
session_attributes = get_session_attributes(intent_request)
slots = get_slots(intent_request)
text = "Gotcha! We would check our schedule and get back to you."
message = {
'contentType': 'PlainText',
'content': text
}
fulfillment_state = "Fulfilled"
return close(intent_request, session_attributes, fulfillment_state, message)
def dispatch(intent_request):
intent_name = intent_request['sessionState']['intent']['name']
response = None
# Dispatch to your bot's intent handlers
if intent_name == 'GetQuote':
return GetQuote(intent_request)
elif intent_name == 'RescheduleAppointment':
return RescheduleAppointment(intent_request)
raise Exception('Intent with name ' + intent_name + ' not supported')
def lambda_handler(event, context):
response = dispatch(event)
print(event)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment