钉钉机器人利用PHP推送消息

钉钉机器人利用PHP推送消息

最近业务上的定时需要进行监控并推送监控信息到钉钉特此记录一下

A.建立机器人

1.进入钉钉的PC客户端群聊,点击右侧的群助手,添加一个机器人

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.选择webhook自定义机器人在这里插入图片描述
3.根据说明建立自己的机器人,添加好安全验证,我此处选用的是ip白名单的验证,复制hook地址,这就是我们消息推送的地址
在这里插入图片描述

B.PHP推动消息的类
<?php

/**
 * Created by PhpStorm.
 * User: ASHIN
 * Date: 2019/12/6
 * Time: 10:20
 */
Class DingDing
{
    private $robot_url = 'https://oapi.dingtalk.com/robot/send?access_token=********';

    public function __construct($robot_url = null)
    {
        if (!is_null($robot_url)) {
            $this->robot_url = $robot_url;
        }
    }

    public function textMsgSend($msg, $mobile = [])
    {
        $msgJson = json_encode([
            'msgtype' => 'text',
            'text' => [
                "content" => $msg
            ],
            'at' => [
                'atMobiles' => $mobile,
                'isAtAll' => false
            ]
        ]);
        $result = $this->sendRequest($msgJson);
        return $result;
    }

    public function linkMsgSend($title, $text, $messageUrl)
    {
        // link类型
        $msgJson = json_encode([
            "msgtype" => "link",
            "link" => [
                "text" => $text,
                "title" => $title,
                "picUrl" => "",
                "messageUrl" => $messageUrl
            ]
        ]);
        $result = $this->sendRequest($msgJson);
        return $result;
    }

    public function markdownMsgSend($title, $text)
    {
        // markdown类型
        $msgJson = json_encode([
            "msgtype" => "markdown",
            "markdown" => [
                "title" => $title,
                "text" => $text
            ]
        ]);
        $result = $this->sendRequest($msgJson);
        return $result;
    }

    public function actionCardMsgSend($title, $text, $messageUrl)
    {
        // 整体跳转ActionCard类型
        $msgJson = json_encode([
            "msgtype" => "actionCard",
            "actionCard" => [
                "title" => $title,
                "text" => $text,
                "hideAvatar" => "0",
                "btnOrientation" => "0",
                "singleTitle" => "阅读全文",
                "singleURL" => $messageUrl
            ]
        ]);
        $result = $this->sendRequest($msgJson);
        return  $result;
    }

    public function singleActionCardMsgSend($title, $text, $success_url, $fail_url, $mobile = [])
    {
        // 独立跳转ActionCard类型
        $msgJson = json_encode([
            "actionCard" => [
                "title" => $title,
                "text" => $text,
                "hideAvatar" => "0",
                "btnOrientation" => "0",
                "btns" => [
                    [
                        "title" => "内容不错",
                        "actionURL" => $success_url
                    ],
                    [
                        "title" => "不感兴趣",
                        "actionURL" => $fail_url
                    ]
                ]
            ],
            "msgtype" => "actionCard"
        ]);
        $result = $this->sendRequest($msgJson);
        return $result;
    }

    public function feedCardMsgSend($links = [], $mobile = [])
    {
        // FeedCard类型
        $msgJson = json_encode([
            "msgtype" => "feedCard",
            "feedCard" => [
                "links" => $links
            ],
//                "links" => [
//            [
//                "title" => "",//标题1
//                "messageURL" => "",//链接1跳转url
//                "picURL" => ""//图片1url
//            ],
//            [
//                "title" => "",//标题2
                "messageURL" => "",//链接2跳转url
                "picURL" => ""//图片2url
//            ]
//        ]
        ]);
        $result = $this->sendRequest($msgJson);
        return $result;
    }


    public function sendRequest($json)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->robot_url);
        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, $json);
        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;
    }
}
C.各个消息类型的演示及对应的消息显示
text类型
$model = new DingDing('https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx');

$text = 'test';
$mobile = ['1xxxxxxx8'];
$res = $model->textMsgSend($text,$mobile);
echo $res;

在这里插入图片描述

link类型
$model = new DingDing('https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx');
$title = '这里是link的title头';
$text = '点我点我  我是text';
$messageUrl = 'https://www.baidu.com';
$res = $model->linkMsgSend($title, $text, $messageUrl);
echo $res;

在这里插入图片描述

markdown类型
$model = new DingDing('https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx');
$title = '这里是markdown的title头';
$text = '### 测试markdown
        内容内容内容内容内容';
$res = $model->markdownMsgSend($title, $text);
echo $res;

在这里插入图片描述

action类型(点击阅读全文跳转地址)
$model = new DingDing('https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx');
$title = '这里是action跳转的title头';
$text = '![screenshot](https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575615851507&di=70a3ed2ff8f2e9450cd24e6e7a819455&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D3319213599%2C3689238169%26fm%3D214%26gp%3D0.jpg)
我是内容';
$messageUrl = 'https://www.baidu.com';
$res = $model->actionCardMsgSend($title, $text, $messageUrl);
echo $res;

在这里插入图片描述

single_action类型(独立跳转地址)
$model = new DingDing('https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx');
$title = '这里是action跳转的title头';
$text = '![screenshot](https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575615851507&di=70a3ed2ff8f2e9450cd24e6e7a819455&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D3319213599%2C3689238169%26fm%3D214%26gp%3D0.jpg)
我是内容';
$success_url = 'https://www.baidu.com';
$fail_url = 'https://www.baidu.com';
$res = $model->singleActionCardMsgSend($title, $text, $success_url, $fail_url);
echo $res;

在这里插入图片描述

feedCard
$model = new DingDing('https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx');
$link = [
    [
        "title" => "标题1",
        "messageURL" => "https://www.baidu.com",//链接1跳转url
        "picURL" => "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575615851507&di=70a3ed2ff8f2e9450cd24e6e7a819455&imgtype=jpg&src=http%3A%2F%2Fimg4.imgtn.bdimg.com%2Fit%2Fu%3D3319213599%2C3689238169%26fm%3D214%26gp%3D0.jpg"//图片1url
    ],
    [
        "title" => "标题2",//
        "messageURL" => "https://www.dingtalk.com",//链接2跳转url
        "picURL" => "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1575615850939&di=cde87c074cd1ea5ca22a968b24d15217&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201604%2F18%2F20160418090625_hdktZ.jpeg"//图片2url
    ],
    [
        "title" => "标题3",//
        "messageURL" => "https://www.dingtalk.com",//链接3跳转url
        "picURL" => ""//图片3url
    ]
];
$res = $model->feedCardMsgSend($link);
echo $res;

在这里插入图片描述

D.注意事项

1.每个机器人每分钟最多发送20条。
2.POST请求中的字符集编码必须设置成UTF-8

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值