Last active
December 12, 2018 07:23
-
-
Save knipknap/d1263da2d681b616c1492ba0aa6627b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from botocore.vendored import requests | |
base_url = 'http://myserver.com:8888/api/' | |
auth_url = base_url+'auth/1.0/session/start' | |
open_url = base_url+'action/1.0/action/start' | |
headers = {'Content-Type': 'application/json'} | |
auth = {'username': 'foo', | |
'password': 'bar'} | |
def login(): | |
data = json.dumps(auth) | |
resp = requests.post(auth_url, data=data, headers=headers) | |
return resp.json()['sid'] | |
def lambda_handler(event, context): | |
door_slot = event['request']['intent']['slots']['door'] | |
door_name = door_slot['value'] | |
try: | |
resolved = door_slot['resolutions']['resolutionsPerAuthority'][0]['values'][0]['value'] | |
resolved_id = resolved['id'] | |
resolved_name = resolved['name'] | |
except KeyError: | |
status = 'Sorry, I am not sure which door to open' | |
else: | |
sid = login() | |
data = json.dumps({'sid': sid, 'id': resolved_id}) | |
resp = requests.post(open_url, data=data, headers=headers) | |
status = 'Opening the '+resolved_name | |
return { | |
'version': '1.0', | |
'sessionAttributes': {}, | |
'response': { | |
'outputSpeech': { | |
'type': 'PlainText', | |
'text': status | |
}, | |
'card': { | |
'type': 'Simple', | |
'title': "MyDoorOpener", | |
'content': "Open the doors" | |
}, | |
'reprompt': { | |
'outputSpeech': { | |
'type': 'PlainText', | |
'text': 'Still waiting?' | |
} | |
}, | |
'shouldEndSession': True | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment