diff --git a/pom.xml b/pom.xml index 4ea96f5..567a6c8 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,11 @@ galimovrf@gmail.com https://github.com/galimru/ + + Desmond Vehar + desmondvehar@gmail.com + https://github.com/dvehar/ + @@ -67,6 +72,7 @@ UTF-8 1.5 2.3.1 + 4.8.1 @@ -162,5 +168,11 @@ gson ${gson.version} + + junit + junit + ${junit.version} + test + diff --git a/src/main/java/net/gpedro/integrations/slack/JsonSerializable.java b/src/main/java/net/gpedro/integrations/slack/JsonSerializable.java new file mode 100644 index 0000000..9b1cbe2 --- /dev/null +++ b/src/main/java/net/gpedro/integrations/slack/JsonSerializable.java @@ -0,0 +1,7 @@ +package net.gpedro.integrations.slack; + +import com.google.gson.JsonObject; + +public interface JsonSerializable { + JsonObject toJson(); +} diff --git a/src/main/java/net/gpedro/integrations/slack/SlackAction.java b/src/main/java/net/gpedro/integrations/slack/SlackAction.java deleted file mode 100644 index b05664d..0000000 --- a/src/main/java/net/gpedro/integrations/slack/SlackAction.java +++ /dev/null @@ -1,92 +0,0 @@ -package net.gpedro.integrations.slack; - -import com.google.gson.JsonObject; - -/** - * @author Galimov Ruslan - */ -public class SlackAction { - - private static final String NAME = "name"; - private static final String TEXT = "text"; - private static final String TYPE = "type"; - private static final String VALUE = "value"; - private static final String CONFIRM = "confirm"; - private static final String STYLE = "style"; - private static final String OPTIONS = "options"; - private static final String OPTION_GROUPS = "option_groups"; - private static final String DATA_SOURCE = "data_source"; - private static final String SELECTED_OPTIONS = "selected_options"; - private static final String MIN_QUERY_LENGTH = "min_query_length"; - - private String name; - private String text; - private SlackActionType type; - private String value; - private SlackActionStyle style; - - public SlackAction(String name, String text, SlackActionType type, String value) { - this.name = name; - this.text = text; - this.type = type; - this.value = value; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - - public SlackActionType getType() { - return type; - } - - public void setType(SlackActionType type) { - this.type = type; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - public SlackActionStyle getStyle() { - return style; - } - - public void setStyle(SlackActionStyle style) { - this.style = style; - } - - public JsonObject toJson() { - final JsonObject data = new JsonObject(); - data.addProperty(NAME, name); - data.addProperty(TEXT, text); - - if (type != null) { - data.addProperty(TYPE, type.getCode()); - } - - data.addProperty(VALUE, value); - - if (style != null) { - data.addProperty(STYLE, style.getCode()); - } - - return data; - } -} diff --git a/src/main/java/net/gpedro/integrations/slack/SlackActionStyle.java b/src/main/java/net/gpedro/integrations/slack/SlackActionStyle.java deleted file mode 100644 index 8383323..0000000 --- a/src/main/java/net/gpedro/integrations/slack/SlackActionStyle.java +++ /dev/null @@ -1,18 +0,0 @@ -package net.gpedro.integrations.slack; - -/** - * @author Galimov Ruslan - */ -public enum SlackActionStyle { - DEFAULT("default"), PRIMARY("primary"), DANGER("danger"); - - private String code; - - SlackActionStyle(String code) { - this.code = code; - } - - public String getCode() { - return code; - } -} diff --git a/src/main/java/net/gpedro/integrations/slack/SlackActionType.java b/src/main/java/net/gpedro/integrations/slack/SlackActionType.java deleted file mode 100644 index 74bccd5..0000000 --- a/src/main/java/net/gpedro/integrations/slack/SlackActionType.java +++ /dev/null @@ -1,19 +0,0 @@ -package net.gpedro.integrations.slack; - -/** - * @author Galimov Ruslan - */ -public enum SlackActionType { - BUTTON("button"), SELECT("select"); - - private String code; - - SlackActionType(String code) { - this.code = code; - } - - public String getCode() { - return code; - } - -} diff --git a/src/main/java/net/gpedro/integrations/slack/SlackAttachment.java b/src/main/java/net/gpedro/integrations/slack/SlackAttachment.java index dbc1cc8..d7f8dcf 100644 --- a/src/main/java/net/gpedro/integrations/slack/SlackAttachment.java +++ b/src/main/java/net/gpedro/integrations/slack/SlackAttachment.java @@ -9,6 +9,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; +import net.gpedro.integrations.slack.attachment.action.SlackAction; /** * Represents an attachment in a slack webhook JSON message. diff --git a/src/main/java/net/gpedro/integrations/slack/attachment/action/BasicButtonSlackAction.java b/src/main/java/net/gpedro/integrations/slack/attachment/action/BasicButtonSlackAction.java new file mode 100644 index 0000000..8de7eb5 --- /dev/null +++ b/src/main/java/net/gpedro/integrations/slack/attachment/action/BasicButtonSlackAction.java @@ -0,0 +1,29 @@ +package net.gpedro.integrations.slack.attachment.action; + +import com.google.gson.JsonObject; + +/** + * @author Desmond Vehar + */ +public class BasicButtonSlackAction extends ButtonSlackAction { + + private static final String URL = "url"; + + private String url; + + public BasicButtonSlackAction(String name, String text, String url) { + super(name, text, Type.BUTTON); + + checkArgForNull(URL, url); + this.url = url; + } + + public JsonObject toJson() { + final JsonObject data = super.toJson(); + + data.addProperty(URL, url); + + return data; + } + +} diff --git a/src/main/java/net/gpedro/integrations/slack/attachment/action/ButtonSlackAction.java b/src/main/java/net/gpedro/integrations/slack/attachment/action/ButtonSlackAction.java new file mode 100644 index 0000000..d834dc7 --- /dev/null +++ b/src/main/java/net/gpedro/integrations/slack/attachment/action/ButtonSlackAction.java @@ -0,0 +1,45 @@ +package net.gpedro.integrations.slack.attachment.action; + +import com.google.gson.JsonObject; +import net.gpedro.integrations.slack.JsonSerializable; + +/** + * @author Desmond Vehar + */ +public abstract class ButtonSlackAction extends SlackAction { + private static final String STYLE = "style"; + + private Style style; + + ButtonSlackAction(String name, String text, Type type) { + super(name, text, type); + } + + public void setStyle(Style style) { + this.style = style; + } + + public JsonObject toJson() { + final JsonObject data = super.toJson(); + + if (style != null) { + data.addProperty(STYLE, style.getCode()); + } + + return data; + } + + public enum Style { + DEFAULT("default"), PRIMARY("primary"), DANGER("danger"); + + private String code; + + Style(String code) { + this.code = code; + } + + public String getCode() { + return code; + } + } +} diff --git a/src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveButtonSlackAction.java b/src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveButtonSlackAction.java new file mode 100644 index 0000000..e0713da --- /dev/null +++ b/src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveButtonSlackAction.java @@ -0,0 +1,62 @@ +package net.gpedro.integrations.slack.attachment.action; + +import com.google.gson.JsonObject; +import net.gpedro.integrations.slack.JsonSerializable; + +/** + * @author Desmond Vehar + */ +public class InteractiveButtonSlackAction extends ButtonSlackAction { + + private static final String VALUE = "value"; + private static final String CONFIRM = "confirm"; + + private String value; + private Confirm confirm; + + public InteractiveButtonSlackAction(String name, String text, String value) { + super(name, text, Type.BUTTON); + + checkArgForNull(VALUE, value); + this.value = value; + } + + public void setConfirm(Confirm confirm) { + this.confirm = confirm; + } + + public JsonObject toJson() { + final JsonObject data = super.toJson(); + + data.addProperty(VALUE, value); + + if (confirm != null) { + data.add(CONFIRM, confirm.toJson()); + } + + return data; + } + + public static class Confirm implements JsonSerializable { + private String title; + private String text; + private String ok_text; + private String dismiss_text; + + public Confirm(String title, String text, String ok_text, String dismiss_text) { + this.title = title; + this.text = text; + this.ok_text = ok_text; + this.dismiss_text = dismiss_text; + } + + public JsonObject toJson() { + JsonObject data = new JsonObject(); + data.addProperty("title", title); + data.addProperty("text", text); + data.addProperty("ok_text", ok_text); + data.addProperty("dismiss_text", dismiss_text); + return data; + } + } +} diff --git a/src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveSelectSlackAction.java b/src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveSelectSlackAction.java new file mode 100644 index 0000000..cdccc79 --- /dev/null +++ b/src/main/java/net/gpedro/integrations/slack/attachment/action/InteractiveSelectSlackAction.java @@ -0,0 +1,60 @@ +package net.gpedro.integrations.slack.attachment.action; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import net.gpedro.integrations.slack.JsonSerializable; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Desmond Vehar + */ +public class InteractiveSelectSlackAction extends SlackAction { + + private static final String OPTIONS = "options"; + + private List