mirror of
https://github.com/geekgeekrun/geekgeekrun.git
synced 2026-09-07 08:27:13 +08:00
set send dingtalk message with delay
This commit is contained in:
+2
-2
@@ -258,11 +258,11 @@ async function mainLoop (hooks) {
|
|||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
case 'STARTUP_CHAT_ERROR_WITH_UNKNOWN_ERROR': {
|
case 'STARTUP_CHAT_ERROR_WITH_UNKNOWN_ERROR': {
|
||||||
hooks.errorEncounter.call(err)
|
hooks.errorEncounter.call([err.message, err.stack].join('\n'))
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
hooks.errorEncounter.call(err)
|
hooks.errorEncounter.call([err.message, err.stack].join('\n'))
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
export function createTextMessage (text) {
|
export function createTextMessage (text) {
|
||||||
return JSON.stringify({
|
return {
|
||||||
"msgtype": "text",
|
"dingtalkRequestBody": {
|
||||||
"text": {
|
"msgtype": "text",
|
||||||
"content": `${text}【bossgeekgo】`
|
"text": {
|
||||||
}
|
"content": `${text}`
|
||||||
})
|
},
|
||||||
|
},
|
||||||
|
insertedTime: new Date(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function requestDingTalkNotify (dingTalkAccessToken, body) {
|
export async function requestDingtalkNotify (dingTalkAccessToken, body) {
|
||||||
const url = new URL(`https://oapi.dingtalk.com/robot/send`)
|
const url = new URL(`https://oapi.dingtalk.com/robot/send`)
|
||||||
url.searchParams.append(
|
url.searchParams.append(
|
||||||
'access_token',
|
'access_token',
|
||||||
|
|||||||
@@ -1,19 +1,60 @@
|
|||||||
import { requestDingTalkNotify, createTextMessage } from './dingtalk.mjs'
|
import { requestDingtalkNotify, createTextMessage } from './dingtalk.mjs'
|
||||||
|
import dayjs from 'dayjs'
|
||||||
|
|
||||||
|
let sendQueueTimer = 0
|
||||||
|
let collectedMessageList = []
|
||||||
|
|
||||||
export default class DingtalkPlugin {
|
export default class DingtalkPlugin {
|
||||||
constructor (dingtalkAccessToken) {
|
constructor (dingtalkAccessToken) {
|
||||||
this.dingtalkAccessToken = dingtalkAccessToken
|
this.dingtalkAccessToken = dingtalkAccessToken
|
||||||
}
|
}
|
||||||
|
setSendQueueTimer () {
|
||||||
|
const _this = this
|
||||||
|
const interval = 2 * 60 * 1000
|
||||||
|
sendQueueTimer = setTimeout(function sendMergedMessage () {
|
||||||
|
if (collectedMessageList.length === 0) {
|
||||||
|
} else if (collectedMessageList.length === 1) {
|
||||||
|
collectedMessageList[0].dingtalkRequestBody.text.content += `\n${dayjs(collectedMessageList[0].insertedTime).format('MM-DD HH:mm:ss')}\n\n【bossgeekgo】`
|
||||||
|
requestDingtalkNotify(
|
||||||
|
_this.dingtalkAccessToken, JSON.stringify(collectedMessageList[0].dingtalkRequestBody)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
requestDingtalkNotify(
|
||||||
|
_this.dingtalkAccessToken, JSON.stringify((createTextMessage(
|
||||||
|
collectedMessageList.map(it => {
|
||||||
|
return `${it.dingtalkRequestBody.text.content}\n${dayjs(it.insertedTime).format('MM-DD HH:mm:ss')}\n`
|
||||||
|
}).join('-----\n') + '\n【bossgeekgo】'
|
||||||
|
)).dingtalkRequestBody)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
collectedMessageList.length = 0
|
||||||
|
sendQueueTimer = setTimeout(sendMergedMessage, interval)
|
||||||
|
}, interval)
|
||||||
|
}
|
||||||
|
destroySendQueueTimer () {
|
||||||
|
clearTimeout(sendQueueTimer)
|
||||||
|
}
|
||||||
apply (hooks) {
|
apply (hooks) {
|
||||||
if (!this.dingtalkAccessToken) {
|
if (!this.dingtalkAccessToken) {
|
||||||
console.log(`[DingtalkPlugin] AccessToken is empty, which makes the plugin won't do anything.`)
|
console.log(`[DingtalkPlugin] AccessToken is empty, which makes the plugin won't do anything.`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
this.destroySendQueueTimer()
|
||||||
|
this.setSendQueueTimer()
|
||||||
hooks.errorEncounter.tap(
|
hooks.errorEncounter.tap(
|
||||||
'DingtalkPlugin',
|
'DingtalkPlugin',
|
||||||
(errorInfo) => {
|
(errorInfo) => {
|
||||||
requestDingTalkNotify(
|
collectedMessageList.push(createTextMessage(errorInfo))
|
||||||
this.dingtalkAccessToken, createTextMessage(errorInfo)
|
}
|
||||||
)
|
)
|
||||||
|
hooks.newChatStartup.tap(
|
||||||
|
'DingtalkPlugin',
|
||||||
|
({jobInfo, bossInfo}) => {
|
||||||
|
collectedMessageList.push(createTextMessage(
|
||||||
|
`${bossInfo.brandName} ${bossInfo.name}
|
||||||
|
${jobInfo.jobName} ${jobInfo.salaryDesc}
|
||||||
|
Chat has startup!`
|
||||||
|
))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user