企业微信通知和飞书通知增加发送失败的日志打印

This commit is contained in:
lizhongyuan3 2023-12-18 13:53:36 +08:00
parent cf3136d22d
commit 1c9f9fb9c5
2 changed files with 16 additions and 12 deletions

View File

@ -36,7 +36,7 @@ import java.util.Map;
public class LarkAlarm extends AbstractAlarm<AlarmContext> { public class LarkAlarm extends AbstractAlarm<AlarmContext> {
public static final String atLabel = "<at id={0}></at>"; public static final String AT_LABEL = "<at id={0}></at>";
@Override @Override
public Integer getAlarmType() { public Integer getAlarmType() {
@ -45,9 +45,7 @@ public class LarkAlarm extends AbstractAlarm<AlarmContext> {
@Override @Override
public boolean asyncSendMessage(AlarmContext context) { public boolean asyncSendMessage(AlarmContext context) {
threadPoolExecutor.execute(() -> { threadPoolExecutor.execute(() -> syncSendMessage(context));
syncSendMessage(context);
});
return true; return true;
} }
@ -69,12 +67,15 @@ public class LarkAlarm extends AbstractAlarm<AlarmContext> {
HttpRequest request = post.body(JsonUtil.toJsonString(builder), ContentType.JSON.toString()); HttpRequest request = post.body(JsonUtil.toJsonString(builder), ContentType.JSON.toString());
HttpResponse execute = request.execute(); HttpResponse execute = request.execute();
LogUtils.debug(log, JsonUtil.toJsonString(execute)); LogUtils.debug(log, JsonUtil.toJsonString(execute));
if (execute.isOk()) {
return true;
}
log.error("发送lark消息失败:{}", execute.body());
return false;
} catch (Exception e) { } catch (Exception e) {
log.error("发送lark消息失败", e); log.error("发送lark消息失败", e);
return false; return false;
} }
return true;
} }
private List buildElements(String text, List<String> ats) { private List buildElements(String text, List<String> ats) {
@ -123,10 +124,10 @@ public class LarkAlarm extends AbstractAlarm<AlarmContext> {
StringBuilder sb = new StringBuilder(text); StringBuilder sb = new StringBuilder(text);
if (ats.stream().map(String::toLowerCase).anyMatch(SystemConstants.AT_ALL::equals)) { if (ats.stream().map(String::toLowerCase).anyMatch(SystemConstants.AT_ALL::equals)) {
sb.append(MessageFormat.format(atLabel, SystemConstants.AT_ALL)); sb.append(MessageFormat.format(AT_LABEL, SystemConstants.AT_ALL));
} else { } else {
ats.stream().filter(StrUtil::isNotBlank) ats.stream().filter(StrUtil::isNotBlank)
.forEach(at -> sb.append(MessageFormat.format(atLabel, at))); .forEach(at -> sb.append(MessageFormat.format(AT_LABEL, at)));
} }
return sb.toString(); return sb.toString();
} }

View File

@ -26,7 +26,7 @@ import java.util.Map;
@Component @Component
public class QiYeWechatAlarm extends AbstractAlarm<AlarmContext> { public class QiYeWechatAlarm extends AbstractAlarm<AlarmContext> {
public static final String atLabel = "<@{0}>"; public static final String AT_LABEL = "<@{0}>";
@Override @Override
public Integer getAlarmType() { public Integer getAlarmType() {
@ -50,19 +50,22 @@ public class QiYeWechatAlarm extends AbstractAlarm<AlarmContext> {
} }
Map<String, Object> map = MapUtil.newHashMap(); Map<String, Object> map = MapUtil.newHashMap();
QiYeWechatMessageContent messageContent = new QiYeWechatMessageContent(); QiYeWechatMessageContent messageContent = new QiYeWechatMessageContent();
messageContent.setContent(StrUtil.sub(DingDingUtils.getAtText(qiYeWechatAttribute.getAts(), context.getText(), atLabel), 0, 4096)); messageContent.setContent(StrUtil.sub(DingDingUtils.getAtText(qiYeWechatAttribute.getAts(), context.getText(), AT_LABEL), 0, 4096));
map.put("msgtype", "markdown"); map.put("msgtype", "markdown");
map.put("markdown", messageContent); map.put("markdown", messageContent);
HttpRequest post = HttpUtil.createPost(webhookUrl); HttpRequest post = HttpUtil.createPost(webhookUrl);
HttpRequest request = post.body(JsonUtil.toJsonString(map), ContentType.JSON.toString()); HttpRequest request = post.body(JsonUtil.toJsonString(map), ContentType.JSON.toString());
HttpResponse execute = request.execute(); HttpResponse execute = request.execute();
LogUtils.debug(log, JsonUtil.toJsonString(execute)); LogUtils.debug(log, JsonUtil.toJsonString(execute));
if (execute.isOk()) {
return true;
}
log.error("发送企业微信消息失败:{}", execute.body());
return false;
} catch (Exception e) { } catch (Exception e) {
log.error("发送企业微信消息失败", e); log.error("发送企业微信消息失败", e);
return false; return false;
} }
return true;
} }
@Override @Override