Skip to content

Commit

Permalink
update: 컨트롤러에 토큰 검증 기능 추가 (sharetreats-team#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlqkrus012345 committed Jul 6, 2023
1 parent c510cc0 commit 82bb854
Showing 1 changed file with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sharetreats.chatbot.module.controller;

import com.sharetreats.chatbot.infra.config.TokenConfig;
import com.sharetreats.chatbot.module.controller.webhook.*;
import lombok.RequiredArgsConstructor;
import org.json.JSONObject;
Expand Down Expand Up @@ -30,7 +31,8 @@ public class WebhookController {
private final SendPurchaseInfo sendPurchaseInfo;
private final SendProductDetail sendproductDetail;
private final ManageSubscription manageSubscription;

private final TokenConfig tokenConfig;
private final SendInvalidTokenMessage sendInvalidTokenMessage;

/**
* Webhook CallBack Data 를 받는 `MAIN API`
Expand All @@ -42,10 +44,17 @@ public class WebhookController {
public ResponseEntity<?> webhook(@RequestBody String callback) {
String event = getEventValueToCallback(callback);

if (event.equals(CONVERSATION_STARTED))
if (event.equals(CONVERSATION_STARTED)) {
String accountId = getUserId(callback);
tokenConfig.generateToken(accountId);
return sendWelcomeMessage.execute();
if (event.equals(MESSAGE))
return sendResponseByTextInMessage(callback);
}
if (event.equals(MESSAGE)) {
String accountId = getSenderId(callback);
boolean isValidToken = tokenConfig.validateToken(accountId);
if (isValidToken) sendResponseByTextInMessage(callback);
else return sendInvalidTokenMessage.execute(accountId);
}
if (event.equals(UNSUBSCRIBED))
manageSubscription.unsubscribe(callback);
if (event.equals(SUBSCRIBED))
Expand Down Expand Up @@ -101,6 +110,16 @@ private static String getTrackingDataToMessage (String callback){
return trackingData;
}

private static String getUserId (String callback) {
JSONObject accountObject = new JSONObject(callback).getJSONObject("user");
return accountObject.getString("id");
}

private static String getSenderId (String callback) {
JSONObject accountObject = new JSONObject(callback).getJSONObject("sender");
return accountObject.getString("id");
}

private static boolean isTrackingDataValid (String trackingData){
return trackingData.equals("name") || trackingData.equals("email") || trackingData.equals("message") || trackingData.equals("discount_code");
}
Expand Down

0 comments on commit 82bb854

Please sign in to comment.