質問編集履歴
1
文言変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
【GAS】LINEグループチャット
|
1
|
+
【GAS】LINEグループチャット投稿するBot
|
body
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
GASでQiitaの記事を参考に
|
4
4
|
LINE Messaging APIとGoogle Apps Scriptを使って
|
5
|
-
グループチャットに
|
5
|
+
グループチャットにLINE Botを開発途中です。
|
6
6
|
何度参考記事を見直しても
|
7
7
|
エラーが出てきます。
|
8
8
|
|
@@ -19,21 +19,7 @@
|
|
19
19
|
|
20
20
|
### 該当のソースコード
|
21
21
|
|
22
|
-
```ここに言語名を入力
|
23
|
-
// Messaging APIのチャネルアクセストークン
|
24
|
-
const CHANNEL_ACCESS_TOKEN = "【ここに個人番号】";
|
25
|
-
// 予定の追加・管理・取得をするカレンダーID
|
26
|
-
const CALENDER_ID = "【ここに個人番号】";
|
27
|
-
// グルチャのグループID
|
28
|
-
const GROUP_ID = "【ここに個人番号】";
|
29
22
|
|
30
|
-
const dateExp = /(\d{2})/(\d{2})\s(\d{2}):(\d{2})/;
|
31
|
-
const dayExp = /(\d+)[/月](\d+)/;
|
32
|
-
const hourMinExp = /(\d+)[:時](\d+)*/;
|
33
|
-
|
34
|
-
/*
|
35
|
-
* ボットにイベントが発生したときの(メイン)処理
|
36
|
-
*/
|
37
23
|
function doPost(e) {
|
38
24
|
let replyToken = JSON.parse(e.postData.contents).events[0].replyToken;
|
39
25
|
let lineType = JSON.parse(e.postData.contents).events[0].type
|
@@ -46,125 +32,10 @@
|
|
46
32
|
let cache = CacheService.getScriptCache();
|
47
33
|
let type = cache.get("type");
|
48
34
|
|
49
|
-
// 状態なし
|
50
|
-
if(type === null) {
|
51
|
-
// 「予定の追加」メッセージを受け取ったとき
|
52
|
-
if(userMessage === "予定の追加") {
|
53
|
-
cache.put("type", 1);
|
54
|
-
reply(replyToken, "予定の日付を教えてください!\n形式指定:『1/23』『1月23日』\nキャンセル:『やめる』と入力");
|
55
|
-
// 「匿名で投稿」メッセージを受け取ったとき
|
56
|
-
} else if(userMessage === "匿名で投稿") {
|
57
|
-
cache.put("type", 10);
|
58
|
-
reply(replyToken, "グルチャに匿名でラインします!投稿内容を教えてください!\nキャンセル:『やめる』と入力");
|
59
|
-
// メッセージの投稿に必要なグループIDの取得(後準備で説明)
|
60
|
-
} else if(userMessage === "getGroupId") {
|
61
|
-
reply(replyToken, JSON.parse(e.postData.contents).events[0].source.groupId);
|
62
|
-
}
|
63
|
-
// 状態あり
|
64
|
-
} else {
|
65
|
-
if(userMessage === "やめる") {
|
66
|
-
cache.remove("type");
|
67
|
-
reply(replyToken, "キャンセルしました");
|
68
|
-
return;
|
69
|
-
}
|
70
|
-
|
71
|
-
// 状態1~5は予定の追加、状態10~11は匿名で投稿
|
72
|
-
switch(type) {
|
73
|
-
// 予定の日付
|
74
|
-
case "1":
|
75
|
-
let [matched, month, day] = userMessage.match(dayExp);
|
76
|
-
cache.put("type", 2);
|
77
|
-
cache.put("month", month);
|
78
|
-
cache.put("day", day);
|
79
|
-
reply(replyToken, "次に開始時刻を教えてください!\n形式指定:『1:23』『12時』『12時34分』\nキャンセル:『やめる』と入力");
|
80
|
-
break;
|
81
|
-
// 予定の開始時刻
|
82
|
-
case "2":
|
83
|
-
let [matcheds, startHour, startMin] = userMessage.match(hourMinExp);
|
84
|
-
cache.put("type", 3);
|
85
|
-
cache.put("start_hour", startHour);
|
86
|
-
if (startMin == null) startMin = "00";
|
87
|
-
cache.put("start_min", startMin);
|
88
|
-
reply(replyToken, "次に終了時刻を教えてください!\n形式指定:『1:23』『12時』『12時34分』\n\キャンセル:『やめる』と入力");
|
89
|
-
break;
|
90
|
-
// 予定の終了時刻
|
91
|
-
case "3":
|
92
|
-
let [matchedss, endHour, endMin] = userMessage.match(hourMinExp);
|
93
|
-
cache.put("type", 4);
|
94
|
-
cache.put("end_hour", endHour);
|
95
|
-
if (endMin == null) endMin = "00";
|
96
|
-
cache.put("end_min", endMin);
|
97
|
-
reply(replyToken, "最後に予定の名前を教えてください!\nキャンセル:『やめる』と入力");
|
98
|
-
break;
|
99
|
-
// 予定の名前
|
100
|
-
case "4":
|
101
|
-
cache.put("type", 5);
|
102
|
-
cache.put("title", userMessage);
|
103
|
-
let [title, startDate, endDate] = createEventData(cache);
|
104
|
-
reply(replyToken, toEventFormat(title, startDate, endDate) + "\n\nで間違いないでしょうか?よろしければ『はい』を、やり直す場合は『いいえ』と入力してください!");
|
105
|
-
break;
|
106
|
-
// 予定の確認
|
107
|
-
case "5":
|
108
|
-
cache.remove("type");
|
109
|
-
if (userMessage === "はい") {
|
110
|
-
let [title, startDate, endDate] = createEventData(cache);
|
111
|
-
CalendarApp.getCalendarById(CALENDER_ID).createEvent(title, startDate, endDate);
|
112
|
-
reply(replyToken, "予定を追加しました!");
|
113
|
-
} else {
|
114
|
-
reply(replyToken, "お手数ですが最初からやり直してください");
|
115
|
-
}
|
116
|
-
break;
|
117
|
-
|
118
|
-
// 匿名で投稿する内容
|
119
|
-
case "10":
|
120
|
-
cache.put("type", 11);
|
121
|
-
cache.put("post", userMessage);
|
122
|
-
let message = createPost(cache);
|
123
|
-
reply(replyToken, message + "\n\nで間違いないでしょうか?よろしければ『はい』を、やり直す場合は『いいえ』と入力してください!\nいたずらや誹謗中傷は絶対にやめてください");
|
124
|
-
break;
|
125
|
-
// 投稿する内容の確認
|
126
|
-
case "11":
|
127
|
-
cache.remove("type");
|
128
|
-
if (userMessage === "はい") {
|
129
|
-
let message = createPost(cache);
|
130
|
-
pushPost("匿名投稿:\n" + message);
|
131
|
-
} else {
|
132
|
-
reply(replyToken, "お手数ですが最初からやり直してください");
|
133
|
-
}
|
134
|
-
cache.remove("post");
|
135
|
-
break;
|
136
|
-
}
|
137
|
-
}
|
138
|
-
}
|
139
|
-
|
140
35
|
/*
|
141
|
-
* 追加する予定の日付、開始時刻、終了時刻、名前の作成・保管
|
142
|
-
*/
|
143
|
-
function createEventData(cache) {
|
144
|
-
const year = new Date().getFullYear();
|
145
|
-
const title = cache.get("title");
|
146
|
-
const startDate = new Date(year, cache.get("month") - 1, cache.get("day"), cache.get("start_hour"), cache.get("start_min"));
|
147
|
-
const endDate = new Date(year, cache.get("month") - 1, cache.get("day"), cache.get("end_hour"), cache.get("end_min"));
|
148
|
-
return [title, startDate, endDate];
|
149
|
-
}
|
150
36
|
|
151
|
-
/*
|
152
|
-
* 追加する予定の確認のためのフォーマット作成
|
153
37
|
*/
|
154
|
-
function toEventFormat(title, startDate, endDate) {
|
155
|
-
const start = Utilities.formatDate(startDate, "JST", "MM/dd HH:mm");
|
156
|
-
const end = Utilities.formatDate(endDate, "JST", "MM/dd HH:mm");
|
157
|
-
const str = title + ": " + start + " ~ " + end;
|
158
|
-
return str;
|
159
|
-
}
|
160
38
|
|
161
|
-
/*
|
162
|
-
* 匿名で投稿する内容の作成・保管
|
163
|
-
*/
|
164
|
-
function createPost(cache){
|
165
|
-
const post = cache.get("post");
|
166
|
-
return post;
|
167
|
-
}
|
168
39
|
|
169
40
|
/*
|
170
41
|
* ボットのメッセージ応答
|
@@ -188,64 +59,6 @@
|
|
188
59
|
return ContentService.createTextOutput(JSON.stringify({"content": "post ok"})).setMimeType(ContentService.MimeType.JSON);
|
189
60
|
}
|
190
61
|
|
191
|
-
/*
|
192
|
-
* ボットからのポスト処理
|
193
|
-
*/
|
194
|
-
function pushPost(body){
|
195
|
-
const url = "https://api.line.me/v2/bot/message/push";
|
196
62
|
|
197
|
-
// 指定のグルチャにPOSTする
|
198
|
-
UrlFetchApp.fetch(url, {
|
199
|
-
"headers": {
|
200
|
-
"Content-Type": "application/json; charset=UTF-8",
|
201
|
-
"Authorization": "Bearer " + CHANNEL_ACCESS_TOKEN,
|
202
|
-
},
|
203
|
-
"method": "post",
|
204
|
-
"payload": JSON.stringify({
|
205
|
-
"to": GROUP_ID,
|
206
|
-
"messages":[{
|
207
|
-
"type": "text",
|
208
|
-
"text": body,
|
209
|
-
}]
|
210
|
-
})
|
211
|
-
})
|
212
|
-
}
|
213
63
|
|
214
|
-
/*
|
64
|
+
/*
|
215
|
-
* 通知する予定の取得
|
216
|
-
*/
|
217
|
-
function getEvents() {
|
218
|
-
let date = new Date();
|
219
|
-
date.setDate(date.getDate() + 1);
|
220
|
-
const events = CalendarApp.getCalendarById(CALENDER_ID).getEventsForDay(date);
|
221
|
-
|
222
|
-
if (events.length !== 0) {
|
223
|
-
let body = "明日の予定は\n";
|
224
|
-
events.forEach(function(event) {
|
225
|
-
const title = event.getTitle();
|
226
|
-
const start = toHHmm(event.getStartTime());
|
227
|
-
const end = toHHmm(event.getEndTime());
|
228
|
-
body += "*" + title + ": " + start + " ~ " + end + "\n";
|
229
|
-
});
|
230
|
-
body += "です!";
|
231
|
-
|
232
|
-
pushPost(body);
|
233
|
-
}
|
234
|
-
}
|
235
|
-
|
236
|
-
/*
|
237
|
-
* 時刻フォーマットの作成
|
238
|
-
*/
|
239
|
-
function toHHmm(date){
|
240
|
-
return Utilities.formatDate(date, "JST", "HH:mm");
|
241
|
-
}
|
242
|
-
```
|
243
|
-
|
244
|
-
### 試したこと
|
245
|
-
|
246
|
-
タイプミスなどの確認ぐらいしか分かりませんでした、、、
|
247
|
-
よろしくお願いいたします????♂️
|
248
|
-
|
249
|
-
### 補足情報(FW/ツールのバージョンなど)
|
250
|
-
|
251
|
-
ここにより詳細な情報を記載してください。
|