Skip to content

Commit

Permalink
Using combinations of webhook data fields as a converstion key for MB…
Browse files Browse the repository at this point in the history
…T and Mailchimp
  • Loading branch information
VSydor committed Jun 29, 2023
1 parent 6761437 commit 11dc384
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.impactupgrade.nucleus.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.impactupgrade.nucleus.entity.JobType;
import com.impactupgrade.nucleus.environment.Environment;
Expand All @@ -14,6 +15,8 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import static com.impactupgrade.nucleus.service.logic.MessagingService.MessageType.SMS;
Expand All @@ -25,6 +28,9 @@
public class MBTController {

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

private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

protected final EnvironmentFactory envFactory;

Expand All @@ -47,11 +53,15 @@ public Response inboundSmsWebhook(
String jobName = "SMS Inbound";
env.startJobLog(JobType.EVENT, null, jobName, "MBT");

// Using combination of subscriber number and today's date
// as a conversation id
// to group all user's messages for current day
String conversationGroupKey = inboundMessageWebhookData.subscriberNo + "::" + new SimpleDateFormat(DATE_FORMAT).format(new Date());
env.messagingService().upsertCrmConversation(
SMS,
inboundMessageWebhookData.message,
SMS,
conversationGroupKey, // TODO: use customParams to contain conversation id?
inboundMessageWebhookData.externalReferenceId,
null); // TODO: use customParams to contain conversation id?
inboundMessageWebhookData.message);

env.endJobLog(jobName);

Expand All @@ -70,12 +80,20 @@ public Response smsStatusWebhook(
) throws Exception {
Environment env = envFactory.init(request);

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

//TODO: store in some way as crm activity?
// Using combination of msisdn and today's date
// as a conversation id
// to group all user's messages' statuses for current day
String conversationGroupKey = messageStatusWebhookData.msisdn + "::" + new SimpleDateFormat(DATE_FORMAT).format(new Date());
env.messagingService().upsertCrmConversation(
SMS,
conversationGroupKey, // TODO: use customParams to contain conversation id?
messageStatusWebhookData.messageId,
messageStatusWebhookData.message);

// env.endJobLog(jobName);
env.endJobLog(jobName);

return Response.ok().build();
}
Expand All @@ -90,7 +108,8 @@ public static final class InboundMessageWebhookData {
public String groupId;
public String communicationCode;
public String messageType;
public String receivedTime; // TODO: date fromat "2019-08-24T14:15:22Z"
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_TIME_FORMAT)
public Date receivedTime;
// 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;
Expand All @@ -104,7 +123,8 @@ public static final class MessageStatusWebhookData {
public String groupName;
public String groupId;
public String communicationCode;
public String deliveredTime; // TODO: date format "2019-08-24T14:15:22Z"
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_TIME_FORMAT)
public Date deliveredTime;
public Map<String, String> properties;
public String statusCode;
public String statusCodeDescription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.List;

import static com.impactupgrade.nucleus.service.logic.MessagingService.MessageType.EMAIL;
Expand All @@ -24,7 +27,9 @@
public class MailchimpController {

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


private static final String DATE_FORMAT = "yyyy-MM-dd";

protected final EnvironmentFactory envFactory;

public MailchimpController(EnvironmentFactory envFactory) {
Expand Down Expand Up @@ -87,11 +92,15 @@ private void processEvent(Event event, Environment env) throws Exception {
return;
}
if ("send".equalsIgnoreCase(event.eventType)) {
// Using sender::recipient::sent-date
// as a conversation id
Date sentAt = Date.from(Instant.ofEpochSecond(event.message.timestamp));
String conversationId = event.message.sender + "::" + event.message.email + "::" + new SimpleDateFormat(DATE_FORMAT).format(sentAt);
env.messagingService().upsertCrmConversation(
EMAIL,
event.message.subject,
EMAIL,
conversationId,
event.message.id,
event.message.email);
event.message.subject); // using subject instead of message body (body n\a in the webhook's payload)
} else {
log.info("skipping event type {}...", event.eventType);
}
Expand Down Expand Up @@ -131,9 +140,8 @@ public static final class Message {
public String email;
public String sender;
public String subject;

//@JsonProperty("ts")
//public Long timestamp;
@JsonProperty("ts")
public Long timestamp; // the integer UTC UNIX timestamp when the message was sent
//TODO: add object definitions, if needed
//@JsonProperty("smtp_events")
//public List<Object> smtpEvents;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Locale;
import java.util.stream.Collectors;

import static com.impactupgrade.nucleus.service.logic.MessagingService.MessageType.SMS;
import static com.impactupgrade.nucleus.util.Utils.noWhitespace;
import static com.impactupgrade.nucleus.util.Utils.trim;

Expand Down Expand Up @@ -269,7 +270,11 @@ public Response conversationsWebhook(

switch (eventType) {
case "onMessageAdded":
env.messagingService().upsertCrmConversation(MessagingService.MessageType.SMS, body, messageSid, conversationSid);
env.messagingService().upsertCrmConversation(
SMS,
conversationSid,
messageSid,
body);
return Response.ok().build();
default:
log.warn("unexpected eventType: " + eventType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public void optOut(CrmContact crmContact) throws Exception {
crmService.updateContact(crmContact);
}

public void upsertCrmConversation(MessageType messageType, String messageBody, String messageSid, String conversationId) throws Exception {
public void upsertCrmConversation(MessageType messageType, String conversationId, String messageSid, String messageBody) throws Exception {
String subject;
if (!Strings.isNullOrEmpty(conversationId)) {
// SMS conversation
Expand Down

0 comments on commit 11dc384

Please sign in to comment.