-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk17-outbound-tph.mjs
42 lines (37 loc) · 1.1 KB
/
k17-outbound-tph.mjs
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
// Next k17 bus depart from tai po market station to fu shin for Scriptable
// default config
const url = 'https://rt.data.gov.hk/v1/transport/mtr/bus/getSchedule';
const headers = {
'Content-Type': 'application/json',
};
const BUS_STOP_ID = 'K17-U010';
const METHOD = 'POST';
const BODY = {
language: 'zh',
routeName: 'K17',
};
// send request to server
const req = new Request(url);
req.method = METHOD;
req.headers = headers;
req.body = JSON.stringify(BODY);
const data = await req.loadJSON();
// compose notification and schedule to user
let title = '';
let message = '';
if (!data.busStop || !data.busTop.length) {
title = 'K17停止服務';
message = `${data.routeStatusRemarkContent}\n${data.footerRemarks}`;
} else {
const buses = data.busStop.find((bs) => bs.busStopId === BUS_STOP_ID).bus;
const firstBus = buses[0];
const secondBus = buses[1];
title = 'K17下班車仲有幾耐走';
message = `1) ${firstBus.departureTimeText}\n2) ${secondBus.departureTimeText}`;
}
const noti = new Notification();
noti.title = title;
noti.body = message;
noti.schedule();
// complete script
Script.complete();