-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
148 lines (112 loc) · 4.12 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from flask import Flask, make_response, render_template, request
import logging
import json
import requests
import config
import random
import string
from google.appengine.ext import ndb
app = Flask(__name__)
CONTENT_TYPE = {'Content-Type': 'application/json;charset=UTF-8'}
LIFX_ENDPOINT = 'https://api.lifx.com/v1beta1'
from jinja2 import Template
class UserAuth(ndb.Model):
user_id = ndb.StringProperty()
pin = ndb.StringProperty()
lifx_token = ndb.StringProperty()
def generate_response(output_speech, card_title="", card_subtitle="", card_content="", session_attributes={}, should_end_session=True):
response = {
"version": "1.0",
"sessionAttributes": {
"user": {
"name": "nelson"
}
},
"response": {
"outputSpeech": {
"type": "PlainText",
"text": output_speech
},
"card": {
"type": "Simple",
"title": card_title,
"subtitle": card_subtitle,
"content": card_content
},
"shouldEndSession": should_end_session
}
}
return json.dumps(response)
@app.route('/', methods=['GET'])
def get():
template = Template(open("templates/home.html").read())
response = template.render(token='token')
return response
@app.route('/token', methods=['POST'])
def get_token():
pin = request.form['pin']
lifx_token = request.form['token']
user_auth = UserAuth.query(UserAuth.pin == pin).get()
if user_auth:
user_auth.lifx_token = lifx_token
user_auth.put()
return "OK"
else:
return "Pin not found"
@app.route('/commands', methods=['POST'])
def post():
logging.info(json.dumps(request.json, indent=4, sort_keys=False))
response = ""
try:
action = request.json["request"]["intent"]["slots"]["action"]["value"]
except TypeError:
response = generate_response("Action not found.")
return response, 200, CONTENT_TYPE
logging.info("Action: %s" % action)
user_id = request.json["session"]["user"]["userId"]
logging.info("User_id: %s" % user_id)
# Check if user we have a LIFx token for this user
user_auth = UserAuth.query(UserAuth.user_id == user_id).get()
if not user_auth or not user_auth.lifx_token:
if not user_auth or not user_auth.pin:
pin = ''.join(random.choice(string.digits) for _ in range(4))
entry = UserAuth()
entry.user_id = user_id
entry.pin = pin
entry.put()
else:
pin =user_auth.pin
speech = """Hello! We need your LIFx token.
To authenticate please go to https://alexalightsapp.appspot.com
and enter pin: {}""".format(pin)
response = generate_response(
output_speech=speech,
card_title="Alexa Lights",
card_subtitle="Auth required",
card_content=speech)
logging.info(json.dumps(json.loads(response), indent=4, sort_keys=False))
return response, 200, CONTENT_TYPE
# Query API to convert name to Symbol
if action == "on":
response = requests.put(LIFX_ENDPOINT + '/lights/all/power', auth=(user_auth.lifx_token, ''), data={'state': 'on'}).json()
if response and 'error' in response and response['error']:
speech = response['error']
else:
speech = "Lights on"
elif action == "off":
response = requests.put(LIFX_ENDPOINT + '/lights/all/power', auth=(user_auth.lifx_token, ''), data={'state': 'off'}).json()
if response and 'error' in response and response['error']:
speech = response['error']
else:
speech = "Lights off"
else:
speech = "Action unknown"
response = generate_response(
output_speech=speech,
card_title="Turn {} lights.".format(action),
card_subtitle=speech,
card_content="")
logging.info(json.dumps(json.loads(response), indent=4, sort_keys=False))
return response, 200, CONTENT_TYPE
if __name__ == '__main__':
app.run()