跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • CF版Server酱,by企业微信通道
未分類
14 2 月 2021

CF版Server酱,by企业微信通道

arron 大佬有话说 :

CF版Server酱,by企业微信通道

本帖最后由 arron 于 2021-2-13 17:16 编辑

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

文章首发在我的博客上(盼望Ip+1):https://blog.arrontg.cf/article/000007/.html

注册企业微信
第一步呢要先注册企业微信,我们直接看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/read
const 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)
});
}

dforel 大佬有话说 :

不错,感谢分享。可能某一天可以用上

Nothing1024 大佬有话说 :

感谢老哥分享!一会试试https://cdn.jsdelivr.net/gh/hishis/forum-master/public/images/patch.gif

jqbaobao 大佬有话说 :

第一步呢要先注册企业微信

silence 大佬有话说 :

感谢大佬,这东西拿来插在签到程序里很好用

kenshuren 大佬有话说 :

感谢分享

logo 大佬有话说 :

mark zsbd

ktaog6 大佬有话说 :

出门打野,路过插眼

zhongziso 大佬有话说 :

mark

b66667777 大佬有话说 :

感谢分享

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具