Skip to content

Commit

Permalink
Merge pull request #20 from sinch/DEVEXP-238-conversation-server-temp…
Browse files Browse the repository at this point in the history
…late

DEVEXP-238: Conversation server template
  • Loading branch information
JPPortier authored Oct 9, 2024
2 parents 89bb715 + 699a1b7 commit 035b386
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 6 deletions.
13 changes: 7 additions & 6 deletions templates/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ Located in `server` section:
### EndPoints
When server is online, declared controllers will respond to following endpoints

| Service | Endpoint |
|--------------|--------------------|
| Numbers | /NumbersEvent |
| SMS | /SmsEvent |
| Verification | /VerificationEvent |
| Voice | /VoiceEvent |
| Service | Endpoint |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|
| [Conversation](https://developers.sinch.com/docs/conversation/callbacks) | /ConversationEvent |
| [Numbers](https://developers.sinch.com/docs/numbers/api-reference/numbers/tag/Numbers-Callbacks/#tag/Numbers-Callbacks/operation/ImportedNumberService_EventsCallback) | /NumbersEvent |
| [SMS](https://developers.sinch.com/docs/sms/api-reference/sms/tag/Webhooks/#tag/Webhooks/section/Callbacks) | /SmsEvent |
| [Verification](https://developers.sinch.com/docs/verification/api-reference/verification/tag/Verification-callbacks/) | /VerificationEvent |
| [Voice](https://developers.sinch.com/docs/voice/api-reference/voice/tag/Callbacks/) | /VoiceEvent |

## Use ngrok to forward request to local server

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.mycompany.app.conversation;

import com.sinch.sdk.SinchClient;
import com.sinch.sdk.domains.conversation.api.v1.WebHooksService;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.ConversationWebhookEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.capability.CapabilityEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.channel.ChannelEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactCreateEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactDeleteEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactIdentitiesDuplicationEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactMergeEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactUpdateEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.conversation.ConversationDeleteEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.conversation.ConversationStartEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.conversation.ConversationStopEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.delivery.EventDeliveryReceiptEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.delivery.MessageDeliveryReceiptEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.inbound.InboundEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.message.MessageInboundEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.message.MessageSubmitEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.opting.OptInEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.opting.OptOutEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.record.RecordNotificationEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.smartconversations.MessageInboundSmartConversationRedactionEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.smartconversations.SmartConversationsEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.unsupported.UnsupportedCallbackEvent;
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("Conversation")
public class Controller {

private final SinchClient sinchClient;
private final ServerBusinessLogic webhooksBusinessLogic;

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

@Autowired
public Controller(SinchClient sinchClient, ServerBusinessLogic webhooksBusinessLogic) {
this.sinchClient = sinchClient;
this.webhooksBusinessLogic = webhooksBusinessLogic;
}

@PostMapping(
value = "/ConversationEvent",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Void> ConversationEvent(
@RequestHeader Map<String, String> headers, @RequestBody String body) {

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

// ensure valid authentication to handle request
var validAuth = webhooks.validateAuthenticationHeader(webhooksSecret, headers, body);

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

// decode the request payload
ConversationWebhookEvent event = webhooks.parseEvent(body);

// let business layer process the request
switch (event) {
case CapabilityEvent e -> webhooksBusinessLogic.handleCapabilityEvent(e);
case ChannelEvent e -> webhooksBusinessLogic.handleChannelEvent(e);
case ContactCreateEvent e -> webhooksBusinessLogic.handleContactCreateEvent(e);
case ContactDeleteEvent e -> webhooksBusinessLogic.handleContactDeleteEvent(e);
case ContactIdentitiesDuplicationEvent e ->
webhooksBusinessLogic.handleContactIdentitiesDuplicationEvent(e);
case ContactMergeEvent e -> webhooksBusinessLogic.handleContactMergeEvent(e);
case ContactUpdateEvent e -> webhooksBusinessLogic.handleContactUpdateEvent(e);
case ConversationDeleteEvent e -> webhooksBusinessLogic.handleConversationDeleteEvent(e);
case ConversationStartEvent e -> webhooksBusinessLogic.handleConversationStartEvent(e);
case ConversationStopEvent e -> webhooksBusinessLogic.handleConversationStopEvent(e);
case EventDeliveryReceiptEvent e -> webhooksBusinessLogic.handleEventDeliveryReceiptEvent(e);
case InboundEvent e -> webhooksBusinessLogic.handleInboundEvent(e);
case MessageDeliveryReceiptEvent e ->
webhooksBusinessLogic.handleMessageDeliveryReceiptEvent(e);
case MessageInboundEvent e -> webhooksBusinessLogic.handleMessageInboundEvent(e);
case MessageInboundSmartConversationRedactionEvent e ->
webhooksBusinessLogic.handleMessageInboundSmartConversationRedactionEvent(e);
case MessageSubmitEvent e -> webhooksBusinessLogic.handleMessageSubmitEvent(e);
case OptInEvent e -> webhooksBusinessLogic.handleOptInEvent(e);
case OptOutEvent e -> webhooksBusinessLogic.handleOptOutEvent(e);
case RecordNotificationEvent e -> webhooksBusinessLogic.handleRecordNotificationEvent(e);
case SmartConversationsEvent e -> webhooksBusinessLogic.handleSmartConversationsEvent(e);
case UnsupportedCallbackEvent e -> webhooksBusinessLogic.handleUnsupportedCallbackEvent(e);
default -> throw new IllegalStateException("Unexpected value: " + event);
}

return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.mycompany.app.conversation;

import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.capability.CapabilityEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.channel.ChannelEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactCreateEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactDeleteEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactIdentitiesDuplicationEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactMergeEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.contact.ContactUpdateEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.conversation.ConversationDeleteEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.conversation.ConversationStartEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.conversation.ConversationStopEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.delivery.EventDeliveryReceiptEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.delivery.MessageDeliveryReceiptEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.inbound.InboundEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.message.MessageInboundEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.message.MessageSubmitEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.opting.OptInEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.opting.OptOutEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.record.RecordNotificationEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.smartconversations.MessageInboundSmartConversationRedactionEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.smartconversations.SmartConversationsEvent;
import com.sinch.sdk.domains.conversation.models.v1.webhooks.events.unsupported.UnsupportedCallbackEvent;
import java.util.logging.Logger;
import org.springframework.stereotype.Component;

@Component("ConversationServerBusinessLogic")
public class ServerBusinessLogic {

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

public void handleCapabilityEvent(CapabilityEvent event) {

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

public void handleChannelEvent(ChannelEvent event) {

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

public void handleContactCreateEvent(ContactCreateEvent event) {

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

public void handleContactDeleteEvent(ContactDeleteEvent event) {

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

public void handleContactIdentitiesDuplicationEvent(ContactIdentitiesDuplicationEvent event) {

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

public void handleContactMergeEvent(ContactMergeEvent event) {

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

public void handleContactUpdateEvent(ContactUpdateEvent event) {

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

public void handleConversationDeleteEvent(ConversationDeleteEvent event) {

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

public void handleConversationStartEvent(ConversationStartEvent event) {

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

public void handleConversationStopEvent(ConversationStopEvent event) {

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

public void handleEventDeliveryReceiptEvent(EventDeliveryReceiptEvent event) {

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

public void handleInboundEvent(InboundEvent event) {

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

public void handleMessageDeliveryReceiptEvent(MessageDeliveryReceiptEvent event) {

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

public void handleMessageInboundEvent(MessageInboundEvent event) {

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

public void handleMessageInboundSmartConversationRedactionEvent(
MessageInboundSmartConversationRedactionEvent event) {

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

public void handleMessageSubmitEvent(MessageSubmitEvent event) {

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

public void handleOptInEvent(OptInEvent event) {

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

public void handleOptOutEvent(OptOutEvent event) {

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

public void handleRecordNotificationEvent(RecordNotificationEvent event) {

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

public void handleSmartConversationsEvent(SmartConversationsEvent event) {

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

public void handleUnsupportedCallbackEvent(UnsupportedCallbackEvent event) {

LOGGER.info("Handle event: " + event);
}
}
3 changes: 3 additions & 0 deletions templates/server/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ sms:

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

# Secret value set for Conversation webhooks calls validation
conversation.webhooks.secret:

0 comments on commit 035b386

Please sign in to comment.