Skip to content

Commit

Permalink
Merge pull request #16 from sinch/feat-add-region-to-sms-gs
Browse files Browse the repository at this point in the history
feat: adding better logging and setting region
  • Loading branch information
JPPortier authored Aug 5, 2024
2 parents 1f7d8ff + 0dd1400 commit dd1eba5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.sinch.sdk.SinchClient;
import com.sinch.sdk.core.utils.StringUtil;
import com.sinch.sdk.models.Configuration;
import com.sinch.sdk.models.SMSRegion;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

Expand All @@ -18,6 +19,9 @@ public class Config {
@Value("${credentials.key-secret}")
String keySecret;

@Value("${sms.region}")
String region;

@Bean
public SinchClient sinchClient() {

Expand All @@ -26,13 +30,15 @@ public SinchClient sinchClient() {
if (!StringUtil.isEmpty(projectId)) {
builder.setProjectId(projectId);
}

if (!StringUtil.isEmpty(keyId)) {
builder.setKeyId(keyId);
}
if (!StringUtil.isEmpty(keySecret)) {
builder.setKeySecret(keySecret);
}
if (!StringUtil.isEmpty(region)) {
builder.setSmsRegion(SMSRegion.from(region));
}

return new SinchClient(builder.build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.sinch.sdk.domains.sms.BatchesService;
import com.sinch.sdk.domains.sms.models.InboundText;
import com.sinch.sdk.domains.sms.models.requests.SendSmsBatchTextRequest;
import com.sinch.sdk.domains.sms.models.webhooks.WebhooksEvent;
import java.util.Collections;
import java.util.logging.Logger;
import org.springframework.stereotype.Component;
Expand All @@ -21,17 +20,18 @@ public ServerBusinessLogic(SinchClient sinchClient) {
private static final Logger LOGGER = Logger.getLogger(ServerBusinessLogic.class.getName());

public void processInboundEvent(InboundText event) {
trace(event);

batches.send(
LOGGER.info("Handle event: " + event);

SendSmsBatchTextRequest smsRequest =
SendSmsBatchTextRequest.builder()
.setTo(Collections.singletonList(event.getFrom()))
.setBody("You sent: " + event.getBody())
.setFrom(event.getTo())
.build());
}
.build();

private void trace(WebhooksEvent event) {
LOGGER.info("Handle event: " + event);
LOGGER.info("Replying with: " + smsRequest);

batches.send(smsRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ credentials:
project-id:
key-id:
key-secret:

sms:
# Sets the region for SMS
# valid values are US and EU
region:
7 changes: 7 additions & 0 deletions templates/server/src/main/java/com/mycompany/app/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.sinch.sdk.SinchClient;
import com.sinch.sdk.core.utils.StringUtil;
import com.sinch.sdk.models.Configuration;
import com.sinch.sdk.models.SMSRegion;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

Expand All @@ -24,6 +25,9 @@ public class Config {
@Value("${credentials.application-api-secret}")
String applicationSecret;

@Value("${sms.region}")
String region;

@Bean
public SinchClient sinchClient() {

Expand All @@ -47,6 +51,9 @@ public SinchClient sinchClient() {
if (!StringUtil.isEmpty(applicationSecret)) {
builder.setApplicationSecret(applicationSecret);
}
if (!StringUtil.isEmpty(region)) {
builder.setSmsRegion(SMSRegion.from(region));
}

return new SinchClient(builder.build());
}
Expand Down
5 changes: 5 additions & 0 deletions templates/server/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ credentials:
application-api-key:
application-api-secret:

sms:
# Sets the region for SMS
# valid values are US and EU
region:

# Secret value set for Numbers webhooks calls validation
numbers.webhooks.secret:

0 comments on commit dd1eba5

Please sign in to comment.