From 8601e45a6507fce98f3453241d46240938b0b1d3 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Portier Date: Wed, 2 Oct 2024 15:14:14 +0200 Subject: [PATCH] refactor (samples): Use webhooks at request level when defined --- .../com/sinch/sample/BaseApplication.java | 20 ++- .../sample/conversation/messages/Send.java | 14 ++- .../java/com/sinch/sample/numbers/Update.java | 9 +- .../com/sinch/sample/sms/batches/Send.java | 4 +- .../com/sinch/sample/sms/batches/Update.java | 23 ++-- .../sample/verification/start/Start.java | 2 +- .../com/sinch/sample/voice/callouts/Call.java | 117 +++++++++--------- .../src/main/resources/config.properties | 4 + 8 files changed, 110 insertions(+), 83 deletions(-) diff --git a/sample-app/src/main/java/com/sinch/sample/BaseApplication.java b/sample-app/src/main/java/com/sinch/sample/BaseApplication.java index 165a72393..7d3b8bc15 100644 --- a/sample-app/src/main/java/com/sinch/sample/BaseApplication.java +++ b/sample-app/src/main/java/com/sinch/sample/BaseApplication.java @@ -5,6 +5,7 @@ import com.sinch.sdk.models.Configuration; import com.sinch.sdk.models.SmsServicePlanCredentials; import java.io.IOException; +import java.util.Optional; import java.util.Properties; import java.util.logging.Logger; @@ -25,8 +26,10 @@ public abstract class BaseApplication { private static final String CONVERSATION_TEMPLATE_ID_KEY = "CONVERSATION_TEMPLATE_ID"; public static final String WEBHOOKS_URL_KEY = "WEBHOOKS_URL"; + public static final String WEBHOOKS_NUMBERS_PATH_KEY = "WEBHOOKS_NUMBERS_PATH"; public static final String WEBHOOKS_VOICE_PATH_KEY = "WEBHOOKS_VOICE_PATH"; public static final String WEBHOOKS_SMS_PATH_KEY = "WEBHOOKS_SMS_PATH"; + public static final String WEBHOOKS_CONVERSATION_PATH_KEY = "WEBHOOKS_CONVERSATION_PATH"; protected static final Logger LOGGER = Utils.initializeLogger(BaseApplication.class.getName()); @@ -48,8 +51,10 @@ public abstract class BaseApplication { protected String smsServicePlanId; protected String smsApiToken; protected String applicationKey; - protected String webhooksVoicePath; - protected String webhooksSmsPath; + protected Optional webhooksConversationPath = Optional.empty(); + protected Optional webhooksNumbersPath = Optional.empty(); + protected Optional webhooksSmsPath = Optional.empty(); + protected Optional webhooksVoicePath = Optional.empty(); Properties properties; @@ -84,9 +89,16 @@ protected BaseApplication() throws IOException { String webhooksUrl = getConfigValue(WEBHOOKS_URL_KEY); if (null != webhooksUrl) { + webhooksConversationPath = + Optional.of( + String.format("%s%s", webhooksUrl, getConfigValue(WEBHOOKS_CONVERSATION_PATH_KEY))); + webhooksNumbersPath = + Optional.of( + String.format("%s%s", webhooksUrl, getConfigValue(WEBHOOKS_NUMBERS_PATH_KEY))); + webhooksSmsPath = + Optional.of(String.format("%s%s", webhooksUrl, getConfigValue(WEBHOOKS_SMS_PATH_KEY))); webhooksVoicePath = - String.format("%s%s", webhooksUrl, getConfigValue(WEBHOOKS_VOICE_PATH_KEY)); - webhooksSmsPath = String.format("%s%s", webhooksUrl, getConfigValue(WEBHOOKS_SMS_PATH_KEY)); + Optional.of(String.format("%s%s", webhooksUrl, getConfigValue(WEBHOOKS_VOICE_PATH_KEY))); } applicationKey = diff --git a/sample-app/src/main/java/com/sinch/sample/conversation/messages/Send.java b/sample-app/src/main/java/com/sinch/sample/conversation/messages/Send.java index 87bc947e5..2b2e5e2e7 100644 --- a/sample-app/src/main/java/com/sinch/sample/conversation/messages/Send.java +++ b/sample-app/src/main/java/com/sinch/sample/conversation/messages/Send.java @@ -49,6 +49,8 @@ public void run() { } SendMessageRequest createRCSSendMessage() { + SendMessageRequest.Builder builder = SendMessageRequest.builder(); + var textMessage = TextMessage.builder() .setText("[Java SDK: Conversation Message] Please select an action") @@ -80,7 +82,7 @@ SendMessageRequest createRCSSendMessage() { .setPostbackData("Location message selected") .build()); - return SendMessageRequest.builder() + builder .setAppId(conversationAppId) .setMessage( AppMessage.builder() @@ -97,10 +99,15 @@ SendMessageRequest createRCSSendMessage() { .build())) .setTtl(25) .build(); + + webhooksConversationPath.ifPresent(builder::setCallbackUrl); + return builder.build(); } SendMessageRequest createSMSSendMessage() { - return SendMessageRequest.builder() + SendMessageRequest.Builder builder = SendMessageRequest.builder(); + + builder .setAppId(conversationAppId) .setMessage( AppMessage.builder() @@ -119,5 +126,8 @@ SendMessageRequest createSMSSendMessage() { .build())) .setTtl(25) .build(); + + webhooksConversationPath.ifPresent(builder::setCallbackUrl); + return builder.build(); } } diff --git a/sample-app/src/main/java/com/sinch/sample/numbers/Update.java b/sample-app/src/main/java/com/sinch/sample/numbers/Update.java index 2e96db4ed..8291d92e1 100644 --- a/sample-app/src/main/java/com/sinch/sample/numbers/Update.java +++ b/sample-app/src/main/java/com/sinch/sample/numbers/Update.java @@ -39,14 +39,15 @@ public void run() { VoiceConfiguration voiceConfiguration = VoiceConfigurationRTC.builder().setAppId(applicationKey).build(); - ActiveNumberUpdateRequest parameters = + ActiveNumberUpdateRequest.Builder builder = ActiveNumberUpdateRequest.builder() .setDisplayName(displayName) // .setSmsConfiguration(smsConfiguration) - .setVoiceConfiguration(voiceConfiguration) - .build(); + .setVoiceConfiguration(voiceConfiguration); + + webhooksNumbersPath.ifPresent(builder::setCallbackUrl); - ActiveNumber value = service.update(virtualPhoneNumber, parameters); + ActiveNumber value = service.update(virtualPhoneNumber, builder.build()); LOGGER.info("Response :" + value); } diff --git a/sample-app/src/main/java/com/sinch/sample/sms/batches/Send.java b/sample-app/src/main/java/com/sinch/sample/sms/batches/Send.java index ceb21ab61..d8f8aa072 100644 --- a/sample-app/src/main/java/com/sinch/sample/sms/batches/Send.java +++ b/sample-app/src/main/java/com/sinch/sample/sms/batches/Send.java @@ -35,9 +35,7 @@ public void run() { .setDeliveryReport(DeliveryReportType.FULL); // Overload default dashboard webhooks URL if defined - if (null != webhooksSmsPath) { - builder.setCallbackUrl(webhooksSmsPath); - } + webhooksSmsPath.ifPresent(builder::setCallbackUrl); BatchText value = client.sms().batches().send(builder.build()); diff --git a/sample-app/src/main/java/com/sinch/sample/sms/batches/Update.java b/sample-app/src/main/java/com/sinch/sample/sms/batches/Update.java index 9f6611613..7e6acc09c 100644 --- a/sample-app/src/main/java/com/sinch/sample/sms/batches/Update.java +++ b/sample-app/src/main/java/com/sinch/sample/sms/batches/Update.java @@ -24,18 +24,17 @@ public static void main(String[] args) { public void run() { LOGGER.info("Updating batch: " + batchId); - BatchText value = - client - .sms() - .batches() - .update( - batchId, - UpdateSmsBatchTextRequest.builder() - .setToRemove(Collections.singletonList("+33745149803")) - .setToAdd(Collections.singletonList("+33745149803")) - .setBody("the body updated") - .setFrom("+33123456789") - .build()); + + UpdateSmsBatchTextRequest.Builder builder = + UpdateSmsBatchTextRequest.builder() + .setToRemove(Collections.singletonList("+33745149803")) + .setToAdd(Collections.singletonList("+33745149803")) + .setBody("the body updated") + .setFrom("+33123456789"); + + webhooksSmsPath.ifPresent(builder::setCallbackUrl); + + BatchText value = client.sms().batches().update(batchId, builder.build()); LOGGER.info("Response: " + value); } diff --git a/sample-app/src/main/java/com/sinch/sample/verification/start/Start.java b/sample-app/src/main/java/com/sinch/sample/verification/start/Start.java index 6c1cb114e..3ece68bd3 100644 --- a/sample-app/src/main/java/com/sinch/sample/verification/start/Start.java +++ b/sample-app/src/main/java/com/sinch/sample/verification/start/Start.java @@ -34,7 +34,7 @@ public void run() { LOGGER.info("Start verification for : " + phoneNumber); NumberIdentity identity = NumberIdentity.valueOf(phoneNumber); - VerificationMethod method = VerificationMethod.FLASH_CALL; + VerificationMethod method = VerificationMethod.SMS; VerificationStartService service = client.verification().v1().verificationStart(); diff --git a/sample-app/src/main/java/com/sinch/sample/voice/callouts/Call.java b/sample-app/src/main/java/com/sinch/sample/voice/callouts/Call.java index 61cb975d8..f98b414aa 100644 --- a/sample-app/src/main/java/com/sinch/sample/voice/callouts/Call.java +++ b/sample-app/src/main/java/com/sinch/sample/voice/callouts/Call.java @@ -41,10 +41,9 @@ public void run() { LOGGER.info("Start call for: " + phoneNumber); - CalloutRequestParameters parameters = - // getTextToSpeechRequest(); - // getCalloutRequest(); - getConferenceRequest(); + CalloutRequestParameters parameters = getTextToSpeechRequest(); + // getCalloutRequest(); + // getConferenceRequest(); var response = client.voice().callouts().call(parameters); @@ -63,59 +62,63 @@ private CalloutRequestParametersTTS getTextToSpeechRequest() { } private CalloutRequestParametersCustom getCalloutRequest() { - return CalloutRequestParametersCustom.builder() - .setCustom("my custom value") - .setIce( - SVAMLControl.builder() - .setAction( - ActionConnectPstn.builder() - .setNumber(E164PhoneNumber.valueOf(phoneNumber)) - .setCli("+123456789") - .build()) - .setInstructions( - Arrays.asList(InstructionSay.builder().setText("Hello from Sinch").build())) - .build()) - .setAce( - SVAMLControl.builder() - .setAction( - ActionRunMenu.builder() - .setLocale("Kimberly") - .setEnableVoice(true) - .setMenus( - Arrays.asList( - Menu.builder() - .setId("main") - .setMainPrompt( - "#tts[Welcome to the main menu. Press 1 to confirm" - + " order or 4 to cancel]") - .setRepeatPrompt("#tts[Incorrect value, please try again]") - .setTimeoutMills(5000) - .setOptions( - Arrays.asList( - MenuOption.builder() - .setDtfm(DualToneMultiFrequency.valueOf("1")) - .setAction( - MenuOptionAction.from( - MenuOptionActionType.MENU, "confirm")) - .build(), - MenuOption.builder() - .setDtfm(DualToneMultiFrequency.valueOf("4")) - .setAction( - MenuOptionAction.from( - MenuOptionActionType.RETURN, "cancel")) - .build())) - .build(), - Menu.builder() - .setId("confirm") - .setMainPrompt( - "#tts[Thank you for confirming your order. Enter your" - + " 4-digit PIN.]") - .setMaxDigits(4) - .build())) - .build()) - .build()) - .setPie(ControlUrl.from(webhooksVoicePath)) - .build(); + + CalloutRequestParametersCustom.Builder builder = + CalloutRequestParametersCustom.builder() + .setCustom("my custom value") + .setIce( + SVAMLControl.builder() + .setAction( + ActionConnectPstn.builder() + .setNumber(E164PhoneNumber.valueOf(phoneNumber)) + .setCli("+123456789") + .build()) + .setInstructions( + Arrays.asList(InstructionSay.builder().setText("Hello from Sinch").build())) + .build()) + .setAce( + SVAMLControl.builder() + .setAction( + ActionRunMenu.builder() + .setLocale("Kimberly") + .setEnableVoice(true) + .setMenus( + Arrays.asList( + Menu.builder() + .setId("main") + .setMainPrompt( + "#tts[Welcome to the main menu. Press 1 to confirm" + + " order or 4 to cancel]") + .setRepeatPrompt("#tts[Incorrect value, please try again]") + .setTimeoutMills(5000) + .setOptions( + Arrays.asList( + MenuOption.builder() + .setDtfm(DualToneMultiFrequency.valueOf("1")) + .setAction( + MenuOptionAction.from( + MenuOptionActionType.MENU, "confirm")) + .build(), + MenuOption.builder() + .setDtfm(DualToneMultiFrequency.valueOf("4")) + .setAction( + MenuOptionAction.from( + MenuOptionActionType.RETURN, "cancel")) + .build())) + .build(), + Menu.builder() + .setId("confirm") + .setMainPrompt( + "#tts[Thank you for confirming your order. Enter your" + + " 4-digit PIN.]") + .setMaxDigits(4) + .build())) + .build()) + .build()); + + webhooksVoicePath.ifPresent(c -> builder.setPie(ControlUrl.from(c))); + + return builder.build(); } private CalloutRequestParametersConference getConferenceRequest() { diff --git a/sample-app/src/main/resources/config.properties b/sample-app/src/main/resources/config.properties index 867b62606..120b33263 100644 --- a/sample-app/src/main/resources/config.properties +++ b/sample-app/src/main/resources/config.properties @@ -1,4 +1,8 @@ CONFERENCE_ID=My Conference Id + +WEBHOOKS_CONVERSATION_PATH=/ConversationEvent +WEBHOOKS_NUMBERS_PATH=/NumbersEvent +WEBHOOKS_SMS_PATH=/SmsEvent WEBHOOKS_VOICE_PATH=/VoiceEvent SINCH_PROJECT_ID=