0.1.2 • Published 3 years ago

tb-notify v0.1.2

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
3 years ago

消息推送工具

JavaScript Style Guide

调用第三方消息推送

调用方法

实例初始化

const Notify = require('tb-notify')
const notify = new Notify(config)
  • config object - 必填
    • tbHost string - 必填 - tb地址,用于生成跳转链接
    • msgType string - 非必填 - 推送消息格式化类型,默认为markdown
    • markdownTemplate object - 非必填 - markdown格式消息正文模版
    • contentTemplate object - 非必填 - 正文主体模版

config配置模版详解

推送消息正文分为两部分,消息主体部分,与格式化类型无关,只与hook事件类型有关,例如task.create事件的消息主体为张三 创建了任务 测试任务,可以通过config.contentTemplate定制。另一部分是除消息主体之外的其他信息和样式,可由config.markdownTemplate定制。

两种模版配置方式
字符串

contentTemplate示例:

contentTemplate: {
    'project.rename': '{userName} 更新了 {oldProjectName} 的项目名称为 {newProjectName}'
}

markdownTemplate示例:

markdownTemplate: {
    'task': `{content}\n>**任务详情**\n>任务标题:<font color=\"comment\">{taskContent}</font>\n>执行者:<font color=\"comment\">{executor}</font>\n>项目名称:<font color=\"comment\">{projectName}</font>\n>\n>[查看详情]({url})`,
}
  • contentTemplateproject.rename是hook事件名称
  • markdownTemplatetask是hook事件大类型
  • 大括号{}中的为变量,会由传入的hookData自动生成,变量名不要改动
函数

contentTemplate示例:

contentTemplate: {
    'project.rename': (options) => `${options.userName} 更新了 ${optins.oldProjectName} 的项目名称为 ${options.newProjectName}`
}

markdownTemplate示例:

markdownTemplate: {
    'task': (options) => `${options.content}\n>**任务详情**\n>任务标题:<font color=\"comment\">${options.taskContent}</font>\n>执行者:<font color=\"comment\">${options.executor}</font>\n>项目名称:<font color=\"comment\">${options.projectName}</font>\n>\n>[查看详情](${options.url})`
}
  • options是对应事件的可用变量
markdownTemplate模版可用类型和对应变量
  • task
    • content string - 消息主体
    • taskContent string - 任务标题
    • executor string - 任务执行者
    • projectName string - 项目名称
    • url string - 默认跳转链接
  • event
    • content string - 消息主体
    • eventTitle string - 日程标题
    • creator string - 日程创建者
    • projectName string - 项目名称
    • url string - 默认跳转链接
contentTemplate模版可用参数
  • project.rename
    • userName string - 操作者姓名
    • oldProjectName string - 旧项目名
    • newProjectName string - 新项目名
  • project.suspend
    • userName string - 操作者姓名
    • projectName string - 项目名称
  • project.archive
    • userName string - 操作者姓名
    • projectName string - 项目名称
  • project.remove
    • userName string - 操作者姓名
    • projectName string - 项目名称
  • project.unsuspend
    • userName string - 操作者姓名
    • projectName string - 项目名称
  • project.update.customfields
    • userName string - 操作者姓名
    • projectName string - 项目名称
  • project.update.statusDegree
    • userName string - 操作者姓名
    • projectName string - 项目名称
  • project.create.customfieldlinks
    • userName string - 操作者姓名
    • projectName string - 项目名称
  • project.remove.customfieldlinks
    • userName string - 操作者姓名
    • projectName string - 项目名称
  • task.create
    • userName string - 操作人姓名
    • taskContent string - 任务标题
  • task.rename
    • userName string - 操作人姓名
    • oldContent string - 旧任务标题
    • newContent string - 新任务标题
  • task.update.startDate
    • userName string - 操作人姓名
    • taskContent string - 任务标题
    • time string - 任务开始时间
  • task.update.dueDate
    • userName string - 操作人姓名
    • taskContent string - 任务标题
    • time string - 任务结束时间
  • task.update.customfields
    • userName string - 操作人姓名
    • taskContent string - 任务标题
  • task.move
    • userName string - 操作人姓名
    • taskContent string - 任务标题
  • task.change.parent
    • userName string - 操作人姓名
    • taskContent string - 任务标题
  • task.update.involveMembers
    • userName string - 操作人姓名
    • taskContent string - 任务标题
  • task.update.executor
    • userName string - 操作人姓名
    • taskContent string - 任务标题
    • excutor string - 任务执行者
  • task.done
    • userName string - 操作人姓名
    • type string - 操作类型:完成,重做
    • taskContent string - 任务标题
  • task.on.reminder
    • userName string - 操作人姓名,默认为Teambition
  • file.update.version
    • userName string - 操作人姓名
    • fileName string - 文件名称
  • file.rename
    • userName string - 操作人姓名
    • fileName string - 文件名称
  • file.move
    • userName string - 操作人姓名
    • fileName string - 文件名称
    • projectName string - 项目名称
    • collectionTitle string - 目录名称
  • file.remove
    • userName string - 操作人姓名
    • fileName string - 文件名称
  • post.update
    • userName string - 操作人姓名
    • postTitle string - 分享标题
  • post.archive
    • userName string - 操作人姓名
    • postTitle string - 分享标题
  • object.comment
    • userName string - 操作人姓名
    • comment string - 评论内容
  • event.create
    • userName string - 操作人姓名
    • eventTitle string - 日程标题
  • event.update
    • userName string - 操作人姓名
    • eventTitle string - 日程标题
    • type string - 修改类型:开始时间为,结束时间为
    • time string - 时间:开始时间或结束时间
  • event.on.reminder
    • time string - 距离日程开始剩余时间,单位:分钟
