Koa路由中写一个钩子函数,用于萤石云消息触发钉钉机器人webhook
//钉钉机器人的地址
let webhook = 'https://oapi.dingtalk.com/robot/send?access_token=4dd5cd7xxxxxxxxxxxxxxxxxxxxxxxxxxx'
let whiteList = ['xxxx'] //摄像头序列号白名单,只有在白名单里的才促发报警消息,用于过滤
//获取webhook消息并转发到钉钉机器人
async function sendToDingtalk (ctx,next) {
await next()
ctx.request.body = JSON.parse(ctx.request.body)
console.log('debugger',ctx.request.body.header)
console.log('debugger',ctx.request.body)
let {deviceId, messageTime, channelNo, type, messageId} = ctx.request.body.header
let {channelName, alarmTime, pictureList } = ctx.request.body.body
console.log('萤石云消息ID:',messageId)
let data = null
if(type=='ys.alarm' && whiteList.includes(deviceId) ){
data = {
"msgtype": "markdown",
"markdown": {
"title": `【告警消息】${channelName}`,
"text": `### 设备名称:${channelName} \n #### 设备序列号:${deviceId} \n #### 通道:${channelNo} \n #### 消息类型:${type} \n #### 时间:${alarmTime} \n ![](${pictureList[0].url})`
}
}
}else if(type=='ys.onoffline'){
data = {
"msgtype": "markdown",
"markdown": {
"title": `【上下线消息】${channelName}`,
"text": `### 设备名称:${channelName} \n #### 设备序列号:${deviceId} \n #### 通道:${channelNo} \n #### 消息类型:${type} \n #### 时间:${alarmTime}`
}
}
}
// 判断消息类型,然后发送到钉钉webhook
if(data != null){
axios.post(webhook,
data
).then(res=>{
console.log('钉钉调用成功')
console.log(res.data)
}).catch(err=>{
console.error(err)
})
}
ctx.body={
messageId
}
}