質問編集履歴

4

コードの編集

2022/01/16 00:19

投稿

nanachannn
nanachannn

スコア10

test CHANGED
File without changes
test CHANGED
@@ -9,7 +9,7 @@
9
9
  何か解決法分かりましたら教えていただきたいです🙏😭
10
10
  コメントアウトが多く見づらいかもしれません。すみません🙇🏻‍♀️
11
11
 
12
- LineBotApplicationクラス
12
+ **LineBotApplicationクラス**
13
13
  ```Java
14
14
  package com.example.linebot;
15
15
 
@@ -24,9 +24,173 @@
24
24
  public static void main(String[] args) {
25
25
  SpringApplication.run(LinebotApplication.class, args);
26
26
  }
27
-
28
27
  }
29
28
  ```
30
29
 
30
+ **Answerクラス**
31
+ ```Java
31
- Answerクラス、Callbackクラス、Searchクラス、の順に写真を添付しています。
32
+ package com.example.linebot.replier;
32
33
 
34
+ import com.linecorp.bot.model.event.PostbackEvent;
35
+ import com.linecorp.bot.model.message.Message;
36
+ import com.linecorp.bot.model.message.TextMessage;
37
+
38
+ public class Answer implements Replier {
39
+
40
+ private PostbackEvent event;
41
+
42
+ public Answer(PostbackEvent event) {
43
+ this.event = event;
44
+ }
45
+
46
+ @Override
47
+ public Message reply() {
48
+ String actionLabel = this.event.getPostbackContent().getData();
49
+ switch (actionLabel) {
50
+ case "CS":
51
+ return new TextMessage("以下のような検索サイトがあります");
52
+ }
53
+ return new TextMessage("?");
54
+ }
55
+ }
56
+ ```
57
+
58
+ **Callbackクラス**
59
+ ```Java
60
+ package com.example.linebot;
61
+
62
+ import com.example.linebot.replier.*;
63
+ import com.linecorp.bot.client.LineBlobClient;
64
+ import com.linecorp.bot.model.event.FollowEvent;
65
+ import com.linecorp.bot.model.event.PostbackEvent;
66
+ import com.linecorp.bot.model.message.Message;
67
+ import com.linecorp.bot.spring.boot.annotation.EventMapping;
68
+ import com.linecorp.bot.spring.boot.annotation.LineMessageHandler;
69
+ import org.slf4j.Logger;
70
+ import org.slf4j.LoggerFactory;
71
+
72
+ import com.linecorp.bot.model.event.message.TextMessageContent;
73
+ import com.linecorp.bot.model.event.MessageEvent;
74
+
75
+ import org.springframework.beans.factory.annotation.Autowired;
76
+
77
+ @LineMessageHandler
78
+ public class Callback {
79
+
80
+ private static final Logger log =
81
+ LoggerFactory.getLogger(Callback.class);
82
+ private LineBlobClient client; //追加3
83
+
84
+ @Autowired
85
+ public Callback(LineBlobClient client) {
86
+ this.client = client;
87
+ } //追加3
88
+
89
+ // フォローイベントに対応する
90
+ @EventMapping
91
+ public Message handleFollow(FollowEvent event) {
92
+ // 実際はこのタイミングでフォロワーのユーザIDをデータベースに格納しておくなど
93
+ Follow follow = new Follow(event);
94
+ return follow.reply();
95
+ }
96
+
97
+ // 文章で話しかけられたとき(テキストメッセージのイベント)に対応する
98
+ @EventMapping
99
+ public Message handleMessage(MessageEvent<TextMessageContent> event) {
100
+ TextMessageContent tmc = event.getMessage();
101
+ String text = tmc.getText();
102
+ switch (text) {
103
+ case "検索":
104
+ Search search = new Search();
105
+ return search.Button();
106
+ case "検索2":
107
+ Search search2 = new Search();
108
+ return search2.Button2();
109
+ default:
110
+ Parrot parrot = new Parrot(event);
111
+ return parrot.reply();
112
+ }
113
+ }
114
+
115
+ // PostBackEventに対応する
116
+ @EventMapping
117
+ public Message handlePostBack(PostbackEvent event) {
118
+ Answer answer = new Answer(event);
119
+ return answer.reply();
120
+ }
121
+ }
122
+ ```
123
+
124
+ **Searchクラス**
125
+ ```Java
126
+ package com.example.linebot.replier;
127
+
128
+ import com.linecorp.bot.client.LineMessagingClient;
129
+ import com.linecorp.bot.model.PushMessage;
130
+ import com.linecorp.bot.model.action.PostbackAction;
131
+ import com.linecorp.bot.model.message.Message;
132
+ import com.linecorp.bot.model.message.TemplateMessage;
133
+ import com.linecorp.bot.model.message.TextMessage;
134
+ import com.linecorp.bot.model.message.template.ConfirmTemplate;
135
+ import com.linecorp.bot.model.response.BotApiResponse;
136
+ import org.slf4j.Logger;
137
+ import org.slf4j.LoggerFactory;
138
+ import org.springframework.beans.factory.annotation.Autowired;
139
+ import org.springframework.web.bind.annotation.RestController;
140
+
141
+ import java.net.URI;
142
+
143
+ import com.linecorp.bot.model.action.URIAction;
144
+
145
+ @RestController
146
+ public class Search {
147
+
148
+ private static final Logger log = LoggerFactory.getLogger(Search.class);
149
+
150
+ // push先のユーザID(本来は、友達登録をした瞬間にDBなどに格納しておく)
151
+ private String userId = "U7804359fb24710b6c35746d6c5dfca0d";
152
+
153
+ private LineMessagingClient client;
154
+
155
+ @Autowired
156
+ public void Search(LineMessagingClient lineMessagingClient) {
157
+ this.client = lineMessagingClient;
158
+ }
159
+
160
+ //検索エンジンの選択ボタン
161
+ public Message Button() {
162
+ try {
163
+ Message msg = new TemplateMessage("検索エンジン",
164
+ new ConfirmTemplate("あなたが検索したいサイトは?",
165
+ new URIAction("Google", URI.create("https://www.google.co.jp/"),
166
+ new URIAction.AltUri(URI.create("https://www.google.co.jp/"))),
167
+ new URIAction("Yahoo!", URI.create("https://www.yahoo.co.jp/"),
168
+ new URIAction.AltUri(URI.create("https://www.yahoo.co.jp/")))
169
+ ));
170
+ return msg;
171
+ } catch (Exception e) {
172
+ return new TextMessage("問題が発生しました");
173
+ }
174
+ }
175
+
176
+ @Autowired
177
+ public Message Button2() {
178
+ try {
179
+ Message msg2 = new TemplateMessage("検索エンジン",
180
+ new ConfirmTemplate("あなたが検索したいサイトは?",
181
+ new URIAction("Bing", URI.create("https://www.bing.co.jp/"),
182
+ new URIAction.AltUri(URI.create("https://www.bing.co.jp/"))),
183
+ new PostbackAction("その他", "CS")));
184
+
185
+ PushMessage pMsg = new PushMessage(userId, msg2);
186
+ BotApiResponse resp = client.pushMessage(pMsg).get();
187
+ log.info("Sent messages: {}", resp);
188
+
189
+ return msg2;
190
+ } catch(Exception e) {
191
+ return new TextMessage("問題が発生しました");
192
+ }
193
+ }
194
+ }
195
+ ```
196
+

