質問編集履歴

2

文言修正

2021/08/24 10:14

投稿

SA-KYO
SA-KYO

スコア37

test CHANGED
File without changes
test CHANGED
@@ -34,7 +34,87 @@
34
34
 
35
35
  ```JavaScript
36
36
 
37
+
38
+
39
+ function doPost(e) {
40
+
41
+ var event = JSON.parse(e.postData.contents).events[0]
42
+
43
+ var replyToken = event.replyToken;
44
+
45
+ var eventtype = event.type;
46
+
47
+ if (typeof replyToken === 'undefined') {
48
+
49
+ return;
50
+
51
+ }
52
+
53
+ var messageText = "";
54
+
55
+ if (eventtype == "postback") {
56
+
57
+ messageText = event.postback.params.datetime;
58
+
59
+ } else {
60
+
61
+ messageText = event.message.text;
62
+
63
+ }
64
+
65
+ var cache = CacheService.getScriptCache();
66
+
67
+ var type = cache.get("type");
68
+
69
+
70
+
71
+ if (type === null) {
72
+
73
+ //予定の追加
74
+
75
+ if (messageText === "予定の追加") {
76
+
77
+ cache.put("type", 1);
78
+
79
+ //開始日時の質問
80
+
81
+ pushDatePicker("  開始日を入力してください", replyToken);
82
+
83
+ //今日、7日間の予定の取得
84
+
85
+ } else if (messageText.match("今日の予定")) {
86
+
87
+ reply(replyToken, getEvents());
88
+
89
+ } else if (messageText.match("今週の予定")) {
90
+
91
+ reply(replyToken, notifyWeekly());
92
+
93
+ } else {
94
+
95
+ //処理方法 の返答
96
+
97
+ replyPlans(replyToken, "「予定の追加」で予定追加します。", "「今日の予定」で今日の予定をお知らせします。", "「今週の予定」で7日間の予定をお知らせします。");
98
+
99
+ }
100
+
101
+ } else {
102
+
103
+ //キャンセル処理
104
+
105
+ if (messageText === "キャンセル") {
106
+
107
+ cache.remove("type");
108
+
109
+ reply(replyToken, "予定追加のキャンセルをしました");
110
+
111
+ return;
112
+
113
+ }
114
+
115
+
116
+
37
- switch (type) {
117
+ switch (type) {
38
118
 
39
119
  case "1":
40
120
 
@@ -156,6 +236,118 @@
156
236
 
157
237
  break;
158
238
 
239
+ }
240
+
241
+ }
242
+
243
+ }
244
+
245
+
246
+
247
+
248
+
249
+ function pushDatePicker(message, replyToken) {
250
+
251
+ var url = "https://api.line.me/v2/bot/message/reply";
252
+
253
+ UrlFetchApp.fetch(url, {
254
+
255
+ "headers": {
256
+
257
+ "Content-Type": "application/json; charset=UTF-8",
258
+
259
+ "Authorization": "Bearer "+ CHANNEL_ACCESS_TOKEN,
260
+
261
+ },
262
+
263
+ "method": "post",
264
+
265
+ "payload": JSON.stringify({
266
+
267
+ "replyToken": replyToken,
268
+
269
+ "messages": [
270
+
271
+ {
272
+
273
+ "type": "template",
274
+
275
+ "altText": "datetime_picker",
276
+
277
+ "template": {
278
+
279
+ "type": "buttons",
280
+
281
+ "thumbnailImageUrl": "https://placehold.jp/80/78cffa/0a0a0a/640x480.png?text=%E4%BA%88%E5%AE%9A%E6%97%A5%E3%81%AE%E8%BF%BD%E5%8A%A0&css=%7B%22border-radius%22%3A%2215px%22%2C%22font-family%22%3A%22%20'arial%20black'background%3A%20-webkit-gradient(linear%2C%20left%20top%2C%20left%20bottom%2C%20from(%23666666)%2C%20to(%23fffff))%22%7D", // 画像のURL
282
+
283
+ "imageAspectRatio": "rectangle", // 画像のアスペクト比、「rectangle: 1.51:1」・「square: 1:1」、デフォルト値はrectangle
284
+
285
+ "imageSize": "cover", // 画像の表示形式
286
+
287
+ "imageBackgroundColor": "#FFFFFF", // 画像の背景色
288
+
289
+ "title": "【予定日の追加】",
290
+
291
+ "text": message,
292
+
293
+ "defaultAction": {
294
+
295
+ "type": "uri",
296
+
297
+ "label": "View detail",
298
+
299
+ "uri": "https://www."
300
+
301
+ },
302
+
303
+ "actions": [
304
+
305
+ {
306
+
307
+ "type": "datetimepicker",
308
+
309
+ "label": "ここをタップ!",
310
+
311
+ "data": "action=settime",
312
+
313
+ "mode": "date",
314
+
315
+ "initial": Utilities.formatDate(new Date(), "Asia/Tokyo", "yyyy-MM-dd"),
316
+
317
+ // "max": "2018-01-24t23:59",
318
+
319
+ // "min": "2017-12-25t00:00"
320
+
321
+ }
322
+
323
+ ]
324
+
325
+ }
326
+
327
+ }
328
+
329
+ ],
330
+
331
+ "notificationDisabled": false // trueだとユーザーに通知されない
332
+
333
+ }),
334
+
335
+ });
336
+
337
+
338
+
339
+ return// ContentService.createTextOutput(JSON.stringify({ "content": "postok"})).setMimeType(ContentService.MimeType.JSON);
340
+
341
+
342
+
343
+ }
344
+
345
+
346
+
347
+ // createData(cache)他
348
+
349
+ // --->略 (現状から変えなくてよいはず)
350
+
159
351
 
160
352
 
161
353
  function createData(cache) {
@@ -176,6 +368,212 @@
176
368
 
177
369
  }
178
370
 
371
+
372
+
373
+ function EventFormat(Date) {
374
+
375
+ var y = Date.getFullYear();
376
+
377
+ var m = Date.getMonth() + 1;
378
+
379
+ var d = Date.getDate();
380
+
381
+ var w = Date.getDay();
382
+
383
+ var weekname = ['日', '月', '火', '水', '木', '金', '土'];
384
+
385
+ m = ('0' + m).slice(-2);
386
+
387
+ d = ('0' + d).slice(-2);
388
+
389
+ return y + '年' + m + '月' + d + '日 (' + weekname[w] + ')\n';
390
+
391
+ }
392
+
393
+
394
+
395
+ function replyPlans(replyToken, message, message2, message3) {
396
+
397
+ var url = "https://api.line.me/v2/bot/message/reply";
398
+
399
+ UrlFetchApp.fetch(url, {
400
+
401
+ "headers": {
402
+
403
+ "Content-Type": "application/json; charset=UTF-8",
404
+
405
+ "Authorization": "Bearer " + CHANNEL_ACCESS_TOKEN,
406
+
407
+ },
408
+
409
+ "method": "post",
410
+
411
+ "payload": JSON.stringify({
412
+
413
+ "replyToken": replyToken,
414
+
415
+ "messages": [{
416
+
417
+ "type": "text",
418
+
419
+ "text": message,
420
+
421
+ },{
422
+
423
+ "type": "text",
424
+
425
+ "text": message2,
426
+
427
+ },{
428
+
429
+ "type": "text",
430
+
431
+ "text": message3,
432
+
433
+ }],
434
+
435
+ }),
436
+
437
+ });
438
+
439
+ return ContentService.createTextOutput(JSON.stringify({"content": "post ok"})).setMimeType(ContentService.MimeType.JSON);
440
+
441
+ }
442
+
443
+
444
+
445
+ function reply(replyToken, message) {
446
+
447
+ var url = "https://api.line.me/v2/bot/message/reply";
448
+
449
+ UrlFetchApp.fetch(url, {
450
+
451
+ "headers": {
452
+
453
+ "Content-Type": "application/json; charset=UTF-8",
454
+
455
+ "Authorization": "Bearer " + CHANNEL_ACCESS_TOKEN,
456
+
457
+ },
458
+
459
+ "method": "post",
460
+
461
+ "payload": JSON.stringify({
462
+
463
+ "replyToken": replyToken,
464
+
465
+ "messages": [{
466
+
467
+ "type": "text",
468
+
469
+ "text": message,
470
+
471
+ }],
472
+
473
+ }),
474
+
475
+ });
476
+
477
+ return ContentService.createTextOutput(JSON.stringify({"content": "post ok"})).setMimeType(ContentService.MimeType.JSON);
478
+
479
+ }
480
+
481
+
482
+
483
+ //今日の予定
484
+
485
+ function getEvents() {
486
+
487
+ var events = CalendarApp.getDefaultCalendar().getEventsForDay(new Date());
488
+
489
+ var body = "今日の予定は\n";
490
+
491
+
492
+
493
+ if (events.length === 0) {
494
+
495
+ body += "ありません。";
496
+
497
+ return body;
498
+
499
+ }
500
+
501
+
502
+
503
+ body += "\n";
504
+
505
+ events.forEach(function(event) {
506
+
507
+ var title = event.getTitle();
508
+
509
+ body += "????" + title + "\n";
510
+
511
+ });
512
+
513
+ body += "\nです!";
514
+
515
+ return body;
516
+
517
+ }
518
+
519
+
520
+
521
+ //7日間の予定
522
+
523
+ function notifyWeekly() {
524
+
525
+ var body = "1週間の予定は\n\n";
526
+
527
+ var weekday = ["日", "月", "火", "水", "木", "金", "土"];
528
+
529
+ for ( var i = 0; i < 7; i++ ) {
530
+
531
+
532
+
533
+ var dt = new Date();
534
+
535
+ dt.setDate(dt.getDate()+i);
536
+
537
+ var events = CalendarApp.getDefaultCalendar().getEventsForDay(dt);
538
+
539
+ body += Utilities.formatDate(dt, "JST", '???? MM/dd(' + weekday[dt.getDay()] + ')') + "\n";
540
+
541
+ if (events.length === 0) {
542
+
543
+ body += "ありません。\n";
544
+
545
+ }
546
+
547
+
548
+
549
+ events.forEach(function(event) {
550
+
551
+ var title = event.getTitle();
552
+
553
+ body += title + "\n";
554
+
555
+ });
556
+
557
+ }
558
+
559
+ return body;
560
+
561
+ }
562
+
563
+
564
+
565
+ function HmFormat(date){
566
+
567
+ return Utilities.formatDate(date, "JST", "HH:mm");
568
+
569
+ }
570
+
571
+
572
+
573
+
574
+
575
+
576
+
179
577
  ```
