自建Server酱 PHP版/CF Worker版

不下载APP,家长在微信上如何使用「学校通知」? 人脸识别门禁考勤消费,平安校园,数字化校园,慧校园系统建设_和谐号教育,和谐号教育,企业微信教育版

在Hostloc上看了mjj的帖子-[经验] MJJ专用server酱 企业微信通道PHP版 ,感觉整一个自己的消息推送的服务也不错,不过对于喜欢折(白)腾(嫖)的我,还是希望使用cf的workers来做。

缘由

相信大家都用过或者听说过server酱
因为微信发布公告将在4月底下线模板消息,Server酱开发了以企业微信为主的多通道新版。旧版将在4月后下线。

这个企业微信是随便都可以注册的,且稍微操作后微信app端也可以收到推送。
而且server酱的服务是有一些限制的,那我们就不如自己整一个推送通道。没有每分钟最多1条,免费版每天5条的限制。

注册企业微信

第一步呢要先注册企业微信,我们直接看server酱的教程,记下教程里的应用ID( agentid ),应用Secret( secret ),企业ID。
原贴大佬把教程复制到石墨了,server酱的还需要登录才能看。https://shimo.im/docs/38dpjtwWtRRVQ6Wy

在CF上创建workers

这里只提供了卡片消息一种,有需要的可以参考企业微信的api文档

访问时带上参数就行啦,
简单点:http://xxx.workers.dev/?msg=xxx
复杂点:http://xxx.workers.dev/?title=xxx&description=xxx&url=xxx

参数

  • corpid //企业id
  • corpsecret //应用secret
  • agentid  //应用id
  • title //消息title,默认“Server酱通知”
  • description //消息内容,默认“通知内容”
  • url //消息跳转url,默认“URL”

企业微信的api文档:https://work.weixin.qq.com/api/doc/90000/90135/90236

访问时带上参数就行啦,get post都可以。
如 http://example.com/server.php?corpid=xxx&corpsecret=xxx&agentid=xxx&title=title&description=desc&url=url 

代码部分

PHP版

<?php

$corpid = $_REQUEST['corpid']; //企业id
$corpsecret = $_REQUEST['corpsecret']; //应用secret
$agentid = $_REQUEST['agentid']; //应用id

//如果就自己用,可以把参数写到这里。
//$corpid = '';
//$corpsecret = '';
//$agentid = ;


$title = $_REQUEST['title']; //消息title
$description = $_REQUEST['description']; //消息内容
$description = str_replace(PHP_EOL, '<br>', $description);
$url = $_REQUEST['url']; //消息跳转url

if(!$corpid or !$corpsecret or !$agentid){
    exit("canshu buquan");
}


//获取access_token
$response = CurlGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpid&corpsecret=$corpsecret","","");

$access_token = json_decode($response)->access_token;
if(!$access_token){
    exit("canshu cuowu");
}else{

    $json = '{"touser":"@all","msgtype":"textcard","agentid":"","textcard":{"title":"","description":"","url":"","btntxt":"更多"},"safe":1,"enable_id_trans":0,"enable_duplicate_check":0}';
    $json = json_decode($json);

    $json->agentid = $agentid;
    $json->textcard->title = $title ? $title : '无标题';
    $json->textcard->description = $description ? $description : '无内容';
    
    $json->textcard->url = $url ? $url : 'URL';

    echo CurlPost("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$access_token","", json_encode($json));
}





function CurlGet($url,$cookies = "",$UserAgent = "")
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); 
    curl_setopt($curl, CURLOPT_URL, $url);     
        curl_setopt($curl, CURLOPT_REFERER, '');
        curl_setopt($curl, CURLOPT_COOKIE, $cookies);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    if ($UserAgent != "") {
        curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
    }
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

function CurlPost($url, $cookies="", $post_data="", $headers=array(), $refer="", $UserAgent = '')
{

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 
        curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); 
    curl_setopt($curl, CURLOPT_URL, $url);     
    curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent); 
        curl_setopt($curl, CURLOPT_COOKIE, $cookies);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    if ($refer != '') {
        curl_setopt($curl, CURLOPT_REFERER, $refer);
    }
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

 

Cloudflare Worker版

//教程: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)
  });
}

 

本站文章资源均来源自网络,除非特别声明,否则均不代表站方观点,并仅供查阅,不作为任何参考依据!
如有侵权请及时跟我们联系,本站将及时删除!
如遇版权问题,请查看 本站版权声明
THE END
分享
二维码
海报
自建Server酱 PHP版/CF Worker版
在Hostloc上看了mjj的帖子-[经验] MJJ专用server酱 企业微信通道PHP版 ,感觉整一个自己的消息推送的服务也不错,不过对于喜欢折(白)腾(嫖)的我,还是希……
<<上一篇
下一篇>>
文章目录
关闭
目 录