钉钉机器人发送自定义消息 PHP 干货

调用类

       $model = new DingListenService;
       $username = 你好;
       $content =  " @13XXXXXXXX \n\n #### 【<font color='red'>红色字体</font>】客户:".$username;
       $model->Markdown($content);

1:注意换行 是 \n\n
2:圈人 AT 类要,$content 内容也要 才生效
3:字体是这样 红色字体
4:1个机器人 1分钟 20条,超出会限流量10分钟

发送类

<?php
declare (strict_types=1);
namespace app\common\service\dingListen;
class DingListen
{
    public $listenToken = "xxxxxxx";  //测试机器人token
    public $listenSecret = "xxxxxxx";  // 测试机器人secret
    /**
     * 请求地址
     * @var string $url
     * */
    public $url = 'https://oapi.dingtalk.com/robot/send';

    /**
     * 请求地址
     * @var string $timestamp 13位毫秒级
     * */
    public $timestamp;

    /**
     * 加签
     * @var string $sign
     * */
    public $sign;

    /**
     * webhookurl
     * @var string $webhookurl
     * */
    public $webhookurl;


    public function __construct(){
        //时间戳
        $this->timestamp = time() * 1000;
        //加签
        $str = $this->timestamp."\n". $this->listenSecret;
        $sign =  hash_hmac("sha256",$str ,$this->listenSecret,true);
        $sign = base64_encode($sign);
        $this->sign = $sign ;
        //返回链接
        $webhookurl = $this->url.'?access_token='.$this->listenToken. '&timestamp='.$this->timestamp.'&sign='.urlencode($this->sign);
        $this->webhookurl = $webhookurl;
    }

    //发送text文本信息
    public function text($content = '', $mobiles = [], $userIds = [], $isAtAll = true)
    {
        $sendContent = [
            "msgtype" => "text",
            "text" => [
                "content" => $content
            ],
            "at" => [
                "atMobiles" => $mobiles,
                "atUserIds" => $userIds,   //不是钉钉管理员无法获取userIds
                "isAtAll" => $isAtAll
            ]
        ];
        $res = $this->_curl_post_json($this->webhookurl, $sendContent);
        return $res;
    }

    public function Markdown($content = '', $title="消息通知", $mobiles = ["13XXXXXXXX"], $userIds = [], $isAtAll = true)
    {
        $sendContent = [
            "msgtype" => "markdown",
            "markdown"=> [
                "title"=> $title,
                "text"=> $content
             ],
            "at" => [
                "atMobiles" => $mobiles,
                "atUserIds" => $userIds,   //不是钉钉管理员无法获取userIds
                "isAtAll" => $isAtAll
            ]
        ];

        $res = $this->_curl_post_json($this->webhookurl, $sendContent);
        return $res;
    }

    //发送json数据
    public function _curl_post_json($url, $data = array())
    {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, 320));
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
发送图片到机器人,可以使用机器人的自定义机器人API接口。下面是一个发送图片的示例代码: ```python import requests url = 'https://oapi.dingtalk.com/robot/send?access_token=<your_access_token>' headers = { 'Content-Type': 'application/json;charset=utf-8' } data = { "msgtype": "image", "image": { "media_id": "<media_id>" } } response = requests.post(url, headers=headers, json=data) print(response.text) ``` 其中,`<your_access_token>` 是你的机器人的访问令牌,`<media_id>` 是你要发送的图片的media_id。可以使用提供的上传图片接口(https://oapi.dingtalk.com/media/upload?access_token=ACCESS_TOKEN&type=image)上传图片并获取media_id。 除了发送图片,还可以发送文本、链接、Markdown等不同类型的消息。下面是一个发送Markdown消息的示例代码: ```python import requests url = 'https://oapi.dingtalk.com/robot/send?access_token=<your_access_token>' headers = { 'Content-Type': 'application/json;charset=utf-8' } data = { "msgtype": "markdown", "markdown": { "title": "标题", "text": "## 标题二 \n\n **加粗文本** \n\n *斜体文本* \n\n [链接](http://www.baidu.com)" } } response = requests.post(url, headers=headers, json=data) print(response.text) ``` 其中,`title` 是消息标题,`text` 是Markdown格式的消息内容。 以上是发送图片和Markdown消息的示例代码,你可以根据自己的需求修改代码和消息类型。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值