2021-09-07 18:35:21 星期二
今天看到了宝塔里可以设置钉钉的登录提醒的通知,我就在想我是不是可以做自己的接口呢,于是我去看了官方的开发文档,挺简单的,只需要在添加机器人,然后拿到机器人的Webhook,就可以了,对了记得配置好安全设置。

参考文档: 钉钉开放平台

下边是我写的代码:
这个是CRUL请求代码:

//公共代码——curl请求
function request_by_curl($remote_server, $post_string) {  
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $remote_server);
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
    // curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    // curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $data = curl_exec($ch);
    curl_close($ch);                
    return $data;  
}

下边是一个例子(我写的提现):

function index(){
    
    $_var_666 = input();
    $webhook = "这里是webhook地址";
      // 举例:markdown类型
    $message = json_encode(["msgtype" => "markdown","markdown" => [
        "title" => "乐檬易支付:",
        "text" => "#### 乐檬易支付\n" .
            ">商户号:".$_var_666['uid']."\n\n" .
            ">用户姓名:".$_var_666['name']."\n\n" .
            ">提现金额:".$_var_666['money']."\n\n" .
            ">实际到账金额:".$_var_666['realmoney']."\n\n" .
            ">提现方式:支付宝\n\n" .
            ">收款账户:".$_var_666['account']."\n\n" .
            "#### 请及时处理\n" 
    ],
    "at" => [
        "atMobiles" => [
            "这里是需要@手机号1",
            "这里是需要@手机号2"
        ],
        "isAtAll" => false
    ]
]);
$data_string = json_encode($data);
$result = $this -> request_by_curl($webhook, $message);  
echo $result;
}

全部的推送类型都给放下边了,有需要的自己去组合使用就可以了


// text类型
$textString = json_encode([
    'msgtype' => 'text',
    'text' => [
        "content" => "我就是田小檬, 是不一样的烟火!"
    ],
    'at' => [
        'atMobiles' => [
            "156xxxx8827",
            "189xxxx8325"
        ],
        'isAtAll' => false

    ]
]);

// link类型
$textString = json_encode([
    "msgtype" => "link",
    "link" => [
        "text" => "这个即将发布的新版本,创始人田小檬(花名“无招”)称它为“红树林”。
而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是“红树林”?",
        "title" => "时代的火车向前开",
        "picUrl" => "",
        "messageUrl" => "https://www.dingtalk.com/",
    ]
]);

// markdown类型
$textString = json_encode([
    "msgtype" => "markdown",
    "markdown" => [
        "title" => "济南天气",
        "text" => "#### 济南天气 @156xxxxxxxx\n" .
            "> 9度,西北风1级,空气良89,相对温度73%\n\n" .
            "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n" .
            "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n"
    ],
    "at" => [
        "atMobiles" => [
            "175xxxx57037",
            "167xxxx5217"
        ],
        "isAtAll" => false
    ]
]);

// 整体跳转ActionCard类型
$textString = json_encode([
    "actionCard" => [
        "title" => "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
        "text" => "![screenshot](@lADOpwk3K80C0M0FoA) 
 ### 乔布斯 20 年前想打造的苹果咖啡厅 
 Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
        "hideAvatar" => "0",
        "btnOrientation" => "0",
        "singleTitle" => "阅读全文",
        "singleURL" => "https://www.dingtalk.com/"
    ],
    "msgtype" => "actionCard"
]);


// 独立跳转ActionCard类型
$textString = json_encode([
    "actionCard" => [
        "title" => "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
        "text" => "![screenshot](@lADOpwk3K80C0M0FoA) 
 ### 乔布斯 20 年前想打造的苹果咖啡厅 
 Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
        "hideAvatar" => "0",
        "btnOrientation" => "0",
        "btns" => [
            [
                "title" => "内容不错",
                "actionURL" => "https://www.dingtalk.com/"
            ],
            [
                "title" => "不感兴趣",
                "actionURL" => "https://www.dingtalk.com/"
            ]
        ]
    ],
    "msgtype" => "actionCard"
]);

// FeedCard类型
$textString = json_encode([
    "feedCard" => [
        "links" => [
            [
                "title" => "时代的火车向前开1",
                "messageURL" => "https://www.dingtalk.com/",
                "picURL" => "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1567105217584&di=4c91fefc045f54267edcf8c544e3bd3b&imgtype=0&src=http%3A%2F%2Fk.zol-img.com.cn%2Fdcbbs%2F16420%2Fa16419096_s.jpg"
            ],
            [
                "title" => "时代的火车向前开2",
                "messageURL" => "https://www.dingtalk.com/",
                "picURL" => ""
            ]
        ]
    ],
    "msgtype" => "feedCard"
]);
如果觉得我的文章对你有用,可以赞助本站,使本站更好的发展