Skip to content

Commit

Permalink
Initial Checkin
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Geer committed Aug 25, 2017
1 parent 2dd116f commit 1b255d0
Show file tree
Hide file tree
Showing 10 changed files with 461 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pipeline {
agent any

stages {
stage('Build') {
steps {
echo 'Building..'
}
}
stage('Test') {
steps {
echo 'Testing..'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
}
}
}
}
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# LexBot as Code (LBaC)

This application expects the definition of a lex bot, lex intents, lex slots, and the corresponding lambdas to
be done via YAML definitions in the corresponding directory. This is a sample application.


## Boto3 Configuration
You will need to configure Boto3 to use your AWS access keys.

In your home directory create two files

### ~/.aws/config
[default]
region=us-east-1

### ~/.aws/credentials
[default]
aws_access_key_id = <ACCESS KEY ID>
aws_secret_access_key = <SECRET ACCESS KEY>


## How To Use
You will need to define your corresponding bots, intents, slot types, and lambdas in the corresponding directories.
You will need to follow the yaml specification shown in the HelloWorld samples.

For lambdas, you will need to put the fullfillment script in the code subdirectory.
In the YAML you will need to set the handler to point to the function to execute in that script in the format

filename.functionname

For example, in our script the file is *sayhello.py* and the function is *execute* so the handler is *sayhello.execute*

To point the intent at a lambda you deploy you will have to specify the ARN of the lambda you deploy which likely
includes your member id for your AWS account.

Once you have setup your boto3, yaml and scripts, you simply run the deploy.py and it will deploy or update
all the resources you defined.
19 changes: 19 additions & 0 deletions bots/HelloWorldBot.lex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: "HelloWorldBot"
description: "A Hello World Bot"
intents:
- intentName: "HelloWorld"
intentVersion: "$LATEST"
# - intentName: "goodbye"
# intentVersion: 1
clarificationPrompt:
messages:
- contentType: 'PlainText'
content: "I do not understand, please reword your query."
maxAttempts: 2
abortStatement:
messages:
- contentType: 'PlainText'
content: "I do not understand, please reword your query."
idleSessionTTLInSeconds: 123
locale: 'en-US'
childDirected: False
133 changes: 133 additions & 0 deletions deploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import os
import boto3
import yaml
import fnmatch
import base64
import shutil

"""
Global Information
"""
lex = boto3.client('lex-models')
lambdaaws = boto3.client("lambda")
s3 = boto3.client("s3")
lambda_bucket = "allengeerlambdas"
rootdir = cwd = os.getcwd()

"""
Place Slots
For each .yaml file in the slots directory, open that file and load the YAML.
Check to see if that slot already exists in AWS, if so set the checksum to enable update.
Write the slot to AWS
"""

os.chdir("%s/slots" %rootdir)
slot_yaml = fnmatch.filter(os.listdir('.'), '*.yaml')

for slot in slot_yaml:
with open(slot, 'r') as stream:
try:
slotdef = yaml.load(stream)
except yaml.YAMLError as exc:
print exc
try:
slotdef_aws = lex.get_slot_type(name=slotdef["name"], version="$LATEST")
slotdef["checksum"] = slotdef_aws["checksum"]
except Exception as e:
print e
lex.put_slot_type(**slotdef)


"""
Place Lambdas
For each .yaml file in the lambda directory, open that file and load the YAML.
For each lambda, put the referred to file into the appropriate zip format (with the included python workspace)
Put that zip file in an S3 bucket
Update the lambda code property to include the appropriate S3 information
Write the lambda to AWS
"""

os.chdir("%s/lambda" %rootdir)
lambda_yaml = fnmatch.filter(os.listdir('.'), '*.yaml')
for lambdafile in lambda_yaml:
with open(lambdafile, 'r') as stream:
try:
lambdadef = yaml.load(stream)
except yaml.YAMLError as exc:
print exc
try:
lamdadef_aws = lambdaaws.get_function(FunctionName=lambdadef["FunctionName"])
print "do lambda update"
shutil.make_archive("%s" % lambdadef["FunctionName"], 'zip', "code")
del lambdadef["Code"]["file"]
lambdaaws.update_function_code(
FunctionName=lambdadef["FunctionName"],
ZipFile=open("%s.zip" %lambdadef["FunctionName"], 'rb').read(),
Publish=False,
DryRun=False
)
except Exception as e:
print "do lambda create"
shutil.make_archive("%s" %lambdadef["FunctionName"], 'zip', "code")
del lambdadef["Code"]["file"]
lambdadef["Code"]["ZipFile"] = open("%s.zip" %lambdadef["FunctionName"], 'rb').read()
lambdaaws.create_function(**lambdadef)

