最近又在闲鱼入手了一台低功耗的mini主机(一天的功耗只有0.2W)部署docker,然后在里面部署了青龙面板执行一些定时的脚本任务。然后看到青龙面板里面有通知设置,里面选项有好多,最后选了server酱,了解之后发现使用挺方便。由于免费用户一天只有5条的额度,而且我本身已经对接过很多钉钉的接口(平常工作用.NET开发,但是钉钉自带调试案例,PHP开发业不难),于是就有了下面的代码了。
实现原理:服务器或者智能设备调用此PHP接口,然后调用钉钉的工作通知接口推送消息到钉钉客户端。
准备工作:
1、钉钉注册一个团队,然后在开发者后台(https://open-dev.dingtalk.com/fe/app#/corp/app)创建一个H5应用(复制AgentID、AppKey、APPSecret)
2、下载钉钉SDK(https://open.dingtalk.com/document/resourcedownload/download-server-sdk),解压后放到同一目录
3、一台服务器(环境最好是nginx+php)
PHP代码如下:
- <?php
- header('Content-Type:application/json; charset=utf-8');
- //ini_set("display_errors","On"); //显示报错信息
- //error_reporting(E_ALL);//显示所有
- require "Dingtalk/TopSdk.php";
- date_default_timezone_set('Asia/Shanghai');
- try {
- //unicode解码
- function decodeUnicode($str) {
- return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
- create_function(
- '$matches',
- 'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
- ), $str);
- }
- //秘钥
- $key = $_REQUEST['key'];
- //标题
- $title = $_REQUEST['title'];
- //内容
- $desc = $_REQUEST['desc'];
- $rst = array("code" => 1, "msg" => "", "success" => "N", "data" => "");
- if ($key != null && $key == "自己设置的密钥") {
- if ($desc != null && $desc != "") {
- $c = new DingTalkClient(DingTalkConstant::$CALL_TYPE_OAPI, DingTalkConstant::$METHOD_GET, DingTalkConstant::$FORMAT_JSON);
- $req_token = new OapiGettokenRequest;
- $req_token -> setAppkey("钉钉应用Appkey");
- $req_token -> setAppsecret("钉钉应用Appsecret");
- $resp_token = $c -> execute($req_token, $access_token, "https://oapi.dingtalk.com/gettoken");
- $errcode = $resp_token -> errcode;
- $token_msg = $resp_token -> errmsg;
- if ($errcode == 0) {
- $access_token = $resp_token -> access_token;
- $time_str = date("Y-m-d H:i:s", time());
- $req = new OapiMessageCorpconversationAsyncsendV2Request;
- $req -> setAgentId("钉钉AgentId");
- $req -> setUseridList("钉钉用户id");
- $msg = new Msg;
- $msg -> msgtype = "markdown";
- $markdown = new Markdown;
- if ($title == null || $title == "") {
- $title = "新消息";
- }
- $desc= str_replace("\\n"," \n",$desc);
- $markdown -> text = "### ".$title." \n*************\n".$desc." \n*************\n".$time_str;
- $markdown -> title = $title;
- $msg -> markdown = $markdown;
- $req -> setMsg($msg);
- $resp = $c -> execute($req, $access_token, "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
- $errcode = $resp -> errcode;
- $errmsg = $resp -> errmsg;
- if ($errcode == 0) {
- $rst = array("code" => 0, "msg" => "发送成功!", "success" => "Y", "data" => $errmsg);
- } else {
- $rst = array("code" => 1, "msg" => "推送消息出错!", "success" => "N", "data" => $errmsg);
- }
- } else {
- $rst = array("code" => 1, "msg" => "获取钉钉token出错!", "success" => "N", "data" => $errmsg);
- }
- } else {
- $rst = array("code" => 1, "msg" => "参数:内容(desc)不能为空!", "success" => "N", "data" => "");
- }
- } else {
- if($key==null||$key=="") {
- $rst = array("code" => 1, "msg" => "实例:http://push.risingsun.cc?key=密钥&title=标题&desc=内容;PS:标题可为空;换行符\\n;换行分割线\\n*************\\n", "success" => "N", "data" => "");
- } else {
- $rst = array("code" => 1, "msg" => "参数:密钥(key)有误!", "success" => "N", "data" => "");
- }
- }
- echo decodeUnicode(json_encode($rst));
- }
- catch (Exception $e) {
- $rst = array("code" => 1, "msg" => $e->getMessage(), "success" => "N", "data" => "异常");
- echo decodeUnicode(json_encode($rst));
- }
因为考虑到跨域使用的问题,这里就顺便开启了nginx的跨域
在nginx对应的vhost配置文件“server”里面增加:
- #允许跨域请求的域,*代表所有
- add_header 'Access-Control-Allow-Origin' *;
- #允许带上cookie请求
- add_header 'Access-Control-Allow-Credentials' 'true';
- #允许请求的方法,比如 GET/POST/PUT/DELETE
- add_header 'Access-Control-Allow-Methods' *;
- #允许请求的header
- add_header 'Access-Control-Allow-Headers' *;
然后重启nginx即可
调用推送的实例:http://push.risingsun.cc?key=密钥&title=标题&desc=内容
推送到钉钉客户端(PC/手机都能收到)就是这样的: