diff --git a/.github/workflows/release-dev.yml b/.github/workflows/release-dev.yml index 1aacfa8..65600e6 100644 --- a/.github/workflows/release-dev.yml +++ b/.github/workflows/release-dev.yml @@ -160,14 +160,12 @@ jobs: OIDC_CLIENT_ID=${{ secrets.OIDC_CLIENT_ID }} OIDC_AUTH_URL=${{ secrets.OIDC_AUTH_URL }} LOGIN_BASE_URI=${{ secrets.LOGIN_BASE_URI }} - META_GRAPH_API_BASE_URI=${{ secrets.META_GRAPH_API_BASE_URI }} - META_FB_PAGE_ID=${{ secrets.META_FB_PAGE_ID }} META_FB_VRFY_TOKEN=${{ secrets.META_FB_VRFY_TOKEN }} + META_FB_APP_SECRET=${{ secrets.META_FB_APP_SECRET }} FPI_APP_TO_APP_USERN=${{ secrets.FPI_APP_TO_APP_USERN }} USER_BASE_URI=${{ secrets.USER_BASE_URI }} LOAD_API_BASE_URI=${{ secrets.LOAD_API_BASE_URI }} - META_FB_APP_SECRET=${{ secrets.META_FB_APP_SECRET }} - META_FB_PAGE_ACCESS_TOKEN=${{ secrets.META_FB_PAGE_ACCESS_TOKEN }} + MSGR_API_BASE_URI=${{ secrets.MSGR_API_BASE_URI }} secrets: | MONGO_CONN_STRING=vbl_mongo_connection_string:latest OIDC_SECRET=oidc_secret_keycloak:latest diff --git a/.github/workflows/release-main.yml b/.github/workflows/release-main.yml index 966c856..986913f 100644 --- a/.github/workflows/release-main.yml +++ b/.github/workflows/release-main.yml @@ -202,17 +202,15 @@ jobs: OIDC_CLIENT_ID=${{ secrets.OIDC_CLIENT_ID }} OIDC_AUTH_URL=${{ secrets.OIDC_AUTH_URL }} LOGIN_BASE_URI=${{ secrets.LOGIN_BASE_URI }} - META_GRAPH_API_BASE_URI=${{ secrets.META_GRAPH_API_BASE_URI }} - META_FB_PAGE_ID=${{ secrets.META_FB_PAGE_ID }} META_FB_VRFY_TOKEN=${{ secrets.META_FB_VRFY_TOKEN }} FPI_APP_TO_APP_USERN=${{ secrets.FPI_APP_TO_APP_USERN }} USER_BASE_URI=${{ secrets.USER_BASE_URI }} LOAD_API_BASE_URI=${{ secrets.LOAD_API_BASE_URI }} + MSGR_API_BASE_URI=${{ secrets.MSGR_API_BASE_URI }} secrets: | MONGO_CONN_STRING=vbl_mongo_connection_string:latest OIDC_SECRET=oidc_secret_keycloak:latest META_FB_APP_SECRET=meta_fb_app_secret:latest - META_FB_PAGE_ACCESS_TOKEN=meta_fb_page_access_token:latest FPI_APP_TO_APP_PASSW=fpi_bot_api_creds_secret_key:latest labels: | env=${{ env.SERVICE_ENV }} diff --git a/fpi-bot-api-core/pom.xml b/fpi-bot-api-core/pom.xml index 7e4a1f3..5a4d07a 100644 --- a/fpi-bot-api-core/pom.xml +++ b/fpi-bot-api-core/pom.xml @@ -82,6 +82,11 @@ fpi-load-api-lib + + com.abavilla + fpi-msgr-api-lib + + diff --git a/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/config/MetaApiKeyConfig.java b/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/config/MetaApiKeyConfig.java index 23c181c..086b0f5 100644 --- a/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/config/MetaApiKeyConfig.java +++ b/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/config/MetaApiKeyConfig.java @@ -33,10 +33,4 @@ public class MetaApiKeyConfig { @ConfigProperty(name = "com.meta.facebook.verify-token") String authorizedToken; - @ConfigProperty(name = "com.meta.facebook.page-access-token") - String pageAccessToken; - - @ConfigProperty(name = "com.meta.facebook.page-id") - String pageId; - } diff --git a/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/processor/MetaMsgEvtPcsr.java b/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/processor/MetaMsgEvtPcsr.java index 16c22ee..e95c735 100644 --- a/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/processor/MetaMsgEvtPcsr.java +++ b/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/processor/MetaMsgEvtPcsr.java @@ -24,7 +24,6 @@ import com.abavilla.fpi.bot.entity.meta.MetaMsgEvt; import com.abavilla.fpi.bot.mapper.meta.MetaMsgEvtMapper; import com.abavilla.fpi.bot.repo.MetaMsgEvtRepo; -import com.abavilla.fpi.bot.service.MetaMsgrApiSvc; import com.abavilla.fpi.fw.dto.impl.RespDto; import com.abavilla.fpi.fw.exceptions.ApiSvcEx; import com.abavilla.fpi.fw.util.DateUtil; @@ -36,6 +35,8 @@ import com.abavilla.fpi.login.ext.rest.TrustedLoginApi; import com.abavilla.fpi.meta.ext.codec.MetaMsgEvtCodec; import com.abavilla.fpi.meta.ext.dto.msgr.ext.MetaMsgEvtDto; +import com.abavilla.fpi.msgr.ext.dto.MsgrMsgReqDto; +import com.abavilla.fpi.msgr.ext.rest.MsgrReqApi; import com.mongodb.ErrorCategory; import com.mongodb.MongoWriteException; import io.netty.handler.codec.http.HttpResponseStatus; @@ -43,19 +44,23 @@ import io.quarkus.vertx.ConsumeEvent; import io.smallrye.mutiny.Uni; import org.apache.commons.lang3.StringUtils; +import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.rest.client.inject.RestClient; @ApplicationScoped public class MetaMsgEvtPcsr { + @ConfigProperty(name = "fpi.app-to-app.auth.username") + String fpiSystemId; + @RestClient TrustedLoginApi loginApi; @RestClient LoadQueryApi loadApi; - @Inject - MetaMsgrApiSvc metaMsgrSvc; + @RestClient + MsgrReqApi msgrApi; @Inject MetaMsgEvtMapper metaMsgEvtMapper; @@ -67,7 +72,7 @@ public class MetaMsgEvtPcsr { public Uni process(MetaMsgEvtDto evt) { Log.info("Received event: " + evt); if (StringUtils.isNotBlank(evt.getContent())) { - return metaMsgrSvc.sendTypingIndicator(evt.getSender(), true).chain(() -> { + return msgrApi.toggleTyping(evt.getSender(), true).chain(() -> { Log.info("Processing event: " + evt.getMetaMsgId()); MetaMsgEvt metaMsgEvt = metaMsgEvtMapper.mapToEntity(evt); metaMsgEvt.setDateCreated(DateUtil.now()); @@ -92,7 +97,7 @@ public Uni process(MetaMsgEvtDto evt) { return null; }); }) - .chain(() -> metaMsgrSvc.sendTypingIndicator(evt.getSender(), false)).replaceWithVoid() + .chain(() -> msgrApi.toggleTyping(evt.getSender(), false)).replaceWithVoid() .onFailure().invoke(this::handleMsgEx); } return Uni.createFrom().voidItem(); @@ -132,13 +137,16 @@ private Uni handleApiEx(MetaMsgEvtDto evt, Throwable ex) { handleAction = sendMsgrMsg(evt, "Error occurred, please try again"); } - return handleAction.chain(() -> metaMsgrSvc.sendTypingIndicator( + return handleAction.chain(() -> msgrApi.toggleTyping( evt.getSender(), false).replaceWithVoid()); } private Uni sendMsgrMsg(MetaMsgEvtDto evt, String msg) { Log.info("Sending msgr msg: " + msg + " event: " + evt.getMetaMsgId()); - return metaMsgrSvc.sendMsg(msg, evt.getSender()).replaceWithVoid(); + var msgReq = new MsgrMsgReqDto(); + msgReq.setRecipient(evt.getSender()); + msgReq.setContent(msg); + return msgrApi.sendMsg(msgReq, fpiSystemId).replaceWithVoid(); } } diff --git a/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/rest/MetaGraphApi.java b/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/rest/MetaGraphApi.java deleted file mode 100644 index 8f38e64..0000000 --- a/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/rest/MetaGraphApi.java +++ /dev/null @@ -1,70 +0,0 @@ -/****************************************************************************** - * FPI Application - Abavilla * - * Copyright (C) 2022 Vince Jerald Villamora * - * * - * This program is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, either version 3 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program. If not, see . * - ******************************************************************************/ - -package com.abavilla.fpi.bot.rest; - -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; - -import com.abavilla.fpi.fw.exceptions.handler.ApiRepoExHandler; -import com.abavilla.fpi.fw.rest.IApi; -import com.abavilla.fpi.meta.ext.dto.ProfileReqReply; -import com.abavilla.fpi.meta.ext.dto.msgr.MsgrReqReply; -import io.smallrye.mutiny.Uni; -import org.eclipse.microprofile.rest.client.annotation.RegisterProvider; -import org.eclipse.microprofile.rest.client.inject.RegisterRestClient; -import org.jboss.resteasy.reactive.RestResponse; - -@RegisterRestClient(configKey = "meta-graph-api") -@Produces(MediaType.APPLICATION_JSON) -@RegisterProvider(value = ApiRepoExHandler.class) -public interface MetaGraphApi extends IApi { - - @POST - @Path("v15.0/{pageId}/messages") - Uni> sendMsgrMsg( - @PathParam("pageId") String pageId, - @QueryParam("recipient") String recipient, - @QueryParam("messaging_type") String type, - @QueryParam("message") String messageNode, - @QueryParam("access_token") String token - ); - - @POST - @Path("v15.0/{pageId}/messages") - Uni> sendTypingIndicator( - @PathParam("pageId") String pageId, - @QueryParam("recipient") String recipient, - @QueryParam("sender_action") String senderAction, - @QueryParam("messaging_type") String type, - @QueryParam("access_token") String token - ); - - @GET - @Path("{profileId}") - Uni> getProfile( - @PathParam("profileId") String profileId, - @QueryParam("fields") String fields, - @QueryParam("access_token") String token - ); -} diff --git a/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/service/MetaMsgrApiSvc.java b/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/service/MetaMsgrApiSvc.java deleted file mode 100644 index 7d5508e..0000000 --- a/fpi-bot-api-core/src/main/java/com/abavilla/fpi/bot/service/MetaMsgrApiSvc.java +++ /dev/null @@ -1,76 +0,0 @@ -/****************************************************************************** - * FPI Application - Abavilla * - * Copyright (C) 2022 Vince Jerald Villamora * - * * - * This program is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, either version 3 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program. If not, see . * - ******************************************************************************/ - -package com.abavilla.fpi.bot.service; - -import javax.enterprise.context.ApplicationScoped; -import javax.inject.Inject; - -import com.abavilla.fpi.bot.config.MetaApiKeyConfig; -import com.abavilla.fpi.bot.entity.enums.SenderAction; -import com.abavilla.fpi.bot.rest.MetaGraphApi; -import com.abavilla.fpi.fw.exceptions.FPISvcEx; -import com.abavilla.fpi.fw.rest.AbsApiSvc; -import com.abavilla.fpi.meta.ext.dto.msgr.MsgDtlDto; -import com.abavilla.fpi.meta.ext.dto.msgr.MsgrReqReply; -import com.abavilla.fpi.meta.ext.dto.msgr.ProfileDto; -import io.smallrye.mutiny.Uni; -import org.jboss.resteasy.reactive.RestResponse; - -@ApplicationScoped -public class MetaMsgrApiSvc extends AbsApiSvc { - - @Inject - MetaApiKeyConfig metaApiKeyConfig; - - public Uni sendMsg(String msg, String recipientId) { - var recipient = new ProfileDto(); - recipient.setId(recipientId); - var msgDtl = new MsgDtlDto(); - msgDtl.setText(msg); - - return client.sendMsgrMsg( - metaApiKeyConfig.getPageId(), - recipient.toJsonStr(), - "RESPONSE", - msgDtl.toJsonStr(), - metaApiKeyConfig.getPageAccessToken() - ).map(this::mapResponse); - } - - public Uni sendTypingIndicator(String recipientId, boolean isTyping) { - var recipient = new ProfileDto(); - recipient.setId(recipientId); - return client.sendTypingIndicator( - metaApiKeyConfig.getPageId(), - recipient.toJsonStr(), - isTyping ? SenderAction.TYPING_ON.getValue() : SenderAction.TYPING_OFF.getValue(), - "RESPONSE", - metaApiKeyConfig.getPageAccessToken() - ).map(this::mapResponse); - } - - private MsgrReqReply mapResponse(RestResponse resp) { - if (resp.getStatus() == RestResponse.StatusCode.OK) { - return resp.getEntity(); - } else { - throw new FPISvcEx("Unable to send messenger reply"); - } - } - -} diff --git a/fpi-bot-api-core/src/main/resources/application.yml b/fpi-bot-api-core/src/main/resources/application.yml index ff48ba7..c903f55 100644 --- a/fpi-bot-api-core/src/main/resources/application.yml +++ b/fpi-bot-api-core/src/main/resources/application.yml @@ -22,8 +22,8 @@ quarkus: user-api: url: ${USER_BASE_URI} scope: javax.inject.Singleton - meta-graph-api: - url: ${META_GRAPH_API_BASE_URI} + msgr-api: + url: ${MSGR_API_BASE_URI} scope: javax.inject.Singleton oidc: auth-server-url: ${OIDC_AUTH_URL:https://localhost:8543/realms/quarkus} @@ -45,8 +45,6 @@ com: facebook: verify-token: ${META_FB_VRFY_TOKEN:FPIMETAVRFYQsARvSdfggYQS} app-secret: ${META_FB_APP_SECRET:ABCJSOWE1234} - page-id: ${META_FB_PAGE_ID:ABCJSOWE1234} - page-access-token: ${META_FB_PAGE_ACCESS_TOKEN:ABCJSOWE1234} fpi: app-to-app: diff --git a/pom.xml b/pom.xml index fe1538f..7c3cd4f 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,7 @@ com.abavilla fpi-framework-pom - 1.1.33 + 1.1.35 4.0.0 @@ -93,6 +93,12 @@ 1.0.45 + + com.abavilla + fpi-msgr-api-lib + 1.1.2 + +