lambdaaws.add_permission(
FunctionName=lambdadef["FunctionName"],
StatementId="%sLEX" %lambdadef["FunctionName"],
Action='lambda:*',
Principal='lex.amazonaws.com'
)


"""
Place Intents
For each .yaml file in the intent directory, open that file and load the YAML.
Check to see if that intent already exists in AWS, if so set the checksum to enable update.
Write the intent to AWS
"""


os.chdir("%s/intents" %rootdir)
intent_yaml = fnmatch.filter(os.listdir('.'), '*.yaml')
for intent in intent_yaml:
with open(intent, 'r') as stream:
try:
intentdef = yaml.load(stream)
except yaml.YAMLError as exc:
print exc
try:
intentdef_aws = lex.get_intent(name=intentdef["name"], version="$LATEST")
intentdef["checksum"] = intentdef_aws["checksum"]
except Exception as e:
print e
lex.put_intent(**intentdef)


"""
Place Bots
For each .yaml file in the bot directory, open that file and load the YAML.
Check to see if that bot already exists in AWS, if so set the checksum to enable update.
Write the bot to AWS
"""

os.chdir("%s/bots" %rootdir)
bot_yaml = fnmatch.filter(os.listdir('.'), '*.yaml')
for bot in bot_yaml:
with open(bot, 'r') as stream:
try:
botdef = yaml.load(stream)
except yaml.YAMLError as exc:
print exc
try:
botdef_aws = lex.get_bot(name=botdef["name"], versionOrAlias="$LATEST")
botdef["checksum"] = botdef_aws["checksum"]
except Exception as e:
print e
lex.put_bot(**botdef)
52 changes: 52 additions & 0 deletions intents/HelloWorld.intent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: "HelloWorld"
description: "Bot says hello"
slots:
- name: "Name"
description: "The supplied name"
slotConstraint: "Required"
slotType: "Name"
slotTypeVersion: "$LATEST"
valueElicitationPrompt:
messages:
- contentType: "PlainText" #SSML
content: "With whom do I have the pleasure of speaking?"
maxAttempts: 2
sampleUtterances:
- "My name is {Name}"
- "Im {Name}"
sampleUtterances:
- "hello"
- "hey"
- "Whats up"
- "How are you"
#confirmationPrompt:
# messages:
# - contentType: "PlainText"
# content: "Say hello?"
# maxAttempts: 2
#rejectionStatement:
# messages:
# - contentType: "PlainText"
# content: "Okay I wont say hello"
#followUpPrompt:
# prompt:
# messages:
# - contentType: "PlainText"
# content: "Sorry this isnt working"
# maxAttempts: 2
# rejectionStatement:
# messages:
# - contentType: "PlainText"
# content: "Sorry this isnt working"
#conclusionStatement:
# messages:
# - contentType: "PlainText"
# content: "Sorry this isnt working"
#dialogCodeHook:
# uri: 'arn:aws:lambda:us-west-2:account-id:function:hello:DEV'
# messageVersion:
fulfillmentActivity:
type: 'CodeHook'
codeHook:
uri: 'arn:aws:lambda:us-east-1:878047780435:function:SayHello'
messageVersion: "1.0"
Binary file added lambda/.DS_Store
Binary file not shown.
29 changes: 29 additions & 0 deletions lambda/SayHello.lambda.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FunctionName: "SayHello"
Runtime: "python2.7" #'nodejs'|'nodejs4.3'|'nodejs6.10'|'java8'|'python2.7'|'python3.6'|'dotnetcore1.0'|'nodejs4.3-edge'
Role: "arn:aws:iam::878047780435:role/service-role/LexLambda"
Handler: "sayhello.execute"
Code:
file: "sayhello.py"
# 'ZipFile': b'bytes'
# 'S3Bucket': 'string'
# 'S3Key': 'string'
# 'S3ObjectVersion': 'string'
Description: "A lambda function to say hello to the provided name"
Timeout: 3
MemorySize: 128
Publish: True
#VpcConfig:
# SubnetIds:
# - "value"
# SecurityGroupIds:
# - "value"
#DeadLetterConfig:
# TargetArn:
#Environment:
# Variables:
# ET: "PhoneHome"
#KMSKeyArn: "arn:aws:iam::878047780435:"
#TracingConfig:
# Mode: "Active" #"PassThrough"
#Tags:
# ET: "PhoneHome"
Loading

0 comments on commit 1b255d0

Please sign in to comment.