GithubHelp home page GithubHelp logo

vaughngit / aws-lex-custom-resources Goto Github PK

View Code? Open in Web Editor NEW

This project forked from amaabca/aws-lex-custom-resources

0.0 0.0 0.0 1.23 MB

AWS Custom CDK Resources for Lex

License: MIT License

TypeScript 100.00%

aws-lex-custom-resources's Introduction

AWS CDK - Lex V2

This package contains an AWS CDK wrapper to create, update and provision Lex V2 bots.

Features

  • Deploy Lex V2 bots easily and reliably between environments!
  • Define your Lex bots using best practices and Infrastructure as Code!
  • Automatically build/release Lex bot versions and aliases!

Installation

Using yarn:

$ yarn add @amaabca/aws-lex-custom-resources

Using npm:

$ npm install @amaabca/aws-lex-custom-resources

Usage

This example is extracted from the AWS OrderFlowersBot sample.

import { Construct } from 'constructs';
import { Stack } from 'aws-cdk-lib';
import {
  LexCustomResource,
  LexBotDefinition,
} from '@amaabca/aws-lex-custom-resources';

export default class MyCdkStack extends Stack {
  constructor(scope: Construct, id: string) {
    super(scope, id);

    // Setup our custom resource from the AWS Serverless Application Repo.
    // Application link: https://serverlessrepo.aws.amazon.com/applications/us-east-1/777566285978/lex-v2-cfn-cr
    const provider = new LexCustomResource(
      this,
      'LexV2CfnCustomResource',
      {
        semanticVersion: '0.3.0',
        logLevel: 'INFO',
      }
    );

    // The LexBotDefinition class is our main entry point to Lex bot creation.
    // Once we're happy with our bot definition, call `botDefinition.build()` to
    // generate the resource.
    const botDefinition = new LexBotDefinition(
      this,
      'OrderFlowersBot',
      provider.serviceToken(),
      {
        botName: 'OrderFlowersBot',
        dataPrivacy: {
          childDirected: false,
        },
        description: 'Bot to order flowers on the behalf of a user',
        idleSessionTTLInSeconds: 300,
        roleArn: provider.serviceLinkedRoleArn(),
      }
    );

    // Add a language for our bot to which we can add intents/slots and slot types.
    const locale = botDefinition.addLocale({
      localeId: 'en_US',
      nluIntentConfidenceThreshold: 0.40,
      voiceSettings: {
        voiceId: 'Ivy',
      },
    });

    locale.addSlotType({
      slotTypeName: 'FlowerTypes',
      description: 'Types of flowers to pick up',
      valueSelectionSetting: {
        resolutionStrategy: 'OriginalValue'
      },
      slotTypeValues: [
        { sampleValue: { value: 'lillies' } },
        { sampleValue: { value: 'roses' } },
        { sampleValue: { value: 'tulips' } },
      ],
    });

    const orderFlowers = locale.addIntent({
      intentName: 'OrderFlowers',
      description: 'Intent to order a bouquet of flowers for pick up',
      sampleUtterances: [
        { utterance: 'I would like to pick up flowers' },
        { utterance: 'I would like to order some flower' },
      ],
      intentConfirmationSetting: {
        promptSpecification: {
          messageGroups: [
            {
              message: {
                plainTextMessage: {
                  value: 'Okay, your {FlowerType} will be ready for pickup by {PickupTime} on {PickupDate}. Does this sound okay?',
                },
              },
            },
          ],
          maxRetries: 2,
        },
        declinationResponse: {
          messageGroups: [
            {
              message: {
                plainTextMessage: {
                  value: 'Okay, I will not place your order.'
                },
              },
            },
          ],
        },
      },
    });

    orderFlowers.addSlot({
      slotName: 'FlowerType',
      slotTypeName: 'FlowerTypes',
      description: 'The type of flowers to pick up',
      valueElicitationSetting: {
        slotConstraint: 'Required',
        promptSpecification: {
          messageGroups: [
            {
              message: {
                plainTextMessage: {
                  value: 'What type of flowers would you like to order?',
                },
              },
            },
          ],
          maxRetries: 2,
        },
      },
    });

    orderFlowers.addSlot({
      slotName: 'PickupDate',
      slotTypeName: 'AMAZON.Date',
      description: 'The date to pick up the flowers',
      valueElicitationSetting: {
        slotConstraint: 'Required',
        promptSpecification: {
          messageGroups: [
            {
              message: {
                plainTextMessage: {
                  value: 'What day do you want the {FlowerType} to be picked up?',
                },
              },
            },
          ],
          maxRetries: 2,
        },
      },
    });

    orderFlowers.addSlot({
      slotName: 'PickupTime',
      slotTypeName: 'AMAZON.Time',
      description: 'The time to pick up the flowers',
      valueElicitationSetting: {
        slotConstraint: 'Required',
        promptSpecification: {
          messageGroups: [
            {
              message: {
                plainTextMessage: {
                  value: 'At what time do you want the {FlowerType} to be picked up?',
                },
              },
            },
          ],
          maxRetries: 2,
        },
      },
    });

    // create/update the bot resource
    const bot = botDefinition.build();

    // create a version that automatically is built when the bot changes
    const version = bot.automaticVersion();

    // create an alias and assign it to the latest bot version
    bot.addAlias({
      botAliasName: 'live',
      botVersion: version.botVersion(),
      botAliasLocaleSettings: {
        en_US: {
          enabled: true
        },
      },
    });
  }
}

License

MIT

aws-lex-custom-resources's People

Contributors

tehzwen avatar darkodosenovic avatar zoiec avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.