180
578
 
181
579
 

1

文言変更

2021/08/24 10:13

投稿

SA-KYO
SA-KYO

スコア37

test CHANGED
File without changes
test CHANGED
@@ -34,6 +34,162 @@
34
34
 
35
35
  ```JavaScript
36
36
 
37
+ switch (type) {
38
+
39
+ case "1":
40
+
41
+ //開始日時の追加
42
+
43
+ if (messageText.match(dateExp2)) {
44
+
45
+ var [matched, start_month, start_day] = messageText.match(dateExp2);
46
+
47
+ cache.put("type", 2);
48
+
49
+ cache.put("start_month", start_month);
50
+
51
+ cache.put("start_day", start_day);
52
+
53
+
54
+
55
+ //終了日時の質問
56
+
57
+ var year = new Date().getFullYear();
58
+
59
+ var startDate = new Date(year, cache.get("start_month") - 1, cache.get("start_day"));
60
+
61
+ //pushDatePicker("開始日は\n"+ EventFormat(startDate) + "\nですね。\n\n次に予定の終了日時をお知らせください。", userId);
62
+
63
+ pushDatePicker("  次に終了日を入力してください。", replyToken);
64
+
65
+
66
+
67
+ break;
68
+
69
+ } else {
70
+
71
+ reply(replyToken, "予定追加処理中です。\n「キャンセル」\nで追加作業をキャンセルします。");
72
+
73
+ break;
74
+
75
+ }
76
+
77
+ case "2":
78
+
79
+ // 終了日時の追加
80
+
81
+ if (messageText.match(dateExp2)) {
82
+
83
+ var [matched, end_month, end_day] = messageText.match(dateExp2);
84
+
85
+ cache.put("type", 3);
86
+
87
+ cache.put("end_month", end_month);
88
+
89
+ cache.put("end_day", end_day);
90
+
91
+ //予定名の質問
92
+
93
+ var year = new Date().getFullYear();
94
+
95
+ var endDate = new Date(year,cache.get("end_month")-1, cache.get("end_day"));
96
+
97
+
98
+
99
+ reply(replyToken, "終了日時は\n"+EventFormat(endDate)+"\n ですね。\n\n最後に予定名を教えてください。");
100
+
101
+ break;
102
+
103
+ }else{
104
+
105
+ reply(replyToken,"予定追加処理中です。\n「キャンセル」\nで追加作業をキャンセルします。");
106
+
107
+ break;
108
+
109
+ }
110
+
111
+ case "3":
112
+
113
+ //最終確認
114
+
115
+ cache.put("type", 4);
116
+
117
+ cache.put("title", messageText);
118
+
119
+ var [title, startDate, endDate] = createData(cache);
120
+
121
+ //予定追加の確認
122
+
123
+ replyPlans(replyToken, "【予定名】:"+title,"開始予定日:\n"+EventFormat(startDate)+"\n終了予定日:\n"+
124
+
125
+ EventFormat(new Date(endDate.setDate(endDate.getDate() - 1))),
126
+
127
+ "予定を追加しますか?\n「はい」か「いいえ」でお知らせください。");
128
+
129
+ break;
130
+
131
+ case "4":
132
+
133
+ if (messageText === "はい") {
134
+
135
+ cache.remove("type");
136
+
137
+ var [title, startDate, endDate]=createData(cache);
138
+
139
+ CalendarApp.getDefaultCalendar().createAllDayEvent(title, startDate, endDate);
140
+
141
+ reply(replyToken,"お疲れ様です☺️\nGoogleカレンダーに予定を追加しました!????");
142
+
143
+ } else if (messageText === "いいえ") {
144
+
145
+ cache.remove("type");
146
+
147
+ reply(replyToken, "予定の追加をキャンセルしました。");
148
+
149
+ } else {
150
+
151
+ reply(replyToken, "「はい」か「いいえ」でお答えください。");
152
+
153
+ break;
154
+
155
+ }
156
+
157
+ break;
158
+
159
+
160
+
161
+ function createData(cache) {
162
+
163
+ var year = new Date().getFullYear();
164
+
165
+ //var year = 2020;
166
+
167
+ var title = cache.get("title");
168
+
169
+ var startDate = new Date(year, cache.get("start_month") - 1, cache.get("start_day"));
170
+
171
+ var endDate = new Date(year, cache.get("end_month") - 1, cache.get("end_day"));
172
+
173
+ endDate = new Date(endDate.setDate(endDate.getDate() + 1));
174
+
175
+ return [title, startDate, endDate];
176
+
177
+ }
178
+
179
+ ```
180
+
181
+
182
+
183
+
184
+
185
+
186
+
187
+ ### 試したこと
188
+
189
+
190
+
191
+ ```JavaScript
192
+
37
193
  "actions": [
38
194
 
39
195
  {
@@ -54,15 +210,7 @@
54
210
 
55
211
  ```
