mirror of
https://github.com/geekgeekrun/geekgeekrun.git
synced 2026-09-05 23:47:54 +08:00
refactor structure to monorepo
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { requestDingtalkNotify, createTextMessage } from './service.mjs'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
let sendQueueTimer = 0
|
||||
let collectedMessageList = []
|
||||
|
||||
export default class DingtalkPlugin {
|
||||
constructor (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) {
|
||||
if (!this.dingtalkAccessToken) {
|
||||
console.log(`[DingtalkPlugin] AccessToken is empty, which makes the plugin won't do anything.`)
|
||||
return
|
||||
}
|
||||
this.destroySendQueueTimer()
|
||||
this.setSendQueueTimer()
|
||||
hooks.errorEncounter.tap(
|
||||
'DingtalkPlugin',
|
||||
(errorInfo) => {
|
||||
collectedMessageList.push(createTextMessage(errorInfo))
|
||||
}
|
||||
)
|
||||
hooks.newChatStartup.tap(
|
||||
'DingtalkPlugin',
|
||||
({jobInfo, bossInfo}) => {
|
||||
collectedMessageList.push(createTextMessage(
|
||||
`${bossInfo.brandName} ${bossInfo.name}
|
||||
${jobInfo.jobName} ${jobInfo.salaryDesc}
|
||||
Chat has startup!`
|
||||
))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "@bossgeekgo/dingtalk-plugin",
|
||||
"private": true,
|
||||
"version": "1.0.0",
|
||||
"description": "@bossgeekgo/dingtalk-plugin",
|
||||
"module": "./index.mjs",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
},
|
||||
"author": "bossgeekgo",
|
||||
"license": "ISC",
|
||||
"dependencies": {}
|
||||
}
|
||||
Generated
+1108
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,30 @@
|
||||
export function createTextMessage (text) {
|
||||
return {
|
||||
"dingtalkRequestBody": {
|
||||
"msgtype": "text",
|
||||
"text": {
|
||||
"content": `${text}`
|
||||
},
|
||||
},
|
||||
insertedTime: new Date(),
|
||||
}
|
||||
}
|
||||
|
||||
export async function requestDingtalkNotify (dingTalkAccessToken, body) {
|
||||
const url = new URL(`https://oapi.dingtalk.com/robot/send`)
|
||||
url.searchParams.append(
|
||||
'access_token',
|
||||
dingTalkAccessToken
|
||||
)
|
||||
|
||||
return await fetch(
|
||||
url,
|
||||
{
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user