插件介绍


PushMe支持插件功能,可以对接收的消息做进一步处理。支持添加多个插件,支持插件排序、设置插件状态。
从PushMeClient v2.0开始,PushMeClient也支持插件功能了,用法同PushMe APP。

仓库地址


Git仓库,主要提供一些插件demo以便参考、使用。
Github:https://github.com/yafoo/pushme-plugin
Gitee:https://gitee.com/yafu/pushme-plugin

插件环境


PushMe APP插件

PushMe插件以QuickJS为脚本引擎,所以只支持js语言,支持ES6语法。所有插件以同步方式运行,所以不支持Promise、Async。
PushMe v3.5.0开始内置base64.encode、base64.decode、md5、toast函数,方便使用。

PushMe Client插件

PushMeClient插件运行在Worker中,部分浏览器函数可能不支持。

插件规范


一个插件就是一个匿名函数,第一个参数为接收到的msg消息对象,包含4个属性,分别为`title`、`content`、`date`和`type`,都是String类型。
第二个参数next为下一个插件的引用。
在函数体内,对msg消息进行加工处理,处理完后,请调用next函数,如果不执行`next();`语句,后面的插件将不会执行。也可以先执行`next();`语句即执行后面插件后,再对msg消息做处理。
所有的插件以著名的洋葱模型运行。
注意:对于消息类型type属性,数据消息和普通消息不能互转。text、markdown为普通消息,data、markdata、chart为数据消息。

插件demo:


智能主题(自动给消息增加主题样式)
/**
* @name 智能主题
* @description 根据消息标题文字,自动设置主题
* @param {Object} msg - 消息体
* @param {String} msg.title - 消息标题
* @param {String} msg.content - 消息内容
* @param {String} msg.date - 消息时间
* @param {String} msg.type - 消息类型
* @param {Function} next - 下一个插件
*/
function(msg, next) {
    if(!['', 'text', 'markdown', 'html'].includes(msg.type)) return next();
    if(/^\[[iswf]\]/.test(msg.title)) return next();
    
    const theme = Object.entries({
        f: ['失败', '错误', '异常', '失效', '过期', '不存在'],
        w: ['告警', '警告', '通知', '提示', '提醒'],
        s: ['成功', '完成'],
        i: ['消息', '信息']
    }).find(([_, words]) => words.some(word => msg.title.includes(word)))?.[0];
    
    if(theme) msg.title = `[${theme}]${msg.title}`;
    next();
}

插件推荐


更多插件,请关注Git仓库