56
212
 
57
-
58
-
59
-
60
-
61
-
62
-
63
- ### 試したこと
213
+
64
-
65
-
66
214
 
67
215
  ```JavaScript
68
216
 
@@ -72,13 +220,13 @@
72
220
 
73
221
  "type": "datetimepicker",
74
222
 
75
- "label": "ここをタップ",
223
+ "label": "ここをタップ",
76
224
 
77
225
  "data": "action=settime",
78
226
 
79
- "mode": "datetime",
227
+ "mode": "date",
80
-
228
+
81
- "initial": Utilities.formatDate(new Date(), "Asia/Tokyo", "yyyy-MM-dd'T'HH:mm"),
229
+ "initial": Utilities.formatDate(new Date(), "Asia/Tokyo", "yyyy-MM-dd"),
82
230
 
83
231
  // "max": "2018-01-24t23:59",
84
232
 
@@ -86,30 +234,6 @@
86
234
 
87
235
  ```
88
236
 
89
-
90
-
91
- ```JavaScript
92
-
93
- "actions": [
94
-
95
- {
96
-
97
- "type": "datetimepicker",
98
-
99
- "label": "ここをタップ!",
100
-
101
- "data": "action=settime",
102
-
103
- "mode": "date",
104
-
105
- "initial": Utilities.formatDate(new Date(), "Asia/Tokyo", "yyyy-MM-dd"),
106
-
107
- // "max": "2018-01-24t23:59",
108
-
109
- // "min": "2017-12-25t00:00"
110
-
111
- ```
112
-
113
237
  に書き換えたが、
114
238
 
115
239
  日付のみにはなったが