Skip to content

Commit 4264763

Browse files
committed
update: 토큰 만료되면 키보드 버튼 보내는 방식으로 수정 (sharetreats-team#43)
1 parent 720d1a4 commit 4264763

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/main/java/com/sharetreats/chatbot/module/controller/WebhookController.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ public ResponseEntity<?> webhook(@RequestBody String callback) {
5252
if (event.equals(MESSAGE)) {
5353
String accountId = getSenderId(callback);
5454
boolean isValidToken = tokenConfig.validateToken(accountId);
55-
if (isValidToken)
56-
return sendResponseByTextInMessage(callback);
55+
boolean isRetry = validateRetry(callback);
56+
if (isValidToken || isRetry) {
57+
if (!isValidToken) tokenConfig.generateToken(accountId);
58+
return sendResponseByTextInMessage(callback);
59+
}
5760
else return sendInvalidTokenMessage.execute(accountId);
5861
}
5962
if (event.equals(UNSUBSCRIBED))
@@ -79,7 +82,7 @@ private ResponseEntity<?> sendResponseByTextInMessage(String callback) {
7982
return sendProductsOfBrand.execute(callback);
8083
if (isContains(text, SEND_TREATS) || isContains(text, NO_DISCOUNT) || isTrackingDataValid(trackingData))
8184
return sendPurchaseInfo.execute(callback);
82-
if (isContains(text, VIEW_BRANDS)) {
85+
if (isContains(text, VIEW_BRANDS) || isContains(text, RETRY)) {
8386
manageSubscription.validateAccount(callback);
8487
return sendCategoryKeyboard.execute(callback);
8588
}
@@ -128,6 +131,14 @@ private static boolean isTrackingDataValid (String trackingData){
128131
return trackingData.equals("name") || trackingData.equals("email") || trackingData.equals("message") || trackingData.equals("discount_code");
129132
}
130133

134+
public boolean validateRetry(String callback) {
135+
String string = new JSONObject(callback).getJSONObject("message").getString("text");
136+
if (string.equals("retry")) {
137+
return true;
138+
}
139+
return false;
140+
}
141+
131142
static class EventType {
132143
public static final String CONVERSATION_STARTED = "conversation_started";
133144
public static final String MESSAGE = "message";
@@ -143,6 +154,7 @@ static class InputKeyword {
143154
public static final String VIEW_MORE = "view more";
144155
public static final String NO_DISCOUNT = "no discount";
145156
public static final String VIEW_BRANDS_CATEGORY = "categoryId";
157+
public static final String RETRY = "retry";
146158
}
147159
}
148160

src/main/java/com/sharetreats/chatbot/module/controller/webhook/SendInvalidTokenMessage.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33

44
import com.sharetreats.chatbot.infra.config.RestTemplateConfig;
5+
import com.sharetreats.chatbot.module.controller.dto.retryDtos.RetryButton;
6+
import com.sharetreats.chatbot.module.controller.dto.retryDtos.RetryKeyboard;
7+
import com.sharetreats.chatbot.module.controller.dto.retryDtos.RetryMessage;
8+
import com.sharetreats.chatbot.module.controller.dto.welcomeDtos.WelcomeButton;
9+
import com.sharetreats.chatbot.module.controller.dto.welcomeDtos.WelcomeKeyboard;
10+
import com.sharetreats.chatbot.module.controller.dto.welcomeDtos.WelcomeMessage;
511
import lombok.AllArgsConstructor;
612
import lombok.Getter;
713
import lombok.NoArgsConstructor;
@@ -10,6 +16,9 @@
1016
import org.springframework.beans.factory.annotation.Value;
1117
import org.springframework.http.*;
1218
import org.springframework.stereotype.Component;
19+
import org.springframework.web.client.RestTemplate;
20+
21+
import java.util.Collections;
1322

1423
@Slf4j
1524
@RequiredArgsConstructor
@@ -24,27 +33,21 @@ public class SendInvalidTokenMessage {
2433
static final String VIBER_SEND_MESSAGE_URL = "https://chatapi.viber.com/pa/send_message";
2534

2635
public ResponseEntity<?> execute(String accountId) {
27-
log.info("send invalid message");
28-
InvalidMessage invalidMessage = new InvalidMessage(accountId, "text", "stchatbot3", "Validity time has expired");
36+
RetryMessage message = new RetryMessage(accountId,
37+
1,
38+
"text",
39+
"Your time has expired.\n" +
40+
"If you want to get back into service, press the \"Retry\".",
41+
new RetryKeyboard("keyboard", false, "#FFFFFF",
42+
Collections.singletonList(new RetryButton(6, 1, "#29A7D9", "reply", "retry", "Retry", "center", "middle", "large"))));
43+
2944

3045
HttpHeaders httpHeaders = new HttpHeaders();
3146
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
3247
httpHeaders.set("X-Viber-Auth-Token", token);
48+
HttpEntity<RetryMessage> httpEntity = new HttpEntity<>(message, httpHeaders);
3349

34-
HttpEntity<InvalidMessage> invalidMessageHttpEntity = new HttpEntity<>(invalidMessage, httpHeaders);
35-
restTemplateConfig.restTemplate().exchange(VIBER_SEND_MESSAGE_URL, HttpMethod.POST, invalidMessageHttpEntity, String.class);
36-
sendWelcomeMessage.execute();
37-
return ResponseEntity.ok().build();
50+
return restTemplateConfig.restTemplate().exchange(VIBER_SEND_MESSAGE_URL, HttpMethod.POST, httpEntity, String.class);
3851
}
3952

40-
@Getter
41-
@NoArgsConstructor
42-
@AllArgsConstructor
43-
private class InvalidMessage {
44-
45-
private String receiver;
46-
private String type;
47-
private String name;
48-
private String text;
49-
}
5053
}

0 commit comments

Comments
 (0)