php使用钉钉机器人消息

//设置关键字 测试环境

$url = 'https://oapi.dingtalk.com/robot/send?access_token=bbb034d110e485fa9ec457b5446eab2270778c93100752d609750c425223868b';
$msg = '测试环境测试消息';

try{
    new  cat();
}catch (Throwable $e){
    $result = [
        "> 错误信息:" .addslashes($e->getMessage()),
        "> 错误文件:" . addslashes($e->getFile()),
        "> 错误行号:" . addslashes($e->getLine()),
        "> ###### 日期:" . date('Y-m-d H:i:s'),
    ];
    send_dingtalk_err($url,join('  \n', $result));

}

/*
* 发送叮叮机器人错误消息
*/
function send_dingtalk_err($url,$msg) {
    send_dingtalk($url, '错误通知(' . '测试环境' . ')', '测试环境'.$msg);
}

/*
* 发送叮叮机器人消息
*/
function send_dingtalk($url, $title, $text, $at = '', $all = 'true') {
    try {
        http_post($url, '{"msgtype":"markdown","markdown":{"title":"' . $title . '","text":"#### ' . $title . '  \n' . $text . '"},"at":{"atMobiles":"' . $at . '","isAtAll":' . $all . '}}');
    } catch (Exception $e) {
    }
}


function http_post($url, $json, $timeout = 120, &$error = null) {
    return curl_request($url, $json, 'POST', $timeout, ['Content-Type: application/json', 'Content-Length: ' . strlen($json)], false, $error);
}

/*
 * 发送后请求
 * curl_request($url, $json, 'POST', 30, array('Content-Type: application/json', 'Content-Length: ' . strlen($json)), false,$error)
 * curl_request($url, $data, 'POST', 30, null, false, $error)
 * curl_request($url, null, 'GET', 30, null, false, $error)
 */
function curl_request($url, $vars, $method = "GET", $timeout = 120, $header = null, $ssl = false, &$error = null) {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $ssl);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $ssl);

    if (strtoupper($method) == "POST") {
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $vars);
    } else {
        curl_setopt($curl, CURLOPT_POST, false);
    }
    if (!is_null($header)) {
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    }
    $data = curl_exec($curl);
    if ($data === false) {
        $error = curl_errno($curl);
        curl_close($curl);

        return false;
    } else {
        curl_close($curl);
        var_dump($data);
        return $data;
    }
}
  • 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、付费专栏及课程。

余额充值