Skip to content

Commit

Permalink
Merge branch 'develop' into main v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
atoa committed Jun 30, 2021
2 parents 39a27b8 + 15017c7 commit 01395ac
Show file tree
Hide file tree
Showing 11 changed files with 574 additions and 12 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.3.0] - 2021-06-30
### Added
- Support to update the Fallback Intent
- Example bot based on the [AWS Workshop](https://amazonlex.workshop.aws/) to
illustrate how to configure Lambda CodeHooks and Conversation Logs

## [0.2.0] - 2021-06-29
### Added
- New example based on QnABot Reponse bots. This example shows how to deploy
Expand All @@ -29,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Initial release

[Unreleased]: https://github.com/aws-samples/aws-lex-v2-cfn-cr/compare/v0.2.0...develop
[0.2.0]: https://github.com/aws-samples/aws-lex-v2-cfn-cr/releases/tag/v0.1.0...v0.2.0
[Unreleased]: https://github.com/aws-samples/aws-lex-v2-cfn-cr/compare/v0.3.0...develop
[0.3.0]: https://github.com/aws-samples/aws-lex-v2-cfn-cr/compare/v0.2.0...v0.3.0
[0.2.0]: https://github.com/aws-samples/aws-lex-v2-cfn-cr/compare/v0.1.0...v0.2.0
[0.1.0]: https://github.com/aws-samples/aws-lex-v2-cfn-cr/releases/tag/v0.1.0
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Deployment options:
Properties:
Location:
ApplicationId: arn:aws:serverlessrepo:us-east-1:777566285978:applications/lex-v2-cfn-cr
SemanticVersion: 0.2.0
SemanticVersion: 0.3.0
Parameters:
# Custom Resource Lambda log level
LogLevel: 'INFO'
Expand Down Expand Up @@ -172,6 +172,16 @@ Resources:
value: What is your zipcode?
maxRetries: 2
allowInterrupt: true
# The Fallback intent is automatically created by the Lex service
# This will update the default fallback intent
- intentName: FallbackIntent
description: Default fallback intent when no other intent matches
intentClosingSetting:
closingResponse:
messageGroups:
- message:
plainTextMessage:
value: Sorry I am having trouble understanding.
# Creates an immutable Bot Version
LexBotVersion:
Expand Down Expand Up @@ -248,7 +258,7 @@ Lex APIS.

## Caveats

- CloudFormation Updates and creation events must complete within the Lambda
- CloudFormation update and creation events must complete within the Lambda
limit of 15 minutes. This also includes building the Bot locales. This is
enough time for the vast majority of bots. The poller functionality of the
CrHelper library is not used to extend this time since larger bot definitions
Expand All @@ -259,10 +269,12 @@ Lex APIS.
CloudFormation again. You can also use the Lex export functionality to an get
an existing working bot version and restore it into the current `DRAFT`
using the Lex import functionality
- A default fallback intent will be automatically created per locale when you
initially deploy the bot. This default fallback intent cannot be modified
with this Custom Resource
- Lex Bot Resource Policies are not implemented
- The Custom Resource stack deploys a regular IAM Role instead of an IAM Service
Linked Role to be used with the Lex Bot. The SAR service does not currently
[support](https://docs.aws.amazon.com/serverlessrepo/latest/devguide/list-supported-resources.html)
Service Linked Roles. We may change this if support is added. You can create
your own Service Linked Role separately and use it in your template

## Development

Expand Down
18 changes: 18 additions & 0 deletions examples/banker-bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
This example deploys a Banker Bot based on the [Lex V2 Workshop](https://amazonlex.workshop.aws/)
which showcases various Lex V2 capabilities.

The template shows how to attach a [Lambda Fulfillment Code Hook](https://docs.aws.amazon.com/lexv2/latest/dg/lambda.html)
and how to configure [Text Conversation Logs](https://docs.aws.amazon.com/lexv2/latest/dg/conversation-logs-configure.html)

Before you use this example, you should deploy the Custom Resource stack
in your AWS account. The Custom Resource stack name is passed as a parameter
when deploying this example. This stack name parameter is used to import the
values of the Lambda function and the IAM role created by the Custom Resource.

The bot in this example can be deployed using the
[AWS Serverless Application Model Command Line Interface (SAM CLI)](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html):

```bash
sam build --use-container
sam deploy --guided
```
15 changes: 15 additions & 0 deletions examples/banker-bot/samconfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
# https://aws.amazon.com/blogs/compute/optimizing-serverless-development-with-samconfig/

version=0.1
[default.deploy.parameters]
stack_name = "lex-v2-cfn-cr-banker-bot"
s3_bucket = "lex-v2-cfn-cr-artifacts-506886316466-us-east-1"
s3_prefix = "sam-artifacts/lex-v2-cfn-banker-bot"
region = "us-east-1"
fail_on_empty_changeset = false
confirm_changeset = true
capabilities = "CAPABILITY_IAM CAPABILITY_AUTO_EXPAND"
parameter_overrides = """
LexV2CfnCrStackName=lex-v2-cfn-cr \
"""
118 changes: 118 additions & 0 deletions examples/banker-bot/src/lambda_functions/banker_bot/lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""
MIT No Attribution
Copyright 2021 Amazon Web Services
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

# https://amazonlex.workshop.aws/banker-bot/create-first-lambda.en.files/BankingBotEnglish.py

import json
import random
import decimal

def random_num():
return(decimal.Decimal(random.randrange(1000, 50000))/100)

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 elicit_intent(intent_request, session_attributes, message):
return {
'sessionState': {
'dialogAction': {
'type': 'ElicitIntent'
},
'sessionAttributes': session_attributes
},
'messages': [ message ] if message != None else None,
'requestAttributes': intent_request['requestAttributes'] if 'requestAttributes' in intent_request else None
}


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 CheckBalance(intent_request):
session_attributes = get_session_attributes(intent_request)
slots = get_slots(intent_request)
account = get_slot(intent_request, 'accountType')
#The account balance in this case is a random number
#Here is where you could query a system to get this information
balance = str(random_num())
text = "Thank you. The balance on your "+account+" account is $"+balance+" dollars."
message = {
'contentType': 'PlainText',
'content': text
}
fulfillment_state = "Fulfilled"
return close(intent_request, session_attributes, fulfillment_state, message)

def FollowupCheckBalance(intent_request):
session_attributes = get_session_attributes(intent_request)
slots = get_slots(intent_request)
account = get_slot(intent_request, 'accountType')
#The account balance in this case is a random number
#Here is where you could query a system to get this information
balance = str(random_num())
text = "Thank you. The balance on your "+account+" account is $"+balance+" dollars."
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 == 'CheckBalance':
return CheckBalance(intent_request)
elif intent_name == 'FollowupCheckBalance':
return FollowupCheckBalance(intent_request)

raise Exception('Intent with name ' + intent_name + ' not supported')

def lambda_handler(event, context):
response = dispatch(event)
return response
Empty file.
Loading

0 comments on commit 01395ac

Please sign in to comment.