3

写真の追加

2022/01/16 00:11

投稿

nanachannn
nanachannn

スコア10

test CHANGED
File without changes
test CHANGED
@@ -9,12 +9,24 @@
9
9
  何か解決法分かりましたら教えていただきたいです🙏😭
10
10
  コメントアウトが多く見づらいかもしれません。すみません🙇🏻‍♀️
11
11
 
12
+ LineBotApplicationクラス
13
+ ```Java
14
+ package com.example.linebot;
15
+
16
+ import com.linecorp.bot.spring.boot.annotation.LineMessageHandler;
17
+ import org.springframework.boot.SpringApplication;
18
+ import org.springframework.boot.autoconfigure.SpringBootApplication;
19
+
20
+ @SpringBootApplication
21
+ @LineMessageHandler
22
+ public class LinebotApplication {
23
+
24
+ public static void main(String[] args) {
25
+ SpringApplication.run(LinebotApplication.class, args);
26
+ }
27
+
28
+ }
29
+ ```
30
+
12
- LineBotApplicationクラス、Answerクラス、Callbackクラス、Searchクラス、の順に写真を添付しています。
31
+ Answerクラス、Callbackクラス、Searchクラス、の順に写真を添付しています。
13
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/87c48c98-8d08-448c-9e9c-76dc5c828286.jpeg)
32
+
14
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/ab03be6a-1c19-4132-8d64-1f821e39c71d.jpeg)
15
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/d50c257e-6086-415b-a83b-87046c48c316.jpeg)
16
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/b0efda04-77d6-4312-9455-4dc1177ec6d0.jpeg)
17
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/9e294a68-64a4-4c1a-8646-fc9d2c62cf35.jpeg)
18
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/bcac84c9-7b52-41eb-9c1b-bf36e0663d4d.jpeg)
19
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/8269dd44-709c-4957-bfbb-147b8eb024df.jpeg)
20
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/953a786f-1cec-4e91-a36e-79e7498139df.jpeg)

