Skip to content

Commit

Permalink
Added MBT inbound/message-status webhooks; Saving inbound messages in…
Browse files Browse the repository at this point in the history
…to crm activity (tasks)
  • Loading branch information
VSydor committed Jun 19, 2023
1 parent 9a410d0 commit bab7584
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/com/impactupgrade/nucleus/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.impactupgrade.nucleus.controller.EmailController;
import com.impactupgrade.nucleus.controller.EventsController;
import com.impactupgrade.nucleus.controller.JobController;
import com.impactupgrade.nucleus.controller.MBTController;
import com.impactupgrade.nucleus.controller.MailchimpController;
import com.impactupgrade.nucleus.controller.PaymentGatewayController;
import com.impactupgrade.nucleus.controller.ScheduledJobController;
Expand Down Expand Up @@ -98,6 +99,7 @@ public void start() throws Exception {
apiConfig.register(twilioFrontlineController());
apiConfig.register(accountingController());
apiConfig.register(jobController());
apiConfig.register(mbtController());

// Controllers that require DB connectivity -- prevent JDBC/Hikari connection errors.
if ("true".equalsIgnoreCase(System.getenv("DATABASE_CONNECTED"))) {
Expand Down Expand Up @@ -157,6 +159,7 @@ public void registerServlets(ServletContextHandler context) throws Exception {}
protected ScheduledJobController scheduledJobController() { return new ScheduledJobController(envFactory); }
protected AccountingController accountingController() { return new AccountingController(envFactory); }
protected JobController jobController() { return new JobController(envFactory); }
protected MBTController mbtController() { return new MBTController(envFactory); }
public EnvironmentFactory getEnvironmentFactory() {
return envFactory;
}
Expand Down
114 changes: 114 additions & 0 deletions src/main/java/com/impactupgrade/nucleus/controller/MBTController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.impactupgrade.nucleus.controller;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.impactupgrade.nucleus.entity.JobType;
import com.impactupgrade.nucleus.environment.Environment;
import com.impactupgrade.nucleus.environment.EnvironmentFactory;
import com.impactupgrade.nucleus.security.SecurityUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Map;

/**
* To receive webhooks from MBT as messages are sent/received.
*/
@Path("/mbt")
public class MBTController {

private static final Logger log = LogManager.getLogger(MBTController.class);

protected final EnvironmentFactory envFactory;

public MBTController(EnvironmentFactory envFactory) {
this.envFactory = envFactory;
}

/**
* The Inbound Messages webhook is triggered by receipt of a message to your MBT account.
*/
@Path("/inbound/sms/webhook")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response inboundSmsWebhook(
InboundMessageWebhookData inboundMessageWebhookData,
@Context HttpServletRequest request
) throws Exception {
Environment env = envFactory.init(request);
SecurityUtil.verifyApiKey(env);

String jobName = "SMS Inbound";
env.startJobLog(JobType.EVENT, null, jobName, "MBT");

env.messagingService().upsertCrmConversation(
inboundMessageWebhookData.message,
inboundMessageWebhookData.externalReferenceId,
null); // TODO: use customParams to contain conversation id?

env.endJobLog(jobName);

return Response.ok().build();
}

/**
* The Message Status webhook is triggered as a message sent from an Account progresses to a Subscriber.
*/
@Path("/sms/status")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response smsStatusWebhook(
MessageStatusWebhookData messageStatusWebhookData,
@Context HttpServletRequest request
) throws Exception {
Environment env = envFactory.init(request);
SecurityUtil.verifyApiKey(env);

// String jobName = "SMS Status";
// env.startJobLog(JobType.EVENT, null, jobName, "MBT");

//TODO: store in some way as crm activity?

// env.endJobLog(jobName);

return Response.ok().build();
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static final class InboundMessageWebhookData {
public String externalReferenceId;
public String type;
public String message;
public String subscriberNo;
public String groupName;
public String groupId;
public String communicationCode;
public String messageType;
public String receivedTime; // TODO: date fromat "2019-08-24T14:15:22Z"
// Every message received sends the data shown in sample to the target URL.
// The customParams parameters may be specified and will be implemented by MBT.
public Map<String, String> customParams;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static final class MessageStatusWebhookData {
public String accountId;
public String message;
public String msisdn;
public String groupName;
public String groupId;
public String communicationCode;
public String deliveredTime; // TODO: date format "2019-08-24T14:15:22Z"
public Map<String, String> properties;
public String statusCode;
public String statusCodeDescription;
public String messageId;
public String referenceId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,14 @@ public void optOut(CrmContact crmContact) throws Exception {
}

public void upsertCrmConversation(String messageBody, String messageSid, String conversationId) throws Exception {
String subject = "SMS CONVERSATION: " + conversationId;
String subject;
if (!Strings.isNullOrEmpty(conversationId)) {
// SMS conversation
subject = "SMS CONVERSATION: " + conversationId;
} else {
// Single sms message
subject = "SMS MESSAGE: " + messageSid;
}

Optional<CrmTask> _crmTask = env.primaryCrmService().getTaskBySubject(subject);
CrmTask crmTask;
Expand Down

0 comments on commit bab7584

Please sign in to comment.