in functions/source/lex_custom_lambda/lex_custom_lambda.py [0:0]
def lambda_handler(event, context):
response_data = {}
bot_name = ""
try:
logger.debug("Received event: %s", json.dumps(event))
logger.debug("Event type: %s", event['RequestType'])
logger.info(event)
if event['RequestType'] in ['Create', 'Update']:
if not valid_properties(event, context, ['BucketName', 'LexJsonKey', 'FulfillmentLambdaArn',
'LexResourcesPrefix','CognitoPoolId','SourceBucket','SourceKey','DestinationBucket','DestinationKey']):
raise RuntimeError("Missing one of the custom lambda properties..")
bot_response = create_bot(event)
bot_name = bot_response['bot_name']
event['ResourceProperties']['BotName'] = bot_name
is_processed = copy_webui_code(event)
if not is_processed:
raise RuntimeError("Failed To Copy WebUI code")
if not bot_response['status']:
response_data['Message'] = "Bot creation failed"
send(event, context, FAILED, response_data, bot_name)
else:
response_data['Message'] = "Bot creation successful"
response_data['BotName'] = bot_name
send(event, context, SUCCESS, response_data, bot_name)
elif event['RequestType'] == 'Delete':
bot_name = event['PhysicalResourceId']
response_data['BotName'] = bot_name
try:
get_bot_response = lex_client.get_bot(name=bot_name, versionOrAlias='$LATEST')
except lex_client.exceptions.NotFoundException as ex:
logger.info("Bot not found, nothing to delete... returning")
send(event, context, SUCCESS, response_data, bot_name)
return
except Exception as ex:
logger.info("Exception during get bot, failed to delete..." + str(ex))
send(event, context, SUCCESS, response_data, bot_name)
return
delete_bot_response = delete_bot(bot_name)
is_processed = delete_webui_code(event)
if not is_processed:
raise RuntimeError("Failed Delete WebUI code")
if delete_bot_response:
status = SUCCESS
response_data['Message'] = 'Successfully deleted lex bot'
else:
status = FAILED
response_data['Message'] = 'Error deleting lex bot'
send(event, context, status, response_data, bot_name)
except Exception as e:
response_data['Message'] = "Unexpected error: " + str(type(e)) + ": " + str(e.args)
print(response_data['Message'])
send(event, context, FAILED, response_data, bot_name)
return