質問編集履歴
1
コードを追加しました。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Javaでスケジュール管理をするLINE BOTを作りたい
|
1
|
+
Javaでスケジュール管理をするLINE BOTを作りたいが、うまくいかない
|
body
CHANGED
@@ -1,18 +1,14 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
-
|
3
|
-
|
2
|
+
javaでスケジュール管理を行えるLINE BOTを作りたいと考えています。
|
4
|
-
|
5
3
|
リッチメニューの『予定追加』からスケジュールの追加(日付入力の後に予定の内容を送信する)、
|
6
4
|
『予定確認』で登録されているスケジュールを
|
7
5
|
「○/○ バイト
|
8
6
|
○/○ 課題締切」
|
9
7
|
などというように返信し、普通に文章を送信するとオウム返しをするようなものを完成形として想定しています。
|
10
|
-
|
11
8
|
しかし、予定を登録して表示させようと思っても何も反応が返ってきませんでした。(オウム返しの方だけはちゃんと返信が返ってきました)
|
12
|
-
調べて出てきたものを少しずつ改変したり応用したりするような形でコード作成をしているのですが、
|
9
|
+
調べて出てきたものを少しずつ改変したり応用したりするような形でコード作成をしているのですが、どうすればうまくいくかわかりません。
|
13
10
|
|
14
|
-
###
|
11
|
+
### ソースコード
|
15
|
-
|
16
12
|
```java
|
17
13
|
package com.example.linebot.replier;
|
18
14
|
|
@@ -23,25 +19,18 @@
|
|
23
19
|
import com.linecorp.bot.model.message.TextMessage;
|
24
20
|
|
25
21
|
public class DialogAnswer implements Replier {
|
26
|
-
|
27
22
|
private PostbackEvent event;
|
28
|
-
|
29
23
|
public DialogAnswer(PostbackEvent event) {
|
30
24
|
this.event = event;
|
31
25
|
}
|
32
|
-
|
33
26
|
private MessageEvent<TextMessageContent> event1;
|
34
|
-
|
35
27
|
public DialogAnswer(MessageEvent<TextMessageContent> event1) {
|
36
28
|
this.event1 = event1;
|
37
29
|
}
|
38
|
-
|
39
|
-
// PostBackEventに対応する
|
40
30
|
@Override
|
41
31
|
public Message reply() {
|
42
32
|
String actionLabel = this.event.getPostbackContent().getData();
|
43
33
|
switch (actionLabel) {
|
44
|
-
|
45
34
|
case "DT":
|
46
35
|
List list = new List(this.event.getPostbackContent().getParams().toString());
|
47
36
|
TextMessageContent tmc = this.event1.getMessage();
|
@@ -52,27 +41,23 @@
|
|
52
41
|
}
|
53
42
|
}
|
54
43
|
```
|
55
|
-
|
56
44
|
```java
|
57
45
|
package com.example.linebot.replier;
|
58
46
|
|
59
47
|
import com.linecorp.bot.model.event.message.TextMessageContent;
|
60
|
-
|
61
48
|
import java.util.ArrayList;
|
62
49
|
|
63
|
-
// 予定
|
50
|
+
// 予定追加
|
64
51
|
public class List {
|
65
52
|
private String date;
|
66
53
|
private ArrayList<String> list = new ArrayList<>();
|
67
54
|
private String scheduleList;
|
68
|
-
|
69
55
|
public List(String date){
|
70
56
|
this.date = date;
|
71
57
|
this.scheduleList = "";
|
72
58
|
}
|
73
59
|
public List(){
|
74
60
|
}
|
75
|
-
|
76
61
|
public void add(TextMessageContent schedule){
|
77
62
|
String text = String.format(date + " " + schedule + "\n");
|
78
63
|
list.add(text);
|
@@ -80,35 +65,26 @@
|
|
80
65
|
scheduleList = String.format(scheduleList + list1);
|
81
66
|
}
|
82
67
|
}
|
83
|
-
|
84
68
|
public String getScheduleList(){
|
85
69
|
return scheduleList;
|
86
70
|
}
|
87
|
-
|
88
71
|
}
|
89
72
|
```
|
90
|
-
|
91
73
|
```java
|
92
74
|
package com.example.linebot.replier;
|
93
75
|
|
94
|
-
|
95
76
|
import com.linecorp.bot.model.message.Message;
|
96
77
|
import com.linecorp.bot.model.message.TextMessage;
|
97
78
|
|
98
|
-
|
99
79
|
// 予定確認
|
100
80
|
public class Verification implements Replier {
|
101
|
-
|
102
81
|
@Override
|
103
82
|
public Message reply() {
|
104
83
|
List scheduleList = new List();
|
105
84
|
return new TextMessage(scheduleList.getScheduleList());
|
106
85
|
}
|
107
|
-
|
108
86
|
}
|
109
|
-
|
110
87
|
```
|
111
|
-
|
112
88
|
```jaca
|
113
89
|
package com.example.linebot;
|
114
90
|
|
@@ -119,38 +95,25 @@
|
|
119
95
|
import com.linecorp.bot.spring.boot.annotation.LineMessageHandler;
|
120
96
|
import org.slf4j.Logger;
|
121
97
|
import org.slf4j.LoggerFactory;
|
122
|
-
|
123
|
-
//オウム返し
|
124
98
|
import com.linecorp.bot.model.event.message.TextMessageContent;
|
125
99
|
import com.linecorp.bot.model.event.MessageEvent;
|
126
|
-
|
127
|
-
//ユーザーの回答に反応
|
128
100
|
import com.linecorp.bot.model.event.PostbackEvent;
|
129
101
|
|
130
|
-
@LineMessageHandler
|
102
|
+
@LineMessageHandler
|
131
103
|
public class Callback {
|
132
|
-
|
133
104
|
private static final Logger log = LoggerFactory.getLogger(Callback.class);
|
134
105
|
|
135
|
-
// フォローイベントに対応する
|
136
106
|
@EventMapping
|
137
107
|
public Message handleFollow(FollowEvent event) {
|
138
|
-
// 実際はこのタイミングでフォロワーのユーザIDをデータベースにに格納しておくなど
|
139
108
|
Follow follow = new Follow(event);
|
140
109
|
return follow.reply();
|
141
110
|
}
|
142
111
|
|
143
|
-
// 文章で話しかけられたとき(テキストメッセージのイベント)に対応する
|
144
112
|
@EventMapping
|
145
|
-
// MessageEvent<TextMessageContent> は、LineBotに送られたテキスト文章を表すクラス
|
146
113
|
public Message handleMessage(MessageEvent<TextMessageContent> event) {
|
147
114
|
TextMessageContent tmc = event.getMessage();
|
148
115
|
String text = tmc.getText();
|
149
116
|
switch (text) {
|
150
|
-
case "やあ":
|
151
|
-
//時間帯に合わせて返信
|
152
|
-
Greet greet = new Greet();
|
153
|
-
return greet.reply();
|
154
117
|
case "予定確認":
|
155
118
|
// リッチメニューから送信
|
156
119
|
Verification verification = new Verification();
|
@@ -162,7 +125,7 @@
|
|
162
125
|
}
|
163
126
|
}
|
164
127
|
|
165
|
-
// PostBackEventに対応する
|
128
|
+
// PostBackEventに対応する
|
166
129
|
@EventMapping
|
167
130
|
public Message handlePostBack(PostbackEvent event) {
|
168
131
|
DialogAnswer dialogAnswer = new DialogAnswer(event);
|
@@ -171,7 +134,6 @@
|
|
171
134
|
|
172
135
|
}
|
173
136
|
```
|
174
|
-
|
175
137
|
```java
|
176
138
|
package com.example.linebot;
|
177
139
|
|
@@ -187,7 +149,6 @@
|
|
187
149
|
import org.springframework.core.io.ClassPathResource;
|
188
150
|
import org.springframework.web.bind.annotation.GetMapping;
|
189
151
|
import org.springframework.web.bind.annotation.RestController;
|
190
|
-
|
191
152
|
import java.io.IOException;
|
192
153
|
import java.nio.file.Files;
|
193
154
|
import java.time.LocalDateTime;
|
@@ -197,7 +158,6 @@
|
|
197
158
|
|
198
159
|
@RestController
|
199
160
|
public class RichMenuController {
|
200
|
-
|
201
161
|
private static final Logger log = LoggerFactory.getLogger(Push.class);
|
202
162
|
|
203
163
|
// push先のユーザID(本来は、友達登録をした瞬間にDBなどに格納しておく)
|
@@ -205,22 +165,15 @@
|
|
205
165
|
// 伏せてありますが、ここにはMessaging APIのチャネル基本設定にある「あなたのユーザーID」を入れています
|
206
166
|
|
207
167
|
private final LineMessagingClient messagingClient;
|
208
|
-
|
209
168
|
private final LineBlobClient blobClient;
|
210
|
-
|
211
169
|
@Autowired
|
212
170
|
public RichMenuController(LineMessagingClient lineMessagingClient, LineBlobClient blobClient) {
|
213
171
|
this.messagingClient = lineMessagingClient;
|
214
172
|
this.blobClient = blobClient;
|
215
173
|
}
|
216
|
-
|
217
|
-
// リッチーメニューを作成する
|
218
174
|
@GetMapping("addRich")
|
219
175
|
public String addRichMenu() {
|
220
176
|
String text = "リッチメニューを作成し、ユーザーに紐付けます";
|
221
|
-
|
222
|
-
// ①リッチメニューを作成
|
223
|
-
// それぞれの意味は https://developers.line.me/ja/reference/messaging-api/#rich-menu-object を参照
|
224
177
|
RichMenu richMenu = RichMenu.builder()
|
225
178
|
.name("リッチメニュー1")
|
226
179
|
.chatBarText("コントローラー")
|
@@ -228,22 +181,15 @@
|
|
228
181
|
.selected(true)
|
229
182
|
.size(RichMenuSize.FULL)
|
230
183
|
.build();
|
231
|
-
|
232
184
|
try {
|
233
|
-
|
234
|
-
// ②作成したリッチメニューの登録( resp1 は作成結果で、リッチメニューIDが入っている)
|
235
185
|
RichMenuIdResponse resp1 = messagingClient.createRichMenu(richMenu).get();
|
236
186
|
log.info("create richmenu:{}", resp1);
|
237
|
-
|
187
|
+
|
238
|
-
// ここでは、src/resource/img/RichMenuSample.jpg(ビルド後は classpath:/img/RichMenuSample.jpg)を指定
|
239
|
-
// 画像の仕様は公式ドキュメントを参照されたい
|
240
188
|
ClassPathResource cpr = new ClassPathResource("/img/RichMenu.png");
|
241
189
|
byte[] fileContent = Files.readAllBytes(cpr.getFile().toPath());
|
242
190
|
BotApiResponse resp2 = blobClient.setRichMenuImage(resp1.getRichMenuId(), "image/jpeg", fileContent).get();
|
243
191
|
log.info("set richmenu image:{}", resp2);
|
244
192
|
|
245
|
-
// ④リッチメニューIdをユーザIdとリンクする( resp3 は、紐付け結果)
|
246
|
-
// リンクすることで作成したリッチメニューを使えるようになる
|
247
193
|
BotApiResponse resp3 = messagingClient.linkRichMenuIdToUser(userId, resp1.getRichMenuId()).get();
|
248
194
|
log.info("link richmenu:{}", resp3);
|
249
195
|
|
@@ -252,22 +198,15 @@
|
|
252
198
|
}
|
253
199
|
return text;
|
254
200
|
}
|
255
|
-
|
256
201
|
@GetMapping("delRich")
|
257
202
|
public String delRichMenu() {
|
258
203
|
String text = "リッチメニューをすべて削除します";
|
259
204
|
try {
|
260
|
-
|
261
|
-
// ①ユーザからリッチメニューを解除する(※Messaging APIで作成したものだけ)
|
262
205
|
messagingClient.unlinkRichMenuIdFromUser(userId);
|
263
206
|
|
264
|
-
// ②作成されているリッチメニューの取得( resp4 は、リッチメニューの一覧情報)
|
265
207
|
RichMenuListResponse resp4 = messagingClient.getRichMenuList().get();
|
266
208
|
log.info("get richmenus:{}", resp4);
|
267
209
|
|
268
|
-
// ③リッチメニューIdを指定して削除する
|
269
|
-
// ここでは resp4 のものをすべて削除しているが、本来はリッチメニューIdと
|
270
|
-
// ユーザIDの対応をDBなどに保存しておいて、不要なものだけを削除する
|
271
210
|
resp4.getRichMenus().stream()
|
272
211
|
.forEach(r -> messagingClient.deleteRichMenu(r.getRichMenuId()));
|
273
212
|
|
@@ -276,25 +215,16 @@
|
|
276
215
|
}
|
277
216
|
return text;
|
278
217
|
}
|
279
|
-
|
280
|
-
// 画像のどの部分(ピクセル)に、どんな動作をするリッチメニューを割り当てるか設定します
|
281
218
|
private List<RichMenuArea> makeRichMenuAreas() {
|
282
219
|
final ArrayList<RichMenuArea> richMenuAreas = new ArrayList<>();
|
283
|
-
// 予定追加
|
284
220
|
richMenuAreas.add(makeDateTimeAction(0, 0, 1250, 1686, "予定追加"));
|
285
|
-
// 予定確認
|
286
221
|
richMenuAreas.add(makeMessageAction(1250, 0, 1250, 1686, "予定確認"));
|
287
222
|
return richMenuAreas;
|
288
223
|
}
|
289
|
-
|
290
|
-
// Botにメッセージを送信する動作をリッチメニューとして割り当てます
|
291
224
|
private RichMenuArea makeMessageAction(int x, int y, int w, int h, String label) {
|
292
225
|
return new RichMenuArea(new RichMenuBounds(x, y, w, h),
|
293
|
-
// 「予定確認」と送信
|
294
226
|
new MessageAction(label, label));
|
295
227
|
}
|
296
|
-
|
297
|
-
// Botに日時イベントを送信する動作をリッチメニューとして割り当てます
|
298
228
|
private RichMenuArea makeDateTimeAction(int x, int y, int w, int h, String label) {
|
299
229
|
return new RichMenuArea(new RichMenuBounds(x, y, w, h),
|
300
230
|
DatetimePickerAction.OfLocalDatetime.builder()
|
@@ -306,4 +236,53 @@
|
|
306
236
|
.build());
|
307
237
|
}
|
308
238
|
}
|
239
|
+
```
|
240
|
+
```java
|
241
|
+
package com.example.linebot.replier;
|
242
|
+
import com.linecorp.bot.model.event.FollowEvent;
|
243
|
+
import com.linecorp.bot.model.message.Message;
|
244
|
+
import com.linecorp.bot.model.message.TextMessage;
|
245
|
+
import java.util.ArrayList;
|
246
|
+
|
247
|
+
// フォローされた時用の返信クラス
|
248
|
+
public class Follow implements Replier {
|
249
|
+
private FollowEvent event;
|
250
|
+
public Follow(FollowEvent event) {
|
251
|
+
this.event = event;
|
252
|
+
}
|
253
|
+
@Override
|
254
|
+
public Message reply() {
|
255
|
+
String userId = this.event.getSource().getUserId();
|
256
|
+
//ユーザーID記録
|
257
|
+
ArrayList<String> userid = new ArrayList<>();
|
258
|
+
userid.add(userId);
|
259
|
+
String text = String.format(
|
260
|
+
"こんにちは!\n" +
|
261
|
+
"スケジュール管理をするBotです。\n" +
|
262
|
+
"\n" +
|
263
|
+
"「予定登録」で予定を登録します。\n" +
|
264
|
+
"「予定確認」で登録されたスケジュールを確認できます。");
|
265
|
+
return new TextMessage(text);
|
266
|
+
}
|
267
|
+
}
|
268
|
+
```
|
269
|
+
```java
|
270
|
+
package com.example.linebot.replier;
|
271
|
+
import com.linecorp.bot.model.message.Message;
|
272
|
+
public interface Replier {
|
273
|
+
Message reply();
|
274
|
+
}
|
275
|
+
```
|
276
|
+
```java
|
277
|
+
package com.example.linebot;
|
278
|
+
import org.springframework.boot.SpringApplication;
|
279
|
+
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
280
|
+
import org.springframework.scheduling.annotation.EnableScheduling;
|
281
|
+
@SpringBootApplication
|
282
|
+
@EnableScheduling
|
283
|
+
public class LinebotApplication {
|
284
|
+
public static void main(String[] args) {
|
285
|
+
SpringApplication.run(LinebotApplication.class, args);
|
286
|
+
}
|
287
|
+
}
|
309
288
|
```
|