php 发送钉钉群消息

 分享一个用php写的发送钉钉群消息的类

支持钉钉机器人安全设置中的加签方式

支持text和link两种消息推送方式

<?php
class Send_DingDing {
        var $ifsecret;
        var $ifat;
        var $secret;
        var $webhook;
        var $message;
        var $msgtype="text";
        var $atlist;
        var $title;
        var $picUrl;
        var $messageUrl;
        var $text;
        public function ifSecret($ifsecret="false"){
                $this->ifsecret=$ifsecret;
        }
        public function setSecret($secret){
                $this->secret=$secret;
        }
        public function setWebhook($webhook){
                $this->webhook=$webhook;
        }
        public function setMessage($message){
                $this->message=$message;
        }
        public function setMsgtype($msgtype){
                $this->msgtype=$msgtype;
        }
        public function ifAt($ifat="false"){
                $this->ifat=$ifat;
        }
        public function setAtlist($atlist){
                $this->atlist=$atlist;
        }
        public function setTitle($title){
                $this->title=$title;
        }
        public function setPicUrl($picUrl){
                $this->picUrl=$picUrl;
        }
        public function setMessageUrl($messageUrl){
                $this->messageUrl=$messageUrl;
        }
        public function setText($text){
                $this->text=$text;
        }

        private function getWebhook(){
                $webhook=$this->webhook;
                $ifsecret=$this->ifsecret;
                $secret=$this->secret;
                if("$ifsecret" == "true"){
                        list($s1, $s2) = explode(' ', microtime());
                        $timestamp = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
                        $data = $timestamp . "\n" . $secret;
                        $signStr = base64_encode(hash_hmac('sha256', $data, $secret,true));
                        $signStr = utf8_encode(urlencode($signStr));
                        $webhook .= "&timestamp=$timestamp&sign=$signStr";
                }
                return $webhook;
        }
        public function getData(){
                $msgtype=$this->msgtype;
                $message=$this->message;
                $title=$this->title;
                $picUrl=$this->picUrl;
                $text=$this->text;
                $messageUrl=$this->messageUrl;
                $ifat=$this->ifat;
                $atlist=$this->atlist;
                if("$msgtype" == "text"){
                        if("$ifat" == "true"){
                                $data = array ('msgtype' => 'text','text' => array ('content' => $message),'at' => array('atMobiles' => $atlist),'isAtAll' => "false");
                        }else{
                                $data = array ('msgtype' => 'text','text' => array ('content' => $message));
                        }
                }elseif("$msgtype" == "link"){
                        $data = array('msgtype' => 'link','link' => array('text' => $text,'title'=>$title,'picUrl'=>$picUrl,'messageUrl'=>$messageUrl));
                }
                $data_string = json_encode($data);
                return $data_string;
        }
        public function send(){
                $webhook=$this->getWebhook();
                $data_string=$this->getData();
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $webhook);
                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, $data_string);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $data = curl_exec($ch);
                curl_close($ch);
                return $data;
                echo $data;
        }


}



$webhook="https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxx";
$ifsecret="true";
$secret="xxxxxxxx";
#link
#/*
$msgtype="test msg";
$text="test text";
$title="test title";
$messageUrl="https://www.baidu.com";
$send_ding=new Send_DingDing();
$send_ding->setWebhook($webhook);
$send_ding->ifSecret($ifsecret);
$send_ding->setSecret($secret);
$send_ding->setMsgtype($msgtype);
$send_ding->setText($text);
$send_ding->setTitle($title);
$send_ding->setMessageUrl($messageUrl);
$send_ding->send();
#*/
#text
/*
$message="test msg";
$ifat="true";
$at_arr=Array("182XXXXXXXX");
print_r($at_arr);
$send_ding=new Send_DingDing();
$send_ding->setWebhook($webhook);
$send_ding->ifSecret($ifsecret);
$send_ding->setSecret($secret);
$send_ding->setMessage($message);
$send_ding->ifAt($ifat);
$send_ding->setAtlist($at_arr);
$send_ding->send();
*/
?>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是钉钉机器人常见的消息类型示例: 1. 文本消息: ``` { "msgtype": "text", "text": { "content": "Hello, World!" }, "at": { "atMobiles": [ "13800000000" ], "isAtAll": false } } ``` 2. Markdown 消息: ``` { "msgtype": "markdown", "markdown": { "title": "钉钉机器人使用指南", "text": "# 钉钉机器人使用指南\n\n欢迎使用钉钉机器人,以下是常用的消息类型示例:\n\n* 文本消息\n* Markdown 消息\n* 图片消息\n* 链接消息\n* Markdown 链接消息\n* ActionCard 消息\n* FeedCard 消息" }, "at": { "atMobiles": [ "13800000000" ], "isAtAll": false } } ``` 3. 图片消息: ``` { "msgtype": "image", "image": { "media_id": "@lADOADgEbXYCk0jNAszN", "pic_url": "https://www.example.com/image.jpg" } } ``` 4. 链接消息: ``` { "msgtype": "link", "link": { "text": "钉钉开放平台", "title": "钉钉开放平台", "picUrl": "", "messageUrl": "https://open.dingtalk.com/" } } ``` 5. Markdown 链接消息: ``` { "msgtype": "markdown", "markdown": { "title": "钉钉机器人使用指南", "text": "# 钉钉机器人使用指南\n\n欢迎使用钉钉机器人,以下是常用的消息类型示例:\n\n* [文本消息](https://www.example.com/text)\n* [Markdown 消息](https://www.example.com/markdown)\n* [图片消息](https://www.example.com/image)\n* [链接消息](https://www.example.com/link)\n* Markdown 链接消息\n* ActionCard 消息\n* FeedCard 消息" }, "at": { "atMobiles": [ "13800000000" ], "isAtAll": false } } ``` 6. ActionCard 消息: ``` { "msgtype": "actionCard", "actionCard": { "title": "钉钉机器人使用指南", "text": "欢迎使用钉钉机器人,以下是常用的消息类型示例:", "hideAvatar": "0", "btnOrientation": "0", "btns": [ { "title": "文本消息", "actionURL": "https://www.example.com/text" }, { "title": "Markdown 消息", "actionURL": "https://www.example.com/markdown" }, { "title": "图片消息", "actionURL": "https://www.example.com/image" } ] } } ``` 7. FeedCard 消息: ``` { "msgtype": "feedCard", "feedCard": { "links": [ { "title": "钉钉开放平台", "messageURL": "https://open.dingtalk.com/", "picURL": "" }, { "title": "钉钉开放平台文档", "messageURL": "https://open-doc.dingtalk.com/", "picURL": "" }, { "title": "钉钉开放平台论坛", "messageURL": "https://openclub.alipay.com/index.php?c=thread&a=subforum&fid=320", "picURL": "" } ] } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值