Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/njsnx/python-alexa
A Simple Python Library to make Alexa skill development easy when aimed used in Lambda
https://github.com/njsnx/python-alexa
Last synced: about 2 months ago
JSON representation
A Simple Python Library to make Alexa skill development easy when aimed used in Lambda
- Host: GitHub
- URL: https://github.com/njsnx/python-alexa
- Owner: njsnx
- Created: 2016-11-02T03:23:16.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-11-29T14:35:09.000Z (about 5 years ago)
- Last Synced: 2024-05-19T04:51:44.001Z (8 months ago)
- Language: Python
- Homepage:
- Size: 1.3 MB
- Stars: 68
- Watchers: 7
- Forks: 11
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Python Alexa
## First Release v1.5This is the second release of my *alexa* library this release note will list some of the features it provides
* Simple request routing
* Get User Location
* Creat custom cards
* Work with Dialog Directives
* Simple Session management
* Simple response crafting
* Utterance Generation using in-code configuration
* Phrase loading - Allows easy integration of multi-language support using a simple JSON configuration fileThis module is aimed at those who want to worry about the cool things you can do with Alexa and Lambda rather than having to worry about how you get the data in or out too much.
It is still recommended to use the Alexa Skills documentation to understand how to actually create a skill but this module will do the heavy lifting... or rather the tedious lifting for you.
I am planning to make this module more robust by making the code more fail-safe as well as adding some extra features like Utterance generation and Intent Scheme generation
Please let me know of bugs or feature requests in the Issues section
## Install
## Install via pip
`$ pip install git+ssh://github.com/nmyster/python-alexa.git`### Upgrade via pip
`$ pip install git+ssh://github.com/nmyster/python-alexa.git - U`## Usage
```python
"""Example skill using alexa module."""from alexa import Alexa
import json
alexa = Alexa("Hello World") # Setup a object for the Alexa class
response = alexa.response # Use the Alexa response attribute to return responses# Create your Lambda handler
def lambda_handler(event, context):
"""Lambda Handler."""
return alexa.route(event)@alexa.launch
def launch():
"""Launch Function."""
return response.statement("Welcome to hello world!")@alexa.intent("GetHello", mapping={"person": "name"})
def get_hello(session, person):
"""Get Hello Intent."""
if "previous" in session.keys():
response.card("Welcome to Hello World!")
return response.question("Welcome
Hello to you {}".format(person))
else:
response.session.set_attribute("previous", True)
return response.question("Welcome
Hello AGAIN to you {}".format(person))
@alexa.session_end
def close():
"""Close function."""
return response.statement("Good bye")if __name__ == '__main__':
with open("./test_events.json") as d:
tests = json.load(d)print(
json.dumps(
lambda_handler(
tests['launch'],
2
),
indent=2
)
)
print(
json.dumps(
lambda_handler(
tests['intent_hello'],
2
),
indent=2
)
)
print(
json.dumps(
lambda_handler(
tests['end'],
2
),
indent=2
)
)```
### Phrases
Using a simple JSON file, you can create a file full of your text responses that you return either in questions/statements or on cards.The idea of this file is to allow you to remove the phrases you send back from the code logic and make it easy for multiple language support. You can see in `examples/phrases.json` the sort of layout you might want to use.
Then, when it comes to loading a phrase, you can just call `alexa.phrases(, , )`. This class will automatically check the locale of the request and if available, load the version of that phrase for that locale. If it doesn't exist, it will load a default value.
#### Example Phrases usage
**phrases.json**```json
{
"phrases": {
"AMAZON.HelpIntent": {
"help": {
"default": [
"I can help you by creating a repair reservation, tell you about Jung hein rick and keep you up to date on how your service with us is going. How can I assist?",
"there is a few ways I can help you. I can tell you about the status of your up coming support requests, arrange for a new request or tell you more about what we do and our service to you.
What would you like to do?
"
]
}
},
"AMAZON.StopIntent": {
"afternoon": {
"default": [
"Good bye. Enjoy the rest of the day"
]
},
"evening": {
"default": [
"Good bye. Have a nice evening"
]
},
"morning": {
"default": [
"Good bye. Have a good day"
]
},
"night": {
"default": [
"Good bye. Have a good night"
]
}
},
...
```**skill.py**
```python
def lambda_handler(event, context):
"""Lambda Handler."""
print(json.dumps(event))
re = alexa.route(event, phrases='./phrases.json') # Route the event using python-alexa
print(json.dumps(re))
return re
@alexa.intent("AMAZON.HelpIntent")
def help(s):
return response.question(alexa.phrases.phrase(alexa.current_intent, "help"))
```### Example Output
**Launch Request**```json
{
"version": "1.0",
"response": {
"outputSpeech": {
"ssml": "Welcome to hello world!",
"type": "SSML"
},
"shouldEndSession": true,
"reprompt": {
"outputSpeech": {
"text": null,
"type": "PlainText"
}
}
}
}
```**GetHello Intent**
```json
{
"version": "1.0",
"response": {
"outputSpeech": {
"ssml": "Welcome
Hello AGAIN to you John Smith
",
"type": "SSML"
},
"shouldEndSession": false,
"reprompt": {
"outputSpeech": {
"text": "How can I help?",
"type": "PlainText"
}
}
},
"sessionAttributes": {
"previous": true
}
}
```**End Session**
```json
{
"version": "1.0",
"response": {
"outputSpeech": {
"ssml": "Good bye",
"type": "SSML"
},
"shouldEndSession": true,
"reprompt": {
"outputSpeech": {
"text": null,
"type": "PlainText"
}
}
},
"sessionAttributes": {
"previous": true
}
}
```# Acknowledgement
This module was inspired by the a Flask focused Alexa project at
https://github.com/johnwheeler/flask-ask