-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
283 lines (268 loc) · 10.5 KB
/
lambda_function.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import sys
import logging
import os
import json
import base64
import hashlib
import hmac
import urllib.request, urllib.parse
import re
logger = logging.getLogger()
logger.setLevel(logging.INFO)
from ac_remote import AcRemote
ac_remote = AcRemote()
from env_monitor import EnvMonitor
env_monitor = EnvMonitor()
# get channel_secret and channel_access_token from your environment variable
channel_secret = os.getenv('LINE_CHANNEL_SECRET', None)
channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', None)
if channel_secret is None:
logger.error('Specify LINE_CHANNEL_SECRET as environment variable.')
sys.exit(1)
if channel_access_token is None:
logger.error('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.')
sys.exit(1)
def lambda_handler(event, context):
logger.info(json.dumps(event))
body = event.get('body', '') # Request body string
hash = hmac.new(channel_secret.encode('utf-8'), body.encode('utf-8'), hashlib.sha256).digest()
signature = base64.b64encode(hash).decode('utf-8')
# Compare X-Line-Signature request header and the signature
if signature != event.get('headers').get('X-Line-Signature', '') and signature != event.get('headers').get('x-line-signature', ''):
logger.error('validate NG')
return {'statusCode': 403, 'body': '{}'}
for event_data in json.loads(body).get('events', []):
if event_data['type'] != 'message':
continue
if event_data['message']['type'] != 'text':
continue
message_text = event_data['message']['text']
valid_temperature = r'(2[0-9]|30)℃'
if message_text == 'オン':
ac_remote.set_power_on()
logger.info("Triggering publish to shadow topic to set power to ON")
message_body = [{
'type': 'text',
'text': '電源をオンにしました。'
}]
elif message_text == 'オフ':
ac_remote.set_power_off()
logger.info("Triggering publish to shadow topic to set power to OFF")
message_body = [{
'type': 'text',
'text': '電源をオフにしました。'
}]
elif message_text == '冷房':
ac_remote.set_mode_cool()
logger.info("Triggering publish to shadow topic to set mode to Cool")
message_body = [{
'type': 'text',
'text': '冷房にしました。'
}]
elif message_text == 'ドライ':
ac_remote.set_mode_dry()
logger.info("Triggering publish to shadow topic to set mode to Dry")
message_body = [{
'type': 'text',
'text': 'ドライにしました。'
}]
elif message_text == '暖房':
ac_remote.set_mode_heat()
logger.info("Triggering publish to shadow topic to set mode to Heat")
message_body = [{
'type': 'text',
'text': '暖房にしました。'
}]
elif message_text == '温度':
message_body = [{
'type': 'text',
'text': '何度にしますか?',
'quickReply': {
'items': [{
'type': 'action',
'action': {
'type': 'message',
'label': '20℃',
'text': '20℃'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '21℃',
'text': '21℃'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '22℃',
'text': '22℃'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '23℃',
'text': '23℃'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '24℃',
'text': '24℃'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '25℃',
'text': '25℃'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '26℃',
'text': '26℃'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '27℃',
'text': '27℃'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '28℃',
'text': '28℃'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '29℃',
'text': '29℃'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '30℃',
'text': '30℃'
}
}]
}
}]
elif re.match(valid_temperature, message_text) is not None:
ac_remote.set_temperature(int(message_text.replace('℃','')))
logger.info("Triggering publish to shadow topic to set temperature")
message_body = [{
'type': 'text',
# 'text': message_text + 'にしました。'
'text': str(ac_remote.get_temperature()) + '℃にしました。'
}]
elif message_text == '室内環境':
env_temperature = env_monitor.get_temperature()
env_humidity = env_monitor.get_humidity()
env_pressure = env_monitor.get_pressure()
message_body = [{
'type': 'text',
'text': '室内環境は\n温度:' + str(round(env_temperature,1)) + '℃\n湿度:' + str(round(env_humidity,1)) + '%\n気圧:' + str(round(env_pressure,1)) + 'hPa\nです。'
}]
else:
ac_power = ac_remote.get_power()
ac_mode = ac_remote.get_mode()
ac_temperature = ac_remote.get_temperature()
env_temperature = env_monitor.get_temperature()
message_body = [{
'type': 'text',
'text': 'エアコンは\n電源:' + ac_power + '\nモード:' + ac_mode + '\n温度:' + str(ac_temperature) + '℃\nに設定されています。\n室温は' + str(round(env_temperature,1)) + '℃です。\nご用件は何ですか?',
'quickReply': {
'items': [{
'type': 'action',
'action': {
'type': 'message',
'label': 'オン',
'text': 'オン'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': 'オフ',
'text': 'オフ'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '冷房',
'text': '冷房'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': 'ドライ',
'text': 'ドライ'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '暖房',
'text': '暖房'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '温度',
'text': '温度'
}
},
{
'type': 'action',
'action': {
'type': 'message',
'label': '室内環境',
'text': '室内環境'
}
}]
}
}]
url = 'https://api.line.me/v2/bot/message/reply'
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + channel_access_token,
}
body = {
'replyToken': event_data['replyToken'],
'messages': message_body
}
req = urllib.request.Request(url, data=json.dumps(body).encode('utf-8'), method='POST', headers=headers)
with urllib.request.urlopen(req) as res:
res_body = res.read().decode('utf-8')
if res_body != '{}':
logger.info(res_body)
return {'statusCode': 200, 'body': '{}'}