From a16abe55190bdb9fa84f86eb5e7fbbcda1483823 Mon Sep 17 00:00:00 2001 From: Surabhi Date: Fri, 26 Nov 2021 13:53:37 +0530 Subject: [PATCH 1/3] caffeine cache changes --- .../adapter/app/config/AppConfiguration1.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/uci/adapter/app/config/AppConfiguration1.java b/src/main/java/com/uci/adapter/app/config/AppConfiguration1.java index 53ad805..442937c 100644 --- a/src/main/java/com/uci/adapter/app/config/AppConfiguration1.java +++ b/src/main/java/com/uci/adapter/app/config/AppConfiguration1.java @@ -1,9 +1,14 @@ package com.uci.adapter.app.config; import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import com.uci.utils.BotService; import com.uci.utils.CampaignService; import io.fusionauth.client.FusionAuthClient; + +import java.time.Duration; + import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; @@ -11,10 +16,13 @@ import org.apache.http.client.HttpClient; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.HttpClients; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.cache.CacheManager; +import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; @@ -67,6 +75,9 @@ public RestTemplate getJSONRestTemplate() { @Value("${fusionauth.key}") public String FUSIONAUTH_KEY; + + @Autowired + public Cache cache; @Bean public FusionAuthClient getFAClient() { @@ -75,10 +86,10 @@ public FusionAuthClient getFAClient() { @Bean public BotService getBotService() { - - WebClient webClient = WebClient.builder() + WebClient webClient = WebClient.builder() .baseUrl(CAMPAIGN_URL) .build(); - return new BotService(webClient, getFAClient()); + + return new BotService(webClient, getFAClient(), cache); } } From ed2eb6fe1f7915f920e2ddc8f9244fa4efbe33d4 Mon Sep 17 00:00:00 2001 From: Surabhi Date: Mon, 29 Nov 2021 18:09:43 +0530 Subject: [PATCH 2/3] sunbird outbound message changes --- .../gs/whatsapp/GSWhatsappService.java | 52 ++++++++++++++++++ .../sunbird/web/SunbirdWebPortalAdapter.java | 55 +++++++++++++++++-- .../sunbird/web/outbound/OutboundMessage.java | 2 + 3 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/uci/adapter/gs/whatsapp/GSWhatsappService.java diff --git a/src/main/java/com/uci/adapter/gs/whatsapp/GSWhatsappService.java b/src/main/java/com/uci/adapter/gs/whatsapp/GSWhatsappService.java new file mode 100644 index 0000000..056a9bb --- /dev/null +++ b/src/main/java/com/uci/adapter/gs/whatsapp/GSWhatsappService.java @@ -0,0 +1,52 @@ +package com.uci.adapter.gs.whatsapp; + +import java.net.URI; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Function; + +import org.springframework.util.MultiValueMap; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.UriComponentsBuilder; + +import com.uci.adapter.netcore.whatsapp.outbound.OutboundMessage; +import com.uci.adapter.netcore.whatsapp.outbound.SendMessageResponse; + +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import reactor.core.publisher.Mono; + +public class GSWhatsappService { + + private final static String GUPSHUP_OUTBOUND = "https://media.smsgupshup.com/GatewayAPI/rest"; + + private final WebClient webClient; + + public GSWhatsappService() { + webClient = WebClient.builder() + .build(); + } + + public Mono sendOutboundMessage(URI url) { + return webClient.get() + .uri(url) + .retrieve() + .bodyToMono(GSWhatsappOutBoundResponse.class) + .map(new Function() { + @Override + public GSWhatsappOutBoundResponse apply(GSWhatsappOutBoundResponse sendMessageResponse) { + if (sendMessageResponse != null) { + System.out.println("RESPONSE " + sendMessageResponse.getResponse()); + return sendMessageResponse; + } else { + return null; + } + } + }).doOnError(new Consumer() { + @Override + public void accept(Throwable throwable) { + System.out.println("ERROR IS " + throwable.getLocalizedMessage()); + } + }); + } +} diff --git a/src/main/java/com/uci/adapter/sunbird/web/SunbirdWebPortalAdapter.java b/src/main/java/com/uci/adapter/sunbird/web/SunbirdWebPortalAdapter.java index 06c5200..1061552 100644 --- a/src/main/java/com/uci/adapter/sunbird/web/SunbirdWebPortalAdapter.java +++ b/src/main/java/com/uci/adapter/sunbird/web/SunbirdWebPortalAdapter.java @@ -23,6 +23,7 @@ import java.sql.Timestamp; import java.time.LocalDateTime; import java.util.ArrayList; +import java.util.Arrays; import java.util.function.Function; @Slf4j @@ -50,6 +51,11 @@ public Mono convertMessageToXMsg(Object message) throws JAXBException, XMessage.MessageType messageType= XMessage.MessageType.TEXT; //Todo: How to get Button choices from normal text from.setUserID(webMessage.getFrom()); + + /* To use later in outbound reply message's message id & to */ + messageIdentifier.setChannelMessageId(webMessage.getMessageId()); + messageIdentifier.setReplyId(webMessage.getTo()); + XMessage x = XMessage.builder() .to(to) .from(from) @@ -68,6 +74,7 @@ public Mono convertMessageToXMsg(Object message) throws JAXBException, public Mono processOutBoundMessageF(XMessage xMsg) throws Exception { log.info("Sending message to transport socket :: " + xMsg.toXML()); OutboundMessage outboundMessage = getOutboundMessage(xMsg); + log.info("Sending final xmessage to transport socket :: " + xMsg.toXML()); // String url = PropertiesCache.getInstance().getProperty("SUNBIRD_OUTBOUND"); String url = "http://transport-socket.ngrok.samagra.io/botMsg/adapterOutbound"; return SunbirdWebService.getInstance(). @@ -113,10 +120,50 @@ public XMessage callOutBoundAPI(XMessage xMsg) throws Exception{ // return sc; // } - private OutboundMessage getOutboundMessage(XMessage xMsg) { - SunbirdMessage sunbirdMessage = SunbirdMessage.builder().title( - xMsg.getPayload().getText() + renderMessageChoices(xMsg.getPayload().getButtonChoices())).choices(xMsg.getPayload().getButtonChoices()).build(); - return OutboundMessage.builder().message(sunbirdMessage).build(); + private OutboundMessage getOutboundMessage(XMessage xMsg) throws JAXBException { + SunbirdMessage sunbirdMessage = SunbirdMessage.builder() + .title(getTextMessage(xMsg)) + .choices(this.getButtonChoices(xMsg)) + .build(); + return OutboundMessage.builder() + .message(sunbirdMessage) + .to(xMsg.getMessageId().getReplyId()) + .messageId(xMsg.getMessageId().getChannelMessageId()) + .build(); + } + + /** + * Get Simplified Text Message + * @param xMsg + * @return String + */ + private String getTextMessage(XMessage xMsg) { + XMessagePayload payload = xMsg.getPayload(); + String text = payload.getText().replace("__", ""); + text = text.replace("\n\n", ""); + payload.setText(text); + return text; + } + + /** + * Get Button Choices with calculated keys + * @param xMsg + * @return ArrayList of ButtonChoices + */ + private ArrayList getButtonChoices(XMessage xMsg) { + ArrayList choices = xMsg.getPayload().getButtonChoices(); + if(choices != null) { + choices.forEach(c -> { + String[] a = c.getText().split(" "); + if(a[0] != null && !a[0].isEmpty()) { + String key = a[0].toString(); + + c.setKey(key); + c.setText(c.getText().replaceFirst(key, "").trim()); + } + }); + } + return choices; } private String renderMessageChoices(ArrayList buttonChoices) { diff --git a/src/main/java/com/uci/adapter/sunbird/web/outbound/OutboundMessage.java b/src/main/java/com/uci/adapter/sunbird/web/outbound/OutboundMessage.java index 6d03e0b..fd2b75f 100644 --- a/src/main/java/com/uci/adapter/sunbird/web/outbound/OutboundMessage.java +++ b/src/main/java/com/uci/adapter/sunbird/web/outbound/OutboundMessage.java @@ -9,4 +9,6 @@ @Builder public class OutboundMessage { private SunbirdMessage message; + private String to; + private String messageId; } \ No newline at end of file From 518030d2606c5994f2c61fd23ec13acae1c2984d Mon Sep 17 00:00:00 2001 From: Surabhi Date: Tue, 30 Nov 2021 10:46:51 +0530 Subject: [PATCH 3/3] deleted unused file --- .../gs/whatsapp/GSWhatsappService.java | 52 ------------------- 1 file changed, 52 deletions(-) delete mode 100644 src/main/java/com/uci/adapter/gs/whatsapp/GSWhatsappService.java diff --git a/src/main/java/com/uci/adapter/gs/whatsapp/GSWhatsappService.java b/src/main/java/com/uci/adapter/gs/whatsapp/GSWhatsappService.java deleted file mode 100644 index 056a9bb..0000000 --- a/src/main/java/com/uci/adapter/gs/whatsapp/GSWhatsappService.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.uci.adapter.gs.whatsapp; - -import java.net.URI; -import java.util.Map; -import java.util.function.Consumer; -import java.util.function.Function; - -import org.springframework.util.MultiValueMap; -import org.springframework.web.reactive.function.client.WebClient; -import org.springframework.web.util.UriComponentsBuilder; - -import com.uci.adapter.netcore.whatsapp.outbound.OutboundMessage; -import com.uci.adapter.netcore.whatsapp.outbound.SendMessageResponse; - -import okhttp3.MediaType; -import okhttp3.OkHttpClient; -import reactor.core.publisher.Mono; - -public class GSWhatsappService { - - private final static String GUPSHUP_OUTBOUND = "https://media.smsgupshup.com/GatewayAPI/rest"; - - private final WebClient webClient; - - public GSWhatsappService() { - webClient = WebClient.builder() - .build(); - } - - public Mono sendOutboundMessage(URI url) { - return webClient.get() - .uri(url) - .retrieve() - .bodyToMono(GSWhatsappOutBoundResponse.class) - .map(new Function() { - @Override - public GSWhatsappOutBoundResponse apply(GSWhatsappOutBoundResponse sendMessageResponse) { - if (sendMessageResponse != null) { - System.out.println("RESPONSE " + sendMessageResponse.getResponse()); - return sendMessageResponse; - } else { - return null; - } - } - }).doOnError(new Consumer() { - @Override - public void accept(Throwable throwable) { - System.out.println("ERROR IS " + throwable.getLocalizedMessage()); - } - }); - } -}