Last active
April 20, 2016 04:06
-
-
Save Skinner927/4c277089818a5a9bd42529ed46780a1f to your computer and use it in GitHub Desktop.
Amazon Alexa/Echo Find My iPhone
This file contains hidden or 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
""" | |
This is a WSGI script that will take responses from Amazon's Echo/Alexa and hit iCloud's FindMyIPhone. | |
Basically, using the power of pyicloud, you can ask Alexa to find your iPhone. | |
Example of what to say to Alexa: | |
Alexa, tell find my iphone dennis | |
or | |
Alexa, tell find my iphone christine | |
or | |
Alexa, tell find my iphone to call christine | |
""" | |
""" | |
App config on Amazon's end: | |
Name: FindMyIPhone | |
Invocation Name: find my iphone | |
Intent Schema: | |
{ | |
"intents": [ | |
{ | |
"intent": "FindIphone", | |
"slots": [ | |
{ | |
"name": "User", | |
"type": "AMAZON.US_FIRST_NAME" | |
} | |
] | |
} | |
] | |
} | |
Utterances: | |
FindIphone {User} | |
FindIphone to call {User} | |
""" | |
""" | |
requirements.txt | |
bitstring==3.1.4 | |
bottle==0.12.9 | |
certifi==2016.2.28 | |
click==6.6 | |
keyring==8.7 | |
keyrings.alt==1.1.1 | |
pyicloud==0.8.3 | |
pytz==2016.3 | |
requests==2.9.1 | |
six==1.10.0 | |
tzlocal==1.2.2 | |
""" | |
import os | |
# Change working directory so relative paths (and template lookup) work again | |
os.chdir(os.path.dirname(__file__)) | |
activate_this = os.path.dirname(os.path.abspath(__file__)) + \ | |
'/venv/bin/activate_this.py' | |
execfile(activate_this, dict(__file__=activate_this)) | |
import bottle | |
from bottle import request, post | |
from pyicloud import PyiCloudService | |
@post('/') | |
def findIphone(): | |
try: | |
intent = request.json['request']['intent'] | |
if intent['name'] == 'FindIphone': | |
user = intent['slots']['User']['value'] | |
return findUserIphone(user) | |
except: | |
return response("Whoops, something broke. Try again.") | |
return response( | |
"No idea what you asked for. Try saying,Find My iPhone Dennis.") | |
# run bottle | |
# do not remove the application assignment (wsgi won't work) | |
application = bottle.default_app() | |
def findUserIphone(user): | |
user = user.lower() | |
if user == 'dennis': | |
api = PyiCloudService('[email protected]', 'hunter2') | |
elif user == 'christine': | |
api = PyiCloudService('[email protected]', 'password1') | |
else: | |
return response("I don't know who " + user + " is.") | |
phones = [d for d in api.devices if d.content['deviceClass'] == 'iPhone'] | |
if len(phones) < 1: | |
return response("Sorry, couldn't find any iPhones for " + user) | |
for p in phones: | |
p.play_sound() | |
return response("Calling " + user + "'s iPhone.") | |
def response(msg): | |
return { | |
"version": "1.0", | |
"response": { | |
"outputSpeech": { | |
"type": "PlainText", | |
"text": msg, | |
}, | |
"shouldEndSession": True | |
} | |
} |
This file contains hidden or 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
curl -vX POST http://localhost -d @sample_request.json --header 'Content-type: application/json' |
This file contains hidden or 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
{ | |
"session": { | |
"new": true, | |
"sessionId": "SessionId.60985d36-b3ef-4112-84f2-d9e6391969fe", | |
"attributes": null, | |
"user": { | |
"userId": "amzn1.ask.account.AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" | |
}, | |
"application": { | |
"applicationId": "amzn1.echo-sdk-ams.app.60985d36-b3ef-4112-84f2-d9e6391969fe" | |
} | |
}, | |
"version": "1.0", | |
"request": { | |
"timestamp": "2016-04-19T04:52:45Z", | |
"type": "IntentRequest", | |
"requestId": "EdwRequestId.60985d36-b3ef-4112-84f2-d9e6391969fe", | |
"intent": { | |
"slots": { | |
"User": { | |
"name": "User", | |
"value": "Dennis" | |
} | |
}, | |
"name": "FindIphone" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment