回答編集履歴
1
ついき
test
CHANGED
@@ -1 +1,169 @@
|
|
1
1
|
postSlackを実行するのではなく、参考リンクのようにjsonにしてreturnしましょう
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
方針としては、メッセージ取得部分を切り離して、トリガーでコールされたときと、doPost(slack command)でコールされたときで、それぞれのハンドラが何をするか制御したほうがいいですね。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
ただこうしたとき、時刻トリガで動作したときに、遅延がなくてもメッセージが送られてしまいますね
|
10
|
+
|
11
|
+
それがいやだとなると、ちょっと面倒(既存コードのコピペですまないので、自分で考えてほしいです)
|
12
|
+
|
13
|
+
```javascript
|
14
|
+
|
15
|
+
const slackAccessToken = PropertiesService.getScriptProperties().getProperty('slack APIで取得した Verification TOKEN');
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
function triggerDriver() {
|
20
|
+
|
21
|
+
const slackApp = SlackApp.create(slackAccessToken);
|
22
|
+
|
23
|
+
// 対象チャンネル
|
24
|
+
|
25
|
+
const channelId = "#train_info";
|
26
|
+
|
27
|
+
const options = {
|
28
|
+
|
29
|
+
// 投稿するユーザーの名前
|
30
|
+
|
31
|
+
username: "train_info",
|
32
|
+
|
33
|
+
icon_emoji: ":train:",
|
34
|
+
|
35
|
+
}
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
const currentDate = new Date();
|
40
|
+
|
41
|
+
const weekday = currentDate.getDay();
|
42
|
+
|
43
|
+
const date = Utilities.formatDate( currentDate, 'Asia/Tokyo', 'M月d日 HH時mm分');
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
if (weekday === 0 || weekday === 6) {
|
48
|
+
|
49
|
+
return;
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
const calendar = CalendarApp.getCalendarById('ja.japanese#holiday@group.v.calendar.google.com');
|
54
|
+
|
55
|
+
if (calendar.getEventsForDay(currentDate, {max: 1}).length > 0) {
|
56
|
+
|
57
|
+
return;
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
const infos = getMessages();
|
62
|
+
|
63
|
+
postMessage(infos, 'slack APIで取得した Webhook Address');
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
function doPost(e) {
|
68
|
+
|
69
|
+
const infos = getMessages();
|
70
|
+
|
71
|
+
const res = {"text": infos};
|
72
|
+
|
73
|
+
return ContentService.createTextOutput(JSON.stringify(res)).setMimeType(ContentService.MimeType.JSON);
|
74
|
+
|
75
|
+
}
|
76
|
+
|
77
|
+
function getMessages() {
|
78
|
+
|
79
|
+
//soubu line info
|
80
|
+
|
81
|
+
const soubuLine = clipInfo("総武線", UrlFetchApp.fetch("https://transit.yahoo.co.jp/traininfo/detail/40/0/").getContentText());
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
//tokyometro tozai line info
|
86
|
+
|
87
|
+
const tozaiLine = clipInfo("東西線", UrlFetchApp.fetch("https://transit.yahoo.co.jp/traininfo/detail/135/0").getContentText());
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
// JR Chuo line info
|
92
|
+
|
93
|
+
const chuoLine = clipInfo("R中央線(快速)", UrlFetchApp.fetch("https://transit.yahoo.co.jp/traininfo/detail/38/0").getContentText());
|
94
|
+
|
95
|
+
return soubuLine + "\n" + tozaiLine + "\n" + chuoLine + "\n";
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
function clipInfo(line, content) {
|
102
|
+
|
103
|
+
if(content.indexOf('現在、事故・遅延に関する情報はありません。') > -1){
|
104
|
+
|
105
|
+
return "◆" + "は平常運転です";
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
const currentDate = new Date();
|
110
|
+
|
111
|
+
const date = Utilities.formatDate( currentDate, 'Asia/Tokyo', 'M月d日 HH時mm分');
|
112
|
+
|
113
|
+
const yahoodatastart = content.indexOf('og:description" content="') + 25;
|
114
|
+
|
115
|
+
const yahoodataend = content.indexOf('の情報です。');
|
116
|
+
|
117
|
+
const yahoodataoutput = content.substring(yahoodatastart, yahoodataend);
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
return "◆" + line + date + "\n" + yahoodataoutput ;
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
function postMessage(message, hookpoint){
|
126
|
+
|
127
|
+
var payload = {
|
128
|
+
|
129
|
+
"text": message,
|
130
|
+
|
131
|
+
"channelId": '#train_info',
|
132
|
+
|
133
|
+
"userName": 'train_info',
|
134
|
+
|
135
|
+
"icon_emoji": ':train:'
|
136
|
+
|
137
|
+
}
|
138
|
+
|
139
|
+
var options = {
|
140
|
+
|
141
|
+
"method": "POST",
|
142
|
+
|
143
|
+
"payload": JSON.stringify(payload),
|
144
|
+
|
145
|
+
"headers": {
|
146
|
+
|
147
|
+
"Content-type": "application/json",
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
var response = UrlFetchApp.fetch('slack APIで取得した Webhook Address',options);
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
if (response.getResponseCode() == 200) {
|
158
|
+
|
159
|
+
return response;
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
return false;
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
```
|