-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MBT inbound/message-status webhooks; Saving inbound messages in…
…to crm activity (tasks)
- Loading branch information
Showing
3 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/main/java/com/impactupgrade/nucleus/controller/MBTController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters