Skip to content

Instantly share code, notes, and snippets.

@knoxxs
Last active May 27, 2020 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knoxxs/ac9cbb50ba37e6b7ed88fa2800efc13a to your computer and use it in GitHub Desktop.
Save knoxxs/ac9cbb50ba37e6b7ed88fa2800efc13a to your computer and use it in GitHub Desktop.
Different examples like use existing properties & set new properties, send message, make API call & send message, importing python's internal libs, parse Rest API block response
Please follow the examples below. To learn more please refer to the docs:
package com.altin.custom_code;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import morph.base.actions.StringVariable;
import morph.base.actions.impl.SetVariableAction;
import morph.base.beans.CustomCodeResponse;
import java.util.Collections;
import java.util.Map;
// Please don't change the class name 'MorphCustomCode'
public class MorphCustomCode implements RequestHandler<Map<String, Object>, CustomCodeResponse> {
@Override
public CustomCodeResponse handleRequest(Map<String, Object> input, Context context) {
CustomCodeResponse customCodeResponse = new CustomCodeResponse();
// Write your code here. Please refer to the docs to learn more - https://morphdesk.freshdesk.com/en/support/solutions/articles/60000664433
//Access exiting properties like this:
Map<String, Object> userVariables = (Map<String, Object>) input.get("userVariables");
String firstName = String.valueOf(userVariables.get("First Name"));
//Set property
SetVariableAction setFirstNameCapsA = new SetVariableAction();
setFirstNameCapsA.setVariableTitle("First Name Caps");
setFirstNameCapsA.setVariable(new StringVariable().value(firstName.toUpperCase()));
//Set action in response
customCodeResponse.setActions(Collections.singletonList(setFirstNameCapsA));
return customCodeResponse;
}
}
package com.altin.custom_code;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import morph.base.actions.impl.PublishMessageAction;
import morph.base.beans.CustomCodeResponse;
import morph.base.beans.simplifiedmessage.SimplifiedMessage;
import morph.base.beans.simplifiedmessage.SimplifiedMessagePayload;
import morph.base.beans.simplifiedmessage.TextMessagePayload;
import java.util.Collections;
import java.util.Map;
// Please don't change the class name 'MorphCustomCode'
public class MorphCustomCode implements RequestHandler<Map<String, Object>, CustomCodeResponse> {
@Override
public CustomCodeResponse handleRequest(Map<String, Object> input, Context context) {
CustomCodeResponse customCodeResponse = new CustomCodeResponse();
// Write your code here. Please refer to the docs to learn more - https://morphdesk.freshdesk.com/en/support/solutions/articles/60000664433
// Access exiting properties like this:
Map<String, Object> userVariables = (Map<String, Object>) input.get("userVariables");
String firstName = String.valueOf(userVariables.get("First Name"));
// Create message
SimplifiedMessagePayload textMessagePayload = new TextMessagePayload()
.text("Hello " + firstName + ", how can I help you?");
SimplifiedMessage messageData = new SimplifiedMessage();
messageData.setMessages(Collections.singletonList(textMessagePayload));
// Send message
PublishMessageAction publishMessageAction = new PublishMessageAction();
publishMessageAction.setSimplifiedMessage(messageData);
// Set action in response
customCodeResponse.setActions(Collections.singletonList(publishMessageAction));
return customCodeResponse;
}
}
package com.altin.custom_code;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import morph.base.actions.impl.PublishMessageAction;
import morph.base.beans.CustomCodeResponse;
import morph.base.beans.simplifiedmessage.SimplifiedMessage;
import morph.base.beans.simplifiedmessage.SimplifiedMessagePayload;
import morph.base.beans.simplifiedmessage.TextMessagePayload;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.jackson.JacksonFeature;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.util.Collections;
import java.util.Map;
// Please don't change the class name 'MorphCustomCode'
public class MorphCustomCode implements RequestHandler<Map<String, Object>, CustomCodeResponse> {
final static Client HTTP_CLIENT = ClientBuilder.newBuilder()
.withConfig(new ClientConfig().property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true))
.register(JacksonFeature.class).build();
@Override
public CustomCodeResponse handleRequest(Map<String, Object> input, Context context) {
CustomCodeResponse customCodeResponse = new CustomCodeResponse();
// Write your code here. Please refer to the docs to learn more - https://morphdesk.freshdesk.com/en/support/solutions/articles/60000664433
try {
// Call API
WebTarget target = HTTP_CLIENT
.target("http://180.149.246.197/RoxyCinemasWebServ/api/service/GetNowShowing?cinemaid=&userid=0" +
"&platform=4&Flag=home");
Invocation.Builder builder = target.request();
Response response = builder.get();
JsonNode jsonNode = response.readEntity(JsonNode.class);
// Create message
StringBuilder message = new StringBuilder("Hello, please select the movie from below.\n\n");
ArrayNode nowShowingList = (ArrayNode) jsonNode.get("nowShowingList");
for (int i = 0; i < nowShowingList.size(); i++) {
JsonNode show = nowShowingList.get(i);
message.append(i).append(". ").append(show.get("'Title'").asText()).append("\n");
}
SimplifiedMessagePayload textMessagePayload = new TextMessagePayload().text(message.toString());
SimplifiedMessage messageData = new SimplifiedMessage();
messageData.setMessages(Collections.singletonList(textMessagePayload));
// Send message
PublishMessageAction publishMessageAction = new PublishMessageAction();
publishMessageAction.setSimplifiedMessage(messageData);
// Set action in response
customCodeResponse.setActions(Collections.singletonList(publishMessageAction));
} catch (Exception e) {
//
}
return customCodeResponse;
}
}
package com.altin.custom_code;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import morph.base.actions.StringVariable;
import morph.base.actions.impl.SetVariableAction;
import morph.base.beans.CustomCodeResponse;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
// Please don't change the class name 'MorphCustomCode'
public class MorphCustomCode implements RequestHandler<Map<String, Object>, CustomCodeResponse> {
@Override
public CustomCodeResponse handleRequest(Map<String, Object> input, Context context) {
CustomCodeResponse customCodeResponse = new CustomCodeResponse();
// Write your code here. Please refer to the docs to learn more - https://morphdesk.freshdesk.com/en/support/solutions/articles/60000664433
List<String> givenList = Arrays
.asList("Dolittle", "The Gentlemen", "Birds of Prey", "The Lovebirds", "Mulan", "The Beatles: Get Back",
"The King’s Man", "Black Widow");
// Randomly select a movie
Random rand = new Random();
String randomMovieName = givenList.get(rand.nextInt(givenList.size()));
//Set property
SetVariableAction setMovieNameA = new SetVariableAction();
setMovieNameA.setVariableTitle("Movie Name");
setMovieNameA.setVariable(new StringVariable().value(randomMovieName));
//Set action in response
customCodeResponse.setActions(Collections.singletonList(setMovieNameA));
return customCodeResponse;
}
}
package com.altin.custom_code;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import morph.base.actions.StringVariable;
import morph.base.actions.impl.SetVariableAction;
import morph.base.beans.CustomCodeResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
// Please don'o't change the class name 'MorphCustomCode'
public class MorphCustomCode implements RequestHandler<Map<String, Object>, CustomCodeResponse> {
@Override
public CustomCodeResponse handleRequest(Map<String, Object> input, Context context) {
CustomCodeResponse customCodeResponse = new CustomCodeResponse();
// Write your code here. Please refer to the docs to learn more - https://morphdesk.freshdesk.com/en/support/solutions/articles/60000664433
// Access Rest API block response
Map<String, Object> flowVariables = (Map<String, Object>) input.get("flowVariables");
String apiResponse = String.valueOf(flowVariables.get("API Response"));
try {
// Parse response
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readValue(apiResponse, JsonNode.class);
// Use the response
JsonNode data = jsonNode.get("data");
String employeeName = data.get("employee_name").asText();
// Set property
SetVariableAction setEmployeeNameA = new SetVariableAction();
setEmployeeNameA.setVariableTitle("Name");
setEmployeeNameA.setVariable(new StringVariable().value(employeeName));
//Set action in response
customCodeResponse.setActions(Collections.singletonList(setEmployeeNameA));
} catch (IOException e) {
//
}
return customCodeResponse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment