GithubHelp home page GithubHelp logo

wavycloud / pylexo Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 33 KB

Python AWS Lex Object (pylexo) to ease the pain in navigating AWS Lex lambda events and in constructing lambda response

License: MIT License

Python 100.00%

pylexo's Introduction

pylexo

Pylexo wraps lex lambda events and converts it into an object so you don't have to remember string keys. All you have to do is pass your event to LexInputEvent

Installation

To use pylexo in AWS Lambda you will have to package pylexo in your lambda package. pylexo depends on jsonobject 0.7.1 (0.8 wouldn't work in AWS Lambda)

pip install pylexo -t /path/to/lambda/package

you can also put it in a sub folder

pip install pylexo -t /path/to/lambda/package/subfolder

but you will have to run this code in your AWS Lambda handler before importing pylexo

import os
root = os.environ.get("LAMBDA_TASK_ROOT")
if root:
    packages = os.path.join(root, 'subfolder')
    logging.info("Inserting {} to path".format(packages))
    sys.path.insert(0, packages)

Usage

This is how to enable auto-complete

import pylexo
event =  {
    "messageVersion": "1.0",
    "invocationSource": "DialogCodeHook",
    "userId": "user_123",
    "sessionAttributes": {
        "RequestorCity": "Portland"
    },
    "bot": {
        "name": "OrderFlowers",
        "alias": None,
        "version": "$LATEST"
    },
    "outputDialogMode": "Text",
    "currentIntent": {
        "name": "OrderFlowers",
        "slots": {
            "PickupTime": None,
            "FlowerType": "Roses",
            "PickupDate": None
        },
        "confirmationStatus": "None"
    }
}

pylexo_event = pylexo.LexInputEvent(event)
print("messageVersion:   {}".format(pylexo_event.messageVersion))
print("invocationSource: {}".format(pylexo_event.invocationSource))
print("userId:           {}".format(pylexo_event.userId))
print("PickupTime:       {}".format(pylexo_event.currentIntent.slots['PickupTime']))

Custom Slots

if you would like to have autocomplete on slots you will have to Override LexInputEvent. Please note that we rely on jsonobject for modeling JSON schema. you can use pylexo command line interface to generate those stubs. after installing pylexo execute the following command to generate a file like order_flower_intent.py

pylexo --filepath order_flower_intent.py --slots PickupTime FlowerType PickupDate --sessions RequestorCity

once order_flower_intent.py is generated you can do the following

import order_flower_intent

def lambda_handler(event, context):
    flower_event = order_flower_intent.LexInputEvent(event)
    print(flower_event.messageVersion)
    print(flower_event.invocationSource)
    print(flower_event.userId)
    print(flower_event.bot.name)
    print(flower_event.bot.alias)
    print(flower_event.bot.version)
    print(flower_event.outputDialogMode)
    print(flower_event.currentIntent.name)
    print(flower_event.currentIntent.confirmationStatus)
    print(flower_event.currentIntent.slots.PickupTime)
    print(flower_event.currentIntent.slots.FlowerType)
    print(flower_event.currentIntent.slots.PickupDate)

pylexo's People

Contributors

gehadshaat avatar

Stargazers

 avatar Devin Pentecost avatar

Watchers

James Cloos avatar  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.