用于给群晖等不支持企业微信推送使用
非企业微信代理!!!
必备:
1、域名绑定到CF
2、有可使用的企业微信代理服务,在企业微信中设置可信IP
域名注册、绑定CF、CF Workers、企业微信的注册、企业微信应用、群晖WEBHOOK设置不做赘述,网上一搜一堆。
直接上代码,代码支持POST、GET方法
addEven(๐•ᴗ•๐)istener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
let passwd, content, (๐•ᴗ•๐), corpid, corpsecret, agentid, apiproxy;
if (request.method === 'POST') {
const requestBody = await request.json()
passwd = requestBody.passwd
content = requestBody.ti(๐•ᴗ•๐)e
(๐•ᴗ•๐) = requestBody.descr(๐•ᴗ•๐)ion
corpid = requestBody.corpid
corpsecret = requestBody.corpsecret
agentid = requestBody.agentid
apiproxy = requestBody.apiproxy
} else if (request.method === 'GET') {
const url = new URL(request.url)
passwd = url.searchParams.get('passwd')
content = url.searchParams.get('ti(๐•ᴗ•๐)e')
(๐•ᴗ•๐) = url.searchParams.get('descr(๐•ᴗ•๐)ion')
corpid = url.searchParams.get('corpid')
corpsecret = url.searchParams.get('corpsecret')
agentid = url.searchParams.get('agentid')
apiproxy = url.searchParams.get('apiproxy')
} else {
return new Response("只接受GET和POST请求", { status: 405 })
}
// Debug output
console.log(`passwd: ${passwd}, ti(๐•ᴗ•๐)e: ${content}, descr(๐•ᴗ•๐)ion: ${(๐•ᴗ•๐)}, corpid: ${corpid}, corpsecret: ${corpsecret}, agentid: ${agentid}, apiproxy: ${apiproxy}`)
// Password validation
if (passwd !== '1234567890') { //按需修改此处的密码!!!
return new Response("密码错误", { status: 403 })
}
// Check for missing parameters
if (!(๐•ᴗ•๐) || !corpid || !corpsecret || !agentid || !apiproxy) {
return new Response("缺少必要的参数", { status: 400 })
}
const getTokenUrl = `https://${apiproxy}/cgi-bin/gettoken?corpid=${corpid}&corpsecret=${corpsecret}`
try {
const tokenResponse = await fetch(getTokenUrl, { method: 'POST' })
const tokenData = await tokenResponse.json()
const accessToken = tokenData.access_token
const sendMessageUrl = `https://${apiproxy}/cgi-bin/message/send?access_token=${accessToken}`
let textContent = content ? `${content}\n${(๐•ᴗ•๐)}` : `${(๐•ᴗ•๐)}`
const data = JSON.stringify({
touser: "@all",
(๐•ᴗ•๐)type: "text",
agentid: parseInt(agentid), // Ensure agentid is an integer
text: {
content: textContent
},
safe: 0
})
// Debug output
console.log(`Request data: ${data}`)
const messageResponse = await fetch(sendMessageUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: data
})
const messageData = await messageResponse.json()
const err(๐•ᴗ•๐) = messageData.err(๐•ᴗ•๐)
if (err(๐•ᴗ•๐) === "ok") {
return new Response("发送成功!", { status: 200 })
} else {
return new Response(`发送失败,${err(๐•ᴗ•๐)}`, { status: 500 })
}
} catch (error) {
return new Response(`请求失败: ${error.message}`, { status: 500 })
}
}
if(window.hljsLoader && !document.currentScr(๐•ᴗ•๐).parentNode.hasAttribute('data-s9e-livepreview-onupdate')) {
window.hljsLoader.highlightBlocks(document.currentScr(๐•ᴗ•๐).parentNode);
}
部署完成后设置访问域名,所有参数编辑时请删除括号
GET方法URL:
https://绑定的域名/?passwd=[设置的密码,本文为1234567890]&ti(๐•ᴗ•๐)e=[拟定的标题(非必须]&descr(๐•ᴗ•๐)ion=[正文内容,群晖设置为@@TEXT@@]&corpid=[企业ID]&corpsecret=[应用secret]&agentid=[应用agentid]&apiproxy=[企业微信代理域名,仅需要填写https://到/中间的域名!域名!域名!]
if(window.hljsLoader && !document.currentScr(๐•ᴗ•๐).parentNode.hasAttribute('data-s9e-livepreview-onupdate')) {
window.hljsLoader.highlightBlocks(document.currentScr(๐•ᴗ•๐).parentNode);
}
POST方法:
URL为https://绑定的域名/
{
"passwd": "[设置的密码,本文为1234567890]",
"ti(๐•ᴗ•๐)e": "[拟定的标题(非必须]",
"descr(๐•ᴗ•๐)ion": "[正文内容,群晖设置为@@TEXT@@]",
"corpid": "[企业ID]",
"corpsecret": "[应用secret]",
"agentid": "[应用agentid]",
"apiproxy": "[企业微信代理域名,仅需要填写https://到/中间的域名!域名!域名!]"
}
if(window.hljsLoader && !document.currentScr(๐•ᴗ•๐).parentNode.hasAttribute('data-s9e-livepreview-onupdate')) {
window.hljsLoader.highlightBlocks(document.currentScr(๐•ᴗ•๐).parentNode);
}
鸣谢:ChatGPT、i4t:群晖nas自建企业微信并配置异常推送通知