前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >php钉钉机器人推送消息api

php钉钉机器人推送消息api

作者头像
Qicloud
发布2022-01-27 19:51:45
8790
发布2022-01-27 19:51:45
举报
文章被收录于专栏:七云博客七云博客

最近内测那个QQHOO!K机器人挺火的,突发奇想写个小玩意直接提交很香,上手的时候tx大大不知因为个啥不给玩了,钉钉有个webhook,于是就有了下边这个玩应

随手写的,代码又臭又长希望有大佬给指正下

代码语言:javascript
复制
PHP复制PHP复制PHP复制    <?php
    /**
     * php 钉钉推送dome
     * User: 神奇的七云
     * Date: 2020/4/15
     * Time: 15:04
     */
    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
    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);
        //$info = curl_getinfo($ch);
        //var_dump($info);
        curl_close($ch);
        return $data;
    }
    $webhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxx";
    /**
     * @var mixed $content  正文
     * @var mixed $title  标题
     * @var mixed $picUrl 图片链接
     * @var mixed $messageUrl 跳转链接
     */
    $time = date("Y-m-d H:i:s", time());
    $content = ($_GET['content']);
    $title = ($_GET['title']);
    $picUrl = ($_GET['picUrl']);
    $messageUrl = ($_GET['messageUrl']);
    $h1 = $_GET['h1'];
    if ($key == md5(md5(date("Ymdhis", time()) - 66547997 * "66547997"))) {
        if ($_GET['type'] == 'text') {
            // text类型 
            //?type=text&content=正文
            $textString = json_encode([
                'msgtype' => 'text',
                'text' => [
                    "content" => $content
                ],
            ]);
            $result = request_by_curl($webhook, $textString);
            echo $result;
        } elseif ($_GET['type'] == 'link') {
            // link类型
            //?type=text&content=正文&title=标题&picUrl=图片链接&messageUrl跳转链接
            $textString = json_encode([
                "msgtype" => "link",
                "link" => [
                    "text" => $content,
                    "title" => $title,
                    "picUrl" => $picUrl,
                    "messageUrl" => $messageUrl,
                ]
            ]);
            $result = request_by_curl($webhook, $textString);
            echo $result;
        } elseif ($_GET['type'] == "markdown") {
            //markdown类型
            //?type=text&content=正文&title=标题&picUrl=图片链接&messageUrl跳转链接
            $textString = json_encode([
                "msgtype" => "markdown",
                "markdown" => [
                    "title" => $title,
                    "text" => "#### $title,\n" .
                        "> $content\n\n" .
                        "> ![screenshot]($picUrl)\n" .
                        "> ###### $time 发布 \n"
                ],
                "at" => [
                    "atMobiles" => [
                        "156xxxx8827",
                        "189xxxx8325"
                    ],
                    "isAtAll" => false
                ]
            ]);
            $result = request_by_curl($webhook, $textString);
            echo $result;
        } elseif ($_GET['type'] == "ActionCard") {
            // 整体跳转ActionCard类型
            //?type=text&content=正文&title=标题&picUrl=图片链接&messageUrl跳转链接&h1=一级标题
            $textString = json_encode([
                "actionCard" => [
                    "title" => $title,
                    "text" => "![]($picUrl) 
    ### $h1 
    $content",
                    "hideAvatar" => "0",
                    "btnOrientation" => "0",
                    "singleTitle" => "阅读全文",
                    "singleURL" => "$messageUrl"
                ],
                "msgtype" => "actionCard"
            ]);
            $result = request_by_curl($webhook, $textString);
            echo $result;
        } else {
            $result = request_by_curl($webhook, $textString);
            echo $result;
        }
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-04-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档