Skip to content

Commit

Permalink
feat (SMS/GettingStarted): 'Response to incoming message' based onto …
Browse files Browse the repository at this point in the history
…v1 package usage
  • Loading branch information
JPPortier committed Jan 23, 2025
1 parent c0e9298 commit 0a90c93
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<name>Sinch Java SDK Server Application</name>

<properties>
<sinch.sdk.java.version>[1.0.0,)</sinch.sdk.java.version>
<sinch.sdk.java.version>[1.5.0,]</sinch.sdk.java.version>
<java.version>21</java.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package com.mycompany.app.sms;

import com.sinch.sdk.SinchClient;
import com.sinch.sdk.domains.sms.WebHooksService;
import com.sinch.sdk.domains.sms.models.InboundText;
import com.sinch.sdk.domains.sms.models.webhooks.WebhooksEvent;
import com.sinch.sdk.domains.sms.api.v1.WebHooksService;
import com.sinch.sdk.domains.sms.models.v1.inbounds.TextMessage;
import com.sinch.sdk.domains.sms.models.v1.webhooks.SmsEvent;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

@RestController("SMS")
public class Controller {

private final SinchClient sinchClient;
private final ServerBusinessLogic webhooksBusinessLogic;

@Value("${sms.webhooks.secret}")
private String webhooksSecret;

@Autowired
public Controller(SinchClient sinchClient, ServerBusinessLogic webhooksBusinessLogic) {
this.sinchClient = sinchClient;
Expand All @@ -27,16 +35,29 @@ public Controller(SinchClient sinchClient, ServerBusinessLogic webhooksBusinessL
value = "/SmsEvent",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Void> smsDeliveryEvent(@RequestBody String body) {
public ResponseEntity<Void> smsDeliveryEvent(
@RequestHeader Map<String, String> headers, @RequestBody String body) {

WebHooksService webhooks = sinchClient.sms().v1().webhooks();

WebHooksService webhooks = sinchClient.sms().webHooks();
// ensure valid authentication to handle request
// See
// https://developers.sinch.com/docs/sms/api-reference/sms/tag/Webhooks/#tag/Webhooks/section/Callbacks
// Contact your account manager to configure your callback sending headers validation or comment
// following line
var validAuth = webhooks.validateAuthenticationHeader(webhooksSecret, headers, body);

// token validation failed
if (!validAuth) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
}

// decode the request payload
WebhooksEvent event = webhooks.parse(body);
SmsEvent event = webhooks.parseEvent(body);

// let business layer process the request
switch (event) {
case InboundText e -> webhooksBusinessLogic.processInboundEvent(e);
case TextMessage e -> webhooksBusinessLogic.processInboundEvent(e);
default -> throw new IllegalStateException("Unexpected value: " + event);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.mycompany.app.sms;

import com.sinch.sdk.SinchClient;
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.api.v1.BatchesService;
import com.sinch.sdk.domains.sms.models.v1.batches.request.TextRequest;
import com.sinch.sdk.domains.sms.models.v1.batches.response.BatchResponse;
import com.sinch.sdk.domains.sms.models.v1.inbounds.TextMessage;
import java.util.Collections;
import java.util.logging.Logger;
import org.springframework.stereotype.Component;
Expand All @@ -14,24 +15,27 @@ public class ServerBusinessLogic {
private final BatchesService batches;

public ServerBusinessLogic(SinchClient sinchClient) {
this.batches = sinchClient.sms().batches();
this.batches = sinchClient.sms().v1().batches();
}

private static final Logger LOGGER = Logger.getLogger(ServerBusinessLogic.class.getName());

public void processInboundEvent(InboundText event) {
public void processInboundEvent(TextMessage event) {

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

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

LOGGER.info("Replying with: " + smsRequest);

batches.send(smsRequest);
BatchResponse response = batches.send(smsRequest);

LOGGER.info("Response: " + response);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ sms:
# Sets the region for SMS
# valid values are US and EU
region:

sms.webhooks.secret:

0 comments on commit 0a90c93

Please sign in to comment.