-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.py
339 lines (293 loc) · 10.3 KB
/
send.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import os
import requests
import datetime
GRAPH_URL = "https://graph.facebook.com/v2.6"
ACCESS_TOKEN = os.environ.get("FB_ACCESS_TOKEN")
WEATHER_URL = "https://opendata.cwb.gov.tw/api/v1/rest/datastore"
WEATHER_KEY = os.environ.get("WEATHER_KEY")
WEATHER_LOCATION = "%E8%87%BA%E5%8D%97%E5%B8%82" # Tainan
NOW_YEAR = datetime.datetime.now().year
NOW_MONTH = datetime.datetime.now().month
NOW_DAY = datetime.datetime.now().day
NOW_HOUR = datetime.datetime.now().hour
NOW_MIN = datetime.datetime.now().minute
NOW_SEC = datetime.datetime.now().second
NOW_TIME = "{0}-{1}-{2} {3}:{4}:{5}".format(NOW_YEAR, NOW_MONTH, NOW_DAY, NOW_HOUR, NOW_MIN, NOW_SEC)
print_time = "{0}-{1}-{2} {3}:{4}".format(NOW_YEAR, NOW_MONTH, NOW_DAY, NOW_HOUR, NOW_MIN)
def forecast_3day_temp(id, text):
'''每三小時為單位,顯示現在的溫度
抓現在系統的時間去比對落在哪一時段
第一個參數是使用者id,第二個參數是地區'''
date_id = "F-D0047-077"
weatherurl = "{0}/{1}?Authorization={2}&format=JSON&elementName=T".format(WEATHER_URL, date_id, WEATHER_KEY, WEATHER_LOCATION)
weather = requests.get(weatherurl)
if weather.status_code == requests.codes.ok:
print("OK")
body = weather.json()
zone_num = 18 # 預設東區18
for i in range(37):
zone = body['records']['locations'][0]['location'][i]['locationName']
if zone == text:
zone_num = i
break
zone = body['records']['locations'][0]['location'][zone_num]['locationName']
url = "{0}/me/messages?access_token={1}".format(GRAPH_URL, ACCESS_TOKEN)
title = "現在" + body['records']['locations'][0]['location'][0]['weatherElement'][0]['description']
location = body['records']['locations'][0]['locationsName']
weatherElement = body['records']['locations'][0]['location'][zone_num]['weatherElement'][0]['time']
start_time = []
end_time = []
parameter = []
toUser = []
for i in range(24):
if i < 23:
start_time.append(weatherElement[i]['dataTime'])
if i > 0:
end_time.append(weatherElement[i]['dataTime'])
parameter.append(weatherElement[i]['elementValue'][0]['value'])
toUser.append(print_time + "\n" + parameter[i] + "°C")
time_interval = split_time(start_time,end_time,NOW_TIME)
if time_interval == None:
time_interval = 1
alltoUser = title + "\n" + location + " " + zone + "\n\n" + toUser[time_interval]
payload = {
"recipient": {"id": id},
"message": {"text": alltoUser}
}
response = requests.post(url, json=payload)
if response.status_code != 200:
print("Unable to send message: " + response.text)
return response.text
def forecast_3day(id, text, element):
'''每三小時為單位,顯示現在的天氣狀況或降雨機率
抓現在系統的時間去比對落在哪一時段
第一個參數是使用者id,第二個參數是地區,第三個參數是詢問的天氣預報因子'''
date_id = "F-D0047-077"
elementName = "WeatherDescription" # 預設全部顯示
if element == 'PoP6h' or element == 'Wx':
elementName = element
weatherurl = "{0}/{1}?Authorization={2}&format=JSON&elementName={3}".format(WEATHER_URL, date_id, WEATHER_KEY, elementName)
weather = requests.get(weatherurl)
if weather.status_code == requests.codes.ok:
print("OK")
body = weather.json()
zone_num = 18 # 預設東區18
for i in range(37):
zone = body['records']['locations'][0]['location'][i]['locationName']
if zone == text:
zone_num = i
break
zone = body['records']['locations'][0]['location'][zone_num]['locationName']
url = "{0}/me/messages?access_token={1}".format(GRAPH_URL, ACCESS_TOKEN)
title = "現在" + body['records']['locations'][0]['location'][0]['weatherElement'][0]['description']
location = body['records']['locations'][0]['locationsName']
weatherElement = body['records']['locations'][0]['location'][zone_num]['weatherElement'][0]['time']
start_time = []
end_time = []
parameter = []
paracode = []
toUser = []
if element == 'PoP6h':
for i in range(11):
start_time.append(weatherElement[i]['startTime'])
end_time.append(weatherElement[i]['endTime'])
parameter.append(weatherElement[i]['elementValue'][0]['value'])
toUser.append(print_time + "\n" + parameter[i] + "%")
else:
for i in range(24):
start_time.append(weatherElement[i]['startTime'])
end_time.append(weatherElement[i]['endTime'])
parameter.append(weatherElement[i]['elementValue'][0]['value'])
paracode.append(weatherElement[i]['elementValue'][1]['value'])
parameter[i] = add_unicode(paracode[i], parameter[i])
toUser.append(print_time + "\n" + parameter[i])
time_interval = split_time(start_time,end_time,NOW_TIME)
alltoUser = title + "\n" + location + " " + zone + "\n\n" + toUser[time_interval]
payload = {
"recipient": {"id": id},
"message": {"text": alltoUser}
}
response = requests.post(url, json=payload)
if response.status_code != 200:
print("Unable to send message: " + response.text)
return response.text
def forecast_1week(id, text):
'''每十二小時為單位,顯示未來一周的天氣狀況
判斷抓到的資料如果起始時間跟結束時間都是同一天,將那天的天氣狀況複寫
第一個參數是使用者id,第二個參數是地區'''
date_id = "F-D0047-079" # 台南
weatherurl = "{0}/{1}?Authorization={2}&format=JSON&elementName=WeatherDescription".format(WEATHER_URL, date_id, WEATHER_KEY)
weather = requests.get(weatherurl)
if weather.status_code == requests.codes.ok:
print("OK")
body = weather.json()
zone_num = 18 # 預設東區18
for i in range(37):
zone = body['records']['locations'][0]['location'][i]['locationName']
if zone == text:
zone_num = i
break
zone = body['records']['locations'][0]['location'][zone_num]['locationName']
url = "{0}/me/messages?access_token={1}".format(GRAPH_URL, ACCESS_TOKEN)
title = "未來一週綜合天氣預報"
location = body['records']['locations'][0]['locationsName']
weatherElement = body['records']['locations'][0]['location'][zone_num]['weatherElement'][0]['time']
start_time = []
end_time =[]
parameter = []
toUser = []
j = 0
for i in range(14):
start_time.append(weatherElement[i]['startTime'])
end_time.append(weatherElement[i]['endTime'])
if start_time[i][:10] == end_time[i][:10]:
if j > 0 and start_time[i][:10] == toUser[j-1][:10]:
j = j - 1
parameter.append(weatherElement[i]['elementValue'][0]['value'])
toUser.append(start_time[i][:10] + "\n" + parameter[j])
j = j + 1
alltoUser = title + "\n" + location + " " + zone
for i in range(j):
alltoUser = alltoUser + "\n\n" + toUser[i]
payload = {
"recipient": {"id": id},
"message": {"text": alltoUser}
}
response = requests.post(url, json=payload)
if response.status_code != 200:
print("Unable to send message: " + response.text)
return response.text
def send_start(id, text):
'''傳訊息
第一個參數是使用者id,第二個參數是要傳送的訊息'''
url = "{0}/me/messages?access_token={1}".format(GRAPH_URL, ACCESS_TOKEN)
payload = {
"recipient": {"id": id},
"message": {"text": text}
}
response = requests.post(url, json=payload)
if response.status_code != 200:
print("Unable to send message: " + response.text)
return response.text
def send_quick_replies(id, text, staten):
'''傳快速按鈕
第一個參數是使用者id,第二個參數是要傳送的訊息,第三個參數是進入的state'''
url = "{0}/me/messages?access_token={1}".format(GRAPH_URL, ACCESS_TOKEN)
if staten == "state3":
message = {
"text":text,
"quick_replies":[
{
"content_type":"text",
"title":"溫度",
"payload":"溫度",
},
{
"content_type":"text",
"title":"降雨機率",
"payload":"降雨機率",
},
{
"content_type":"text",
"title":"天氣狀況",
"payload":"天氣狀況",
},
{
"content_type":"text",
"title":"結束",
"payload":"結束",
}
]
}
elif staten == "state2":
message = {
"text":text,
"quick_replies":[
{
"content_type":"text",
"title":"現在",
"payload":"現在",
},
{
"content_type":"text",
"title":"未來一週",
"payload":"未來一週",
},
{
"content_type":"text",
"title":"結束",
"payload":"結束",
}
]
}
payload = {
"recipient": {"id": id},
"message": message
}
response = requests.post(url, json=payload)
if response.status_code != 200:
print("Unable to send message: " + response.text)
return response.text
def send_button_message(id, text):
'''傳快速按鈕
第一個參數是使用者id,第二個參數是要傳送的訊息'''
url = "{0}/me/messages?access_token={1}".format(GRAPH_URL, ACCESS_TOKEN)
payload = {
"recipient": {"id": id},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":"資料來源",
"buttons":[
{
"type":"web_url",
"url":"https://opendata.cwb.gov.tw/index",
"title":text,
"webview_height_ratio": "tall"
}
]
}
}
}
}
response = requests.post(url, json=payload)
if response.status_code != 200:
print("Unable to send message: " + response.text)
return response.text
def split_time(start, end, time):
'''找出時間的區間
第一個參數是起始時間的list,第二個參數是結束時間的list,第三個參數是想找出的時間區間
回傳list的index'''
index = 0
t = datetime.datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
for s, e in zip(start, end):
st = datetime.datetime.strptime(s, '%Y-%m-%d %H:%M:%S')
et = datetime.datetime.strptime(e, '%Y-%m-%d %H:%M:%S')
if(st <= t and et > t):
return index
index += 1
return index
def add_unicode(paracode, text):
'''在回傳的天氣狀況前面加上符號
第一個參數是天氣描述代碼,第二個參數是天氣狀況'''
cloud = ['02', '05', '06', '49', '58']
rain = ['04', '12', '17', '26', '29', '31']
umbrella = ['13', '18', '24', '34', '36', '57', '59']
mine = ['17', '29', '31', '36']
if paracode == '07' or paracode == '08':
text = u'\u26c5 ' + text # 雲和太陽
if paracode == '01':
text = u'\u2600 ' + text # 太陽
if paracode in cloud:
text = u'\u2601 ' + text # 雲
if paracode in rain:
text = u'\u2614 ' + text # 雨
if paracode in umbrella:
text = u'\uf302 ' + text # 陣雨
if paracode in mine:
text = u'\u26a1 ' + text # 雷
if paracode == '60' or paracode == '61':
text = u'\u26c4 ' + text # 雪
return text