模版示例
{content}
>**任务详情**
>任务标题:<font color=\"comment\">{taskContent}</font>
>执行者:<font color=\"comment\">{executor}</font>
>项目名称:<font color=\"comment\">{projectName}</font>
>
>[查看详情]({url})`
格式化结果示例

张三 创建了任务 测试任务

任务详情\ 任务标题:测试任务\ 执行者:未指派\ 项目名称:测试项目\ \ 查看详情

config默认配置

{
  msgType: 'markdown', 
  markdownTemplate: {
    'task': `{content}\n>**任务详情**\n>任务标题:<font color=\"comment\">{taskContent}</font>\n>执行者:<font color=\"comment\">{executor}</font>\n>项目名称:<font color=\"comment\">{projectName}</font>\n>\n>[查看详情]({url})`,
    'event': `{content}\n>**任务详情**\n>日程标题:<font color=\"comment\">{eventTitle}</font>\n>创建者:<font color=\"comment\">{creator}</font>\n>项目名称:<font color=\"comment\">{projectName}</font>\n>\n>[查看详情]({url})`
  },
  contentTemplate: {
    'project.rename': '{userName} 更新了 {oldProjectName} 的项目名称为 {newProjectName}',
    'project.suspend': '{userName} 将 {projectName} 归档',
    'project.archive': '{userName} 将 {projectName} 删除',
    'project.remove': '{userName} 将 {projectName} 彻底删除',
    'project.unsuspend': '{userName} 将 {projectName} 恢复',
    'project.update.customfields': '{userName} 更新了 {projectName} 的项目信息',
    'project.update.statusDegree': '{userName} 更新了 {projectName} 的项目状态',
    'project.create.customfieldlinks': '{userName} 创建了 {projectName} 的新的项目信息字段',
    'project.remove.customfieldlinks': '{userName} 创建了 {projectName} 的新的项目信息字段',
    'task.create': '{userName} 创建了任务 {taskContent}',
    'task.rename': '{userName} 更新了任务 {oldContent} 标题为 {newContent}',
    'task.update.startDate': '{userName} 更新了任务 {taskContent} 任务开始时间为 {}',
    'task.update.dueDate': '{userName} 更新了任务 {taskContent} 任务截止时间为 {}',
    'task.update.customfields': '{userName} 更新了任务 {taskContent} 自定义字段',
    'task.move': '{userName} 移动了任务 {taskContent}',
    'task.change.parent': '{userName} 将任务 {taskContent} 转化为了子任务',
    'task.update.involveMembers': '{userName} 在任务 {taskContent} 添加您为参与者',
    'task.update.executor': '{userName} 指派任务 {taskContent} 执行者为  {}',
    'task.done': '{userName} {type}了任务 {taskContent}',
    'task.on.reminder': '{userName} 提醒您查看任务',
    'file.update.version': '{userName} 更新了 {fileName} 的文件版本',
    'file.rename': '{userName} 更新了 {fileName} 的文件名称',
    'file.move': '{userName} 移动了 {fileName} 的文件到 {projectName}-{collectionTitle}',
    'file.remove': '{userName} 彻底删除了 {fileName}',
    'post.update': '{userName} 更新了分享 {postTitle}',
    'post.archive': '{userName} 删除了分享 {postTitle}',
    'object.comment': '{userName} 添加了评论:{comment}',
    'event.create': '{userName} 创建了日程 {eventTitle}',
    'event.update': '{userName} 更新了日程 {eventTitle} {type} {time}',
    'event.on.reminder': '该日程将于 {time} 后开始'
  }
}

发送消息推送

Notify.prototype.send(url, data, headers)

  • url string - 必填 - 请求地址
  • data object - 非必填 - 请求参数,默认值为{}
  • method string - 非必填 - 请求方法,默认为POST请求
  • headers object - 非必填 - 自定义请求头,默认值为{}

获取格式化的推送消息正文

Notify.prototype.formatMsg(hookData, msgType)

  • hookData object - 必填 - webhook推送的消息体
  • msgType string - 非必填 - 若未制定该参数则查询实例初始化中config.msgType,若初始化时未指定,默认未markdown
    • markdown 返回markdown类型的推送消息正文

该方法目前只支持markdown格式