#教程#cloudflare workers/CF版Server酱,by企业微信通道
主机社 1月前

在Hostloc上看了mjj的帖子- MJJ专用server酱 企业微信通道PHP版(https://www.hostloc.com/thread-806295-1-1.html),感觉整一个自己的消息推送的服务也不错,不过对于喜欢折(白)腾(票)的我,还是希望使用cfworkers来做。

注册企业微信

第一步呢要先注册企业微信,我们直接看server酱的教程,记下教程里的应用ID( agentid ),应用Secret( secret ),企业ID。

原贴大佬把教程复制到石墨了,server酱的还需要登录才能看:https://shimo.im/docs/38dpjtwWtRRVQ6Wy

在CF上创建workers

这里只提供了卡片消息一种,有需要的可以参考企业微信的api文档:https://work.weixin.qq.com/api/doc/90000/90135/90236

访问时带上参数就行啦,

简单点:http://xxx.workers.dev/?msg=xxx

复杂点:http://xxx.workers.dev/?title=xxx&description=xxx&url=xxx

其中

title : 消息title,默认“Server酱通知”

description : 消息内容,默认“通知内容”

url : 跳转地址,默认“URL”

workers的代码如下:

//教程:https://shimo.im/docs/38dpjtwWtRRVQ6Wy/readconst OPT = {  corpid : '',//企业id  agentid:'',//应用id  corpsecret:'', //应用secret  access_token:undefined}addEventListener('fetch', event => {  event.respondWith(handleRequest(event.request))})/*** Respond to the request* @param {Request} request*/async function handleRequest(request) {  let url = new URL(request.url);  //消息title  let title = url.searchParams.get('title')||"Server酱通知";  //消息内容  let description = url.searchParams.get('description')||url.searchParams.get('msg')||"通知内容";  //跳转地址  let jumpUrl = url.searchParams.get('url')||"URL";  //获取access_token,写入OPT中  let msg = await getAccessToken();  if(msg.errcode){    return new Response(JSON.stringify(msg), {      status: 200,      headers:{        'content-type':'application/json; charset=UTF-8'      }    })  }  //发送消息  return await pushMsg(title, description, jumpUrl);}//获取access_token,写入OPT中async function getAccessToken(){  let result = await fetch("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+OPT.corpid+"&corpsecret="+OPT.corpsecret);  let json = await result.json();  console.log(json)  if(json.errcode==0){    OPT.access_token = json.access_token  }  return json}//发送消息async function pushMsg(title, description,url="URL",btntxt="更多"){  let body = {    /**     * 指定接收消息的成员,成员ID列表(多个接收者用‘|’分隔,最多支持1000个)。     * 特殊情况:指定为”@all”,则向该企业应用的全部成员发送     */    "touser" : "@all",//非必须,但touser、toparty、totag不能同时为空,后面不再强调。    /**     * 指定接收消息的部门,部门ID列表,多个接收者用‘|’分隔,最多支持100个。     * 当touser为”@all”时忽略本参数     */    //"toparty" : "PartyID1|PartyID2",//非必须    /**     * 指定接收消息的标签,标签ID列表,多个接收者用‘|’分隔,最多支持100个。     * 当touser为”@all”时忽略本参数     */    //"totag" : "TagID1 | TagID2",//非必须    "msgtype" : "textcard",//必须,消息类型,此时固定为:textcard    "agentid" : OPT.agentid,//企业应用的id,整型。企业内部开发,可在应用的设置页面查看;第三方服务商,可通过接口 获取企业授权信息 获取该参数值    "textcard" : {      "title" : title,//必须,标题,不超过128个字节,超过会自动截断(支持id转译)      "description" : description, //必须,消息内容,最长不超过2048个字节,超过将截断(支持id转译)      "url": url,//点击后跳转的链接。最长2048字节,请确保包含了协议头(http/https)      "btntxt":btntxt //非必须,按钮文字。 默认为“详情”, 不超过4个文字,超过自动截断。    },    "safe":0, //非必须, 表示是否是保密消息,0表示可对外分享,1表示不能分享且内容显示水印,默认为0    "enable_id_trans": 0,//非必须,表示是否开启id转译,0表示否,1表示是,默认0。仅第三方应用需要用到,企业自建应用可以忽略。    "enable_duplicate_check": 0,//非必须,表示是否开启重复消息检查,0表示否,1表示是,默认0    "duplicate_check_interval": 1800 //非必须,表示是否重复消息检查的时间间隔,默认1800s(3小时),最大不超过4小时  }  return fetch("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+OPT.access_token,{    method:'post',    body: JSON.stringify(body)  });}

 

作者:arron

支持作者:blog.arrontg.cf

最新回复 (0)
返回
网站标签