Skip to content

Commit

Permalink
add banker bot example and minor README changes
Browse files Browse the repository at this point in the history
  • Loading branch information
atoa committed Jun 30, 2021
1 parent 63bdce3 commit 8c7ced8
Show file tree
Hide file tree
Showing 7 changed files with 511 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.3.0] - 2021-06-30
### Added
- Support for FallbackIntent
- 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
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ Lex APIS.
an existing working bot version and restore it into the current `DRAFT`
using the Lex import functionality
- 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 8c7ced8

Please sign in to comment.