Skip to content

Commit

Permalink
Fix Bug Can't replyMessage. fixes #290 @2h
Browse files Browse the repository at this point in the history
  • Loading branch information
みぞ@CrazyBeatCoder committed Jan 21, 2018
1 parent 362ccd7 commit 8786065
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
8 changes: 0 additions & 8 deletions lily-white-line-bot.iml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,5 @@
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.9" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.9" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.linecorp.bot:line-bot-api-client:1.14.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.9" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.8.9" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okhttp3:logging-interceptor:3.8.1" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.8.1" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okio:okio:1.13.0" level="project" />
<orderEntry type="library" name="Maven: com.squareup.retrofit2:converter-jackson:2.3.0" level="project" />
<orderEntry type="library" name="Maven: com.squareup.retrofit2:retrofit:2.3.0" level="project" />
</component>
</module>
21 changes: 17 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,25 @@ Copyright 2015 Google Inc.
<artifactId>line-bot-model</artifactId>
<version>1.14.0</version>
</dependency>
<!-- [END Java SDK for Messaging API BOT] -->

<!-- [START Jackson] -->
<dependency>
<groupId>com.linecorp.bot</groupId>
<artifactId>line-bot-api-client</artifactId>
<version>1.14.0</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.9</version>
</dependency>
<!-- [END Java SDK for Messaging API BOT] -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.0</version>
</dependency>
<!-- [END Jackson] -->

</dependencies>
<build>
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/com/mizo0203/lilywhite/repo/LineRepository.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.mizo0203.lilywhite.repo;

import com.linecorp.bot.client.LineMessagingClient;
import com.linecorp.bot.client.LineMessagingClientBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.linecorp.bot.model.ReplyMessage;
import com.linecorp.bot.model.response.BotApiResponse;
import com.mizo0203.lilywhite.repo.line.messaging.data.MessageObject;
import com.mizo0203.lilywhite.repo.line.messaging.data.PushMessageData;
import com.mizo0203.lilywhite.repo.line.messaging.data.webHook.event.RequestBody;
Expand All @@ -16,29 +14,31 @@
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;

/** 注意: com.linecorp.bot:line-bot-api-client は Java App Engine アプリケーションで使用できない */
/* package */ class LineRepository {

private static final Logger LOG = Logger.getLogger(LineRepository.class.getName());

private static final String MESSAGING_API_REPLY_MESSAGE_URL_STR =
"https://api.line.me/v2/bot/message/reply";
private static final String MESSAGING_API_PUSH_MESSAGE_URL_STR =
"https://api.line.me/v2/bot/message/push";
private static final String MESSAGING_API_IDS_MEMBERS_GROUP_URL_STR =
"https://api.line.me/v2/bot/group/%s/members/ids";

private final LineMessagingClient mLineMessagingClient;
private String mChannelAccessToken;

LineRepository(String channelAccessToken) {
mLineMessagingClient = new LineMessagingClientBuilder(channelAccessToken).build();
mChannelAccessToken = channelAccessToken;
}

@SuppressWarnings("EmptyMethod")
Expand All @@ -61,17 +61,17 @@ public void destroy() {
*/
public void replyMessage(ReplyMessage replyMessage) {
try {
BotApiResponse resp = mLineMessagingClient.replyMessage(replyMessage).get();
dumpBotApiResponse(resp);
} catch (InterruptedException | ExecutionException e) {
LOG.log(Level.SEVERE, "replyMessage", e);
final URL url = new URL(MESSAGING_API_REPLY_MESSAGE_URL_STR);
final Map<String, String> reqProp = new HashMap<>();
reqProp.put("Content-Type", "application/json");
reqProp.put("Authorization", "Bearer " + mChannelAccessToken);
final String body = PaserUtil.toJson(replyMessage);
HttpUtil.post(url, reqProp, body, null);
} catch (JsonProcessingException | MalformedURLException e) {
e.printStackTrace();
}
}

private void dumpBotApiResponse(BotApiResponse resp) {
LOG.info(resp.toString());
}

/**
* プッシュメッセージを送る
*
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/mizo0203/lilywhite/util/PaserUtil.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.mizo0203.lilywhite.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.linecorp.bot.model.ReplyMessage;
import com.mizo0203.lilywhite.repo.line.messaging.data.PushMessageData;
import com.mizo0203.lilywhite.repo.line.messaging.data.ReplyMessageData;
import com.mizo0203.lilywhite.repo.line.messaging.data.webHook.event.RequestBody;

import java.io.BufferedReader;
Expand All @@ -18,9 +20,9 @@ public static RequestBody parseWebhooksData(String json) {
return new Gson().fromJson(json, RequestBody.class);
}

/** @see Gson#fromJson(String, Class) */
public static String toJson(ReplyMessageData data) {
return new Gson().toJson(data, ReplyMessageData.class);
/** @see ObjectMapper#writeValueAsString(Object) */
public static String toJson(ReplyMessage data) throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(data);
}

/** @see Gson#fromJson(String, Class) */
Expand Down

0 comments on commit 8786065

Please sign in to comment.