Node-RED 企业微信机器人—文本消息

目录


一、功能目标

在第一篇的基础上,边沿检测会输出:

text 复制代码
msg.edgeType = rising 或 falling

本篇根据这个字段构造企业微信文本消息:

  • 上升沿:提示设备运行,禁止进入。
  • 下降沿:提示设备停机,并显示运行时长。

整体流程:

text 复制代码
边沿检测
    ↓
消息构造
    ↓
消息队列
    ↓
企业微信HTTP发送

二、模式值转中文

PLC 输出的模式一般是数字,企业微信消息中应转换为可读文字。

示例:

js 复制代码
function getModeName(mode) {
    const modeNum = parseInt(mode);
    switch (modeNum) {
        case 11: return "<模式名称A>";
        case 12: return "<模式名称B>";
        case 21: return "<模式名称C>";
        case 22: return "<模式名称D>";
        default: return `未知模式(${mode})`;
    }
}

注意:这里的模式名称也属于现场业务信息,写公开文章时建议用 <模式名称A> 代替。

三、构造消息模板

根据边沿类型选择模板:

js 复制代码
const messageTemplates = {
    rising: {
        template: (location, time) =>
            `【设备区域】设备运行\n` +
            `当前模式: ${location}\n` +
            `启动时间: ${time}\n` +
            `正在运行,请勿进入现场区域。\n`
    },
    falling: {
        template: (location, time, duration) =>
            `【设备区域】设备停机\n` +
            `运行时长: ${duration}\n` +
            `停机时间: ${time}\n`
    }
};

构造企业微信消息体:

js 复制代码
const edgeType = msg.edgeType || "rising";
const duration = msg.duration || "暂未记录";
const modeValue = flow.get('mode') || 0;
const location = getModeName(modeValue);
const timeStr = formatDateTime(new Date());

let content;
if (edgeType === "rising") {
    content = messageTemplates.rising.template(location, timeStr);
} else {
    content = messageTemplates.falling.template(location, timeStr, duration);
}

msg.payload = {
    msgtype: "text",
    text: {
        content: content,
        mentioned_mobile_list: ["@all"]
    }
};

return msg;

四、消息队列

文本消息进入 q-gate 消息队列。

作用:

  • 给企业微信发送加缓冲。
  • 避免瞬间多条消息连续冲击接口。
  • 后续需要暂停、排队、清空消息时,有统一控制点。

配置示例:

text 复制代码
defaultState:open
maxQueueLength:20
keepNewest:true
persist:true

五、企业微信HTTP发送

使用 http request 节点:

text 复制代码
Method:POST
URL:https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=<企业微信机器人KEY>
Return:text

六、本篇小结

本篇完成了企业微信文本报警:

text 复制代码
rising  -> 设备运行消息
falling -> 设备停机消息

架构上保留了消息队列,后续 PDF 文件消息也可以复用同一个发送出口。


相关推荐
星云API技术支持。40 分钟前
企业微信二次开发机器人:如何实现消息分类、转发与业务分发
机器人·企业微信
天空属于哈夫克342 分钟前
企业微信私域SOP消息节点设置
企业微信
星云API技术支持。3 小时前
企业微信二次开发机器人:如何根据不同群聊配置独立处理逻辑
机器人·企业微信
梦想的旅途23 天前
如何高效调用企业微信通讯录API管理组织架构
java·开发语言·企业微信
梦想的旅途24 天前
企业微信API如何实现自动化办公
运维·自动化·企业微信
天空属于哈夫克34 天前
企业微信API实现企业内部消息精准推送的最佳实践
企业微信
星云API技术支持4 天前
企业微信二次开发:外部群机器人如何把群消息接入工单系统?
机器人·企业微信
星云API技术支持。4 天前
企业微信二次开发:外部群机器人,从群消息到业务系统如何打通?
机器人·企业微信
梦想的旅途24 天前
企业微信API在考勤与审批流程自动化中的应用
运维·自动化·企业微信
梦想的旅途24 天前
企业微信API接口调用频率限制与性能优化技巧
企业微信