thingboard告警信息格式美化

原始报警json内容:

javascript 复制代码
{ 

	"severity": "CRITICAL",
	"acknowledged": false,
	"cleared": false,
	"assigneeId": null,
	"startTs": 1739801102349,
	"endTs": 1739801102349,
	"ackTs": 0,
	"clearTs": 0,
	"assignTs": 0,
	"propagate": false,
	"propagateToOwner": false,
	"propagateToTenant": false,
	"propagateRelationTypes": [],
	"originatorName": "DTU-863569063344204",
	"originatorLabel": "DTU-863569063344204",
	"assignee": null,
	"name": "Inactivity TimeOut",
	"status": "ACTIVE_UNACK",
	"details": {}
}

报警格式美化js代码:

javascript 复制代码
var date = new Date();
var year = date.getFullYear();
var month = ('0' + (date.getMonth() + 1)).slice(-2);
var day = ('0' + date.getDate()).slice(-2);
var hours = ('0' + date.getHours()).slice(-2);
var minutes = ('0' + date.getMinutes()).slice(-2);
var seconds = ('0' + date.getSeconds()).slice(-2);

var currentTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;

// 解析告警时间
function formatUnixTime(unixTimestamp) {
    var date = new Date(unixTimestamp);
    var year = date.getFullYear();
    var month = ('0' + (date.getMonth() + 1)).slice(-2);
    var day = ('0' + date.getDate()).slice(-2);
    var hours = ('0' + date.getHours()).slice(-2);
    var minutes = ('0' + date.getMinutes()).slice(-2);
    var seconds = ('0' + date.getSeconds()).slice(-2);
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
}

// 格式化报警级别
var severityMap = {
    "CRITICAL": "🔥 **严重告警**",
    "MAJOR": "⚠ **主要告警**",
    "MINOR": "🔶 **次要告警**",
    "WARNING": "ℹ **警告**"
};
var severity = severityMap[msg.severity] || "⚠ **未知级别**";

// 确认状态
var acknowledged = msg.acknowledged ? "✅ 已确认" : "❌ 未确认";
var cleared = msg.cleared ? "✅ 已清除" : "❌ 未清除";

// 组装格式化的告警信息
var messageContent =
    "📢 **设备告警通知** 📢\n" +
    "------------------------------\n" +
    "📅 **当前时间**: " + currentTime + "\n" +
    "📍 **设备名称**: " + msg.originatorName + "\n" +
    "🔖 **设备标识**: " + msg.originatorLabel + "\n" +
    "⚠ **告警名称**: " + msg.name + "\n" +
    "🚨 **告警级别**: " + severity + "\n" +
    "🕒 **告警开始时间**: " + formatUnixTime(msg.startTs) + "\n" +
    "🕒 **告警结束时间**: " + formatUnixTime(msg.endTs) + "\n" +
    "🔎 **告警状态**: " + msg.status + "\n" +
    "📌 **确认状态**: " + acknowledged + "\n" +
    "📌 **清除状态**: " + cleared + "\n" +
    "------------------------------";

var newMsg = {
    "msgtype": "text",
    "text": {
        "content": messageContent,
        "mentioned_list": ["wangqing", "@all"],
        "mentioned_mobile_list": ["13800001111", "@all"]
    }
};

return {
    msg: newMsg
};

输出到企业微信消息:

相关推荐
tsumikistep16 小时前
【前后端】接口文档与导入
前端·后端·python·硬件架构
q***385116 小时前
SpringCloud实战十三:Gateway之 Spring Cloud Gateway 动态路由
java·spring cloud·gateway
行走的陀螺仪17 小时前
.vscode 文件夹配置详解
前端·ide·vscode·编辑器·开发实践
小白学大数据17 小时前
Python爬虫伪装策略:如何模拟浏览器正常访问JSP站点
java·开发语言·爬虫·python
smaller_maple17 小时前
linux问题记录1
linux·运维·服务器
2503_9284115617 小时前
11.24 Vue-组件2
前端·javascript·vue.js
Bigger17 小时前
🎨 用一次就爱上的图标定制体验:CustomIcons 实战
前端·react.js·icon
谢尔登18 小时前
原来Webpack在大厂中这样进行性能优化!
前端·webpack·性能优化
程序员西西18 小时前
SpringBoot接口安全:APIKey保护指南
java·spring boot·计算机·程序员·编程·编程开发
summer_west_fish18 小时前
单体VS微服务:架构选择实战指南
java·微服务·架构