Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor (samples): Use webhooks at request level when defined #162

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions sample-app/src/main/java/com/sinch/sample/BaseApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());

Expand All @@ -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<String> webhooksConversationPath = Optional.empty();
protected Optional<String> webhooksNumbersPath = Optional.empty();
protected Optional<String> webhooksSmsPath = Optional.empty();
protected Optional<String> webhooksVoicePath = Optional.empty();

Properties properties;

Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public void run() {
}

SendMessageRequest<?> createRCSSendMessage() {
SendMessageRequest.Builder<ChoiceMessage> builder = SendMessageRequest.<ChoiceMessage>builder();

var textMessage =
TextMessage.builder()
.setText("[Java SDK: Conversation Message] Please select an action")
Expand Down Expand Up @@ -80,7 +82,7 @@ SendMessageRequest<?> createRCSSendMessage() {
.setPostbackData("Location message selected")
.build());

return SendMessageRequest.<ChoiceMessage>builder()
builder
.setAppId(conversationAppId)
.setMessage(
AppMessage.<ChoiceMessage>builder()
Expand All @@ -97,10 +99,15 @@ SendMessageRequest<?> createRCSSendMessage() {
.build()))
.setTtl(25)
.build();

webhooksConversationPath.ifPresent(builder::setCallbackUrl);
return builder.build();
}

SendMessageRequest<?> createSMSSendMessage() {
return SendMessageRequest.<TextMessage>builder()
SendMessageRequest.Builder<TextMessage> builder = SendMessageRequest.<TextMessage>builder();

builder
.setAppId(conversationAppId)
.setMessage(
AppMessage.<TextMessage>builder()
Expand All @@ -119,5 +126,8 @@ SendMessageRequest<?> createSMSSendMessage() {
.build()))
.setTtl(25)
.build();

webhooksConversationPath.ifPresent(builder::setCallbackUrl);
asein-sinch marked this conversation as resolved.
Show resolved Hide resolved
return builder.build();
}
}
9 changes: 5 additions & 4 deletions sample-app/src/main/java/com/sinch/sample/numbers/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
23 changes: 11 additions & 12 deletions sample-app/src/main/java/com/sinch/sample/sms/batches/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 vs 📞


VerificationStartService service = client.verification().v1().verificationStart();

Expand Down
117 changes: 60 additions & 57 deletions sample-app/src/main/java/com/sinch/sample/voice/callouts/Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions sample-app/src/main/resources/config.properties
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
Loading