为了账号安全,请及时绑定邮箱和手机立即绑定

钉钉机器人webhook 对接 ThinkPHP3.2 实现Bug告警通知

标签:
ThinkPHP

前言:

为什么写这篇文章?

原因如下:

项目告警多通过SMS、mail 等方式通知到相应的人员,现在钉钉出了个webhook机器人接入,自定义的机器人支持Post 告警消息到群里,支持更多可能性。

详情可以参考钉钉开发官网文档:

https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.Dq3B2b&treeId=257&articleId=105735&docType=1

下面我们来学习下 ThinkPHP3.2中实现 通过钉钉机器人webhook 发送异常信息到 群里。

一、我们新建的第三方类库DingTalk放到Vendor目录下,

在Vendor目录下新建DingTalk目录, 同时在DingTalk目录下新建  Corefunction.php文件(发送异常消息的类文件)和 GetApiData.php文件(钉钉机器人接口公用函数);

Corefunction.php 文件的内容如下:

<?php

/* *

* 钉钉机器人接口公用函数

* 详细:该类是请求、通知返回两个文件所调用的公用函数核心处理文件

* 日期:2018-01-16

* 该代码仅供学习使用,只是提供一个参考。

*/

/**

* 远程获取数据,POST模式

* @param $remote_server 机器人对应的Webhook地址

* @param $post_string   发送的内容

* return 远程输出数据

*/

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);

    curl_close($ch);

    return $data;

}

?>

Corefunction.php 文件的内容如下:

<?php

/**

* 类名:GetApiData

* 功能:钉钉机器人webhook接口请求提交类

* 该代码仅供学习使用,只是提供一个参考。

**/

    class GetApiData {

        protected $files = '';

        protected $line = '';

        protected $message = '';

        protected $trace = '';

        public function __construct($files, $line, $messag, $trace){

            $this->files   = $files;

            $this->line    = $line;

            $this->message = $message;

            $this->trace   = $trace;

        }

        public function abnormalRemind()  {

            $clientIp = getenv('REMOTE_ADDR');

            $all_message = "## 某某项目异常提醒\n\n"

                    ."> clientIp: $clientIp \n\n"

                    ."> file: $this->file \n\n"

                    ."> line: $this->line \n\n"

                    ."> message: $this->message \n\n"

                    ."> trace: $this->trace";

            $remote_server = C('DING_TALK_API');     # 从config.php 取 Webhook地址(https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx)

            $content = json_encode(markdown('异常提醒', $all_message));

            request_by_curl($remote_server , $content);

            return true;

         }


    /* *

     * 定义消息类型(markdown)及消息数据

     */

    protected function markdown($title, $message){

        $data = array (

        'msgtype' => 'markdown',

        'markdown' => array (

            'title' => $title,

            'text' => $message

           )

        );

        return $data;

    }



二、在ThinkPHP系统内置的异常模板在系统目录的 Tpl/think_exception.tpl 中调用异常类,实现 项目异常消息推送到 钉钉群; 在Tpl/think_exception.tpl 下 加入的代码如下所示:

<?php if(isset($e['file']))  { ?>

<?php

    Vendor('DingTalk.GetApiData');

    $file = $e['file'];

    $line = $e['line'];

    $trace = nl2br($e['trace']);

    $message = strip_tags($e['message']);

    $msg = new \GetApiData($file, $line, $message, $trace);

    $msg->abnormalRemind();

?>

<?php  } ?>

三、异常提醒如下图所示:


小结:

钉钉 webhook机器人可以实现消息推送到钉钉群里 , 自定义机器人支持文本(text)、连接(link)、markdown(markdown)三种消息类型。



作者:平常xin
链接:https://www.jianshu.com/p/d928340fef40

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消