2

画像の追加

2022/01/15 23:44

投稿

nanachannn
nanachannn

スコア10

test CHANGED
File without changes
test CHANGED
@@ -10,11 +10,11 @@
10
10
  コメントアウトが多く見づらいかもしれません。すみません🙇🏻‍♀️
11
11
 
12
12
  LineBotApplicationクラス、Answerクラス、Callbackクラス、Searchクラス、の順に写真を添付しています。
13
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/4ceedeca-6edc-4efd-8ad5-592b4adc8b28.jpeg)
14
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/b2f52fd5-6c9d-4dfa-8359-467632009a07.jpeg)
15
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/6aaf2b08-d010-463c-8799-6fd4598c723f.jpeg)]
13
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/87c48c98-8d08-448c-9e9c-76dc5c828286.jpeg)
14
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/ab03be6a-1c19-4132-8d64-1f821e39c71d.jpeg)
15
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/d50c257e-6086-415b-a83b-87046c48c316.jpeg)
16
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/254e9b64-7ad8-42ef-aa56-30632070b493.jpeg)]
16
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/b0efda04-77d6-4312-9455-4dc1177ec6d0.jpeg)
17
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/9e294a68-64a4-4c1a-8646-fc9d2c62cf35.jpeg)
18
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/bcac84c9-7b52-41eb-9c1b-bf36e0663d4d.jpeg)
17
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/068b1546-eb38-46a6-b3d8-bd5b53f10254.jpeg)]
19
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/8269dd44-709c-4957-bfbb-147b8eb024df.jpeg)
18
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/9389345f-74bd-409c-aeba-4bae24dec909.jpeg)]
20
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/953a786f-1cec-4e91-a36e-79e7498139df.jpeg)
19
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/fb71a40f-04ed-452e-9e28-4a42a3200cd6.jpeg)]
20
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/3d07a675-03f2-4e5d-adc3-b3aa441a7a93.jpeg)

1

2022/01/15 23:41

投稿

nanachannn
nanachannn

スコア10

test CHANGED
File without changes
test CHANGED
@@ -12,4 +12,9 @@
12
12
  LineBotApplicationクラス、Answerクラス、Callbackクラス、Searchクラス、の順に写真を添付しています。
13
13
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/4ceedeca-6edc-4efd-8ad5-592b4adc8b28.jpeg)
14
14
  ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/b2f52fd5-6c9d-4dfa-8359-467632009a07.jpeg)
15
- ![![![![![![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/6aaf2b08-d010-463c-8799-6fd4598c723f.jpeg)](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/254e9b64-7ad8-42ef-aa56-30632070b493.jpeg)](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/068b1546-eb38-46a6-b3d8-bd5b53f10254.jpeg)](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/9389345f-74bd-409c-aeba-4bae24dec909.jpeg)](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/fb71a40f-04ed-452e-9e28-4a42a3200cd6.jpeg)](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/3d07a675-03f2-4e5d-adc3-b3aa441a7a93.jpeg)
15
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/6aaf2b08-d010-463c-8799-6fd4598c723f.jpeg)]
16
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/254e9b64-7ad8-42ef-aa56-30632070b493.jpeg)]
17
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/068b1546-eb38-46a6-b3d8-bd5b53f10254.jpeg)]
18
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/9389345f-74bd-409c-aeba-4bae24dec909.jpeg)]
19
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/fb71a40f-04ed-452e-9e28-4a42a3200cd6.jpeg)]
20
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-01-16/3d07a675-03f2-4e5d-adc3-b3aa441a7a93.jpeg)