Slack rule node refactoring; default notification settings improvements
This commit is contained in:
parent
cd50eabd40
commit
d221cf1354
@ -148,21 +148,33 @@ public class DefaultNotificationSettingsService implements NotificationSettingsS
|
|||||||
}
|
}
|
||||||
|
|
||||||
NotificationTarget originatorEntityOwnerUsers = createTarget(tenantId, "Users of the entity owner", new OriginatorEntityOwnerUsersFilter(),
|
NotificationTarget originatorEntityOwnerUsers = createTarget(tenantId, "Users of the entity owner", new OriginatorEntityOwnerUsersFilter(),
|
||||||
"Customer users in case trigger entity (e.g. alarm) has customer, tenant admins otherwise");
|
"In case trigger entity (e.g. created device or alarm) is owned by customer, then recipients are this customer's users, otherwise tenant admins");
|
||||||
NotificationTarget affectedUser = createTarget(tenantId, "Affected user", new AffectedUserFilter(),
|
NotificationTarget affectedUser = createTarget(tenantId, "Affected user", new AffectedUserFilter(),
|
||||||
"If rule trigger is an action that affects some user (e.g. alarm assigned to user) - this user");
|
"If rule trigger is an action that affects some user (e.g. alarm assigned to user) - this user");
|
||||||
|
|
||||||
NotificationTemplate alarmNotificationTemplate = createTemplate(tenantId, "Alarm notification", NotificationType.ALARM,
|
NotificationTemplate newAlarmNotificationTemplate = createTemplate(tenantId, "New alarm notification", NotificationType.ALARM,
|
||||||
|
"New alarm '${alarmType}'",
|
||||||
|
"Severity: ${alarmSeverity}, originator: ${alarmOriginatorEntityType} '${alarmOriginatorName}'",
|
||||||
|
"notifications", null, null);
|
||||||
|
AlarmNotificationRuleTriggerConfig newAlarmRuleTriggerConfig = new AlarmNotificationRuleTriggerConfig();
|
||||||
|
newAlarmRuleTriggerConfig.setAlarmTypes(null);
|
||||||
|
newAlarmRuleTriggerConfig.setAlarmSeverities(null);
|
||||||
|
newAlarmRuleTriggerConfig.setNotifyOn(Set.of(AlarmAction.CREATED));
|
||||||
|
createRule(tenantId, "New alarm", newAlarmNotificationTemplate.getId(), newAlarmRuleTriggerConfig,
|
||||||
|
List.of(tenantAdmins.getId(), originatorEntityOwnerUsers.getId()), "Send notification to tenant admins and alarm's customer users " +
|
||||||
|
"when an alarm is created");
|
||||||
|
|
||||||
|
NotificationTemplate alarmUpdateNotificationTemplate = createTemplate(tenantId, "Alarm update notification", NotificationType.ALARM,
|
||||||
"Alarm '${alarmType}' - ${action}",
|
"Alarm '${alarmType}' - ${action}",
|
||||||
"Severity: ${alarmSeverity}, originator: ${alarmOriginatorEntityType} '${alarmOriginatorName}'",
|
"Severity: ${alarmSeverity}, originator: ${alarmOriginatorEntityType} '${alarmOriginatorName}'",
|
||||||
"notifications", null, null);
|
"notifications", null, null);
|
||||||
AlarmNotificationRuleTriggerConfig alarmRuleTriggerConfig = new AlarmNotificationRuleTriggerConfig();
|
AlarmNotificationRuleTriggerConfig alarmRuleTriggerConfig = new AlarmNotificationRuleTriggerConfig();
|
||||||
alarmRuleTriggerConfig.setAlarmTypes(null);
|
alarmRuleTriggerConfig.setAlarmTypes(null);
|
||||||
alarmRuleTriggerConfig.setAlarmSeverities(null);
|
alarmRuleTriggerConfig.setAlarmSeverities(null);
|
||||||
alarmRuleTriggerConfig.setNotifyOn(Set.of(AlarmAction.CREATED, AlarmAction.SEVERITY_CHANGED, AlarmAction.ACKNOWLEDGED, AlarmAction.CLEARED));
|
alarmRuleTriggerConfig.setNotifyOn(Set.of(AlarmAction.SEVERITY_CHANGED, AlarmAction.ACKNOWLEDGED, AlarmAction.CLEARED));
|
||||||
createRule(tenantId, "Alarm", alarmNotificationTemplate.getId(), alarmRuleTriggerConfig,
|
createRule(tenantId, "Alarm update", alarmUpdateNotificationTemplate.getId(), alarmRuleTriggerConfig,
|
||||||
List.of(originatorEntityOwnerUsers.getId()), "Send notification to tenant admins or customer users " +
|
List.of(tenantAdmins.getId(), originatorEntityOwnerUsers.getId()), "Send notification to tenant admins and alarm's customer users " +
|
||||||
"when any alarm is created, updated or cleared");
|
"when any alarm is updated or cleared");
|
||||||
|
|
||||||
NotificationTemplate deviceActionNotificationTemplate = createTemplate(tenantId, "Device action notification", NotificationType.ENTITY_ACTION,
|
NotificationTemplate deviceActionNotificationTemplate = createTemplate(tenantId, "Device action notification", NotificationType.ENTITY_ACTION,
|
||||||
"${entityType} was ${actionType}",
|
"${entityType} was ${actionType}",
|
||||||
|
|||||||
@ -15,15 +15,12 @@
|
|||||||
*/
|
*/
|
||||||
package org.thingsboard.rule.engine.notification;
|
package org.thingsboard.rule.engine.notification;
|
||||||
|
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
import org.thingsboard.common.util.DonAsynchron;
|
import org.thingsboard.common.util.DonAsynchron;
|
||||||
import org.thingsboard.rule.engine.api.RuleNode;
|
import org.thingsboard.rule.engine.api.RuleNode;
|
||||||
import org.thingsboard.rule.engine.api.TbContext;
|
import org.thingsboard.rule.engine.api.TbContext;
|
||||||
import org.thingsboard.rule.engine.api.TbNode;
|
import org.thingsboard.rule.engine.api.TbNode;
|
||||||
import org.thingsboard.rule.engine.api.TbNodeConfiguration;
|
import org.thingsboard.rule.engine.api.TbNodeConfiguration;
|
||||||
import org.thingsboard.rule.engine.api.TbNodeException;
|
import org.thingsboard.rule.engine.api.TbNodeException;
|
||||||
import org.thingsboard.server.common.data.notification.targets.slack.SlackConversation;
|
|
||||||
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
|
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
|
||||||
import org.thingsboard.server.common.data.plugin.ComponentType;
|
import org.thingsboard.server.common.data.plugin.ComponentType;
|
||||||
import org.thingsboard.server.common.msg.TbMsg;
|
import org.thingsboard.server.common.msg.TbMsg;
|
||||||
@ -34,8 +31,8 @@ import java.util.concurrent.ExecutionException;
|
|||||||
type = ComponentType.EXTERNAL,
|
type = ComponentType.EXTERNAL,
|
||||||
name = "send to Slack",
|
name = "send to Slack",
|
||||||
configClazz = TbSlackNodeConfiguration.class,
|
configClazz = TbSlackNodeConfiguration.class,
|
||||||
nodeDescription = "Send message to a Slack channel or user",
|
nodeDescription = "Send message via Slack",
|
||||||
nodeDetails = "",
|
nodeDetails = "Sends message to a Slack channel or user",
|
||||||
uiResources = {"static/rulenode/rulenode-core-config.js"}
|
uiResources = {"static/rulenode/rulenode-core-config.js"}
|
||||||
)
|
)
|
||||||
public class TbSlackNode implements TbNode {
|
public class TbSlackNode implements TbNode {
|
||||||
@ -50,7 +47,7 @@ public class TbSlackNode implements TbNode {
|
|||||||
@Override
|
@Override
|
||||||
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
|
public void onMsg(TbContext ctx, TbMsg msg) throws ExecutionException, InterruptedException, TbNodeException {
|
||||||
String token;
|
String token;
|
||||||
if (config.isUseDefaultNotificationSettings()) {
|
if (config.isUseSystemSettings()) {
|
||||||
token = ctx.getSlackService().getToken(ctx.getTenantId());
|
token = ctx.getSlackService().getToken(ctx.getTenantId());
|
||||||
} else {
|
} else {
|
||||||
token = config.getBotToken();
|
token = config.getBotToken();
|
||||||
@ -60,28 +57,11 @@ public class TbSlackNode implements TbNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String message = TbNodeUtils.processPattern(config.getMessageTemplate(), msg);
|
String message = TbNodeUtils.processPattern(config.getMessageTemplate(), msg);
|
||||||
|
DonAsynchron.withCallback(ctx.getExternalCallExecutor().executeAsync(() -> {
|
||||||
ListenableFuture<?> result;
|
ctx.getSlackService().sendMessage(ctx.getTenantId(), token, config.getConversation().getId(), message);
|
||||||
if (StringUtils.isNotEmpty(config.getConversationId())) {
|
}),
|
||||||
result = ctx.getExternalCallExecutor().executeAsync(() -> {
|
r -> ctx.tellSuccess(msg),
|
||||||
ctx.getSlackService().sendMessage(ctx.getTenantId(), token, config.getConversationId(), message);
|
e -> ctx.tellFailure(msg, e));
|
||||||
});
|
|
||||||
} else {
|
|
||||||
result = ctx.getExternalCallExecutor().executeAsync(() -> {
|
|
||||||
SlackConversation conversation = ctx.getSlackService().findConversation(ctx.getTenantId(), token, config.getConversationType(), config.getConversationNamePattern());
|
|
||||||
if (conversation == null) {
|
|
||||||
throw new IllegalArgumentException("Couldn't find conversation by name pattern");
|
|
||||||
}
|
|
||||||
ctx.getSlackService().sendMessage(ctx.getTenantId(), token, conversation.getId(), message);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
DonAsynchron.withCallback(result, r -> {
|
|
||||||
ctx.tellSuccess(msg);
|
|
||||||
},
|
|
||||||
e -> {
|
|
||||||
ctx.tellFailure(msg, e);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,8 +17,10 @@ package org.thingsboard.rule.engine.notification;
|
|||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.thingsboard.rule.engine.api.NodeConfiguration;
|
import org.thingsboard.rule.engine.api.NodeConfiguration;
|
||||||
|
import org.thingsboard.server.common.data.notification.targets.slack.SlackConversation;
|
||||||
import org.thingsboard.server.common.data.notification.targets.slack.SlackConversationType;
|
import org.thingsboard.server.common.data.notification.targets.slack.SlackConversationType;
|
||||||
|
|
||||||
|
import javax.validation.Valid;
|
||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
@ -26,14 +28,14 @@ import javax.validation.constraints.NotNull;
|
|||||||
public class TbSlackNodeConfiguration implements NodeConfiguration<TbSlackNodeConfiguration> {
|
public class TbSlackNodeConfiguration implements NodeConfiguration<TbSlackNodeConfiguration> {
|
||||||
|
|
||||||
private String botToken;
|
private String botToken;
|
||||||
private boolean useDefaultNotificationSettings;
|
private boolean useSystemSettings;
|
||||||
@NotEmpty
|
@NotEmpty
|
||||||
private String messageTemplate;
|
private String messageTemplate;
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private SlackConversationType conversationType;
|
private SlackConversationType conversationType;
|
||||||
private String conversationId; // if not set, need to specify conversationNamePattern
|
@NotNull
|
||||||
private String conversationNamePattern;
|
@Valid
|
||||||
|
private SlackConversation conversation;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TbSlackNodeConfiguration defaultConfiguration() {
|
public TbSlackNodeConfiguration defaultConfiguration() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user