第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

釘釘機(jī)器人webhook 對(duì)接 ThinkPHP3.2 實(shí)現(xiàn)Bug告警通知

標(biāo)簽:
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

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消