首页 >php框架 >Laravel >Laravel能用钉钉接收系统警报!

Laravel能用钉钉接收系统警报!

藏色散人
藏色散人转载
2022-01-13 10:24:152036浏览

下面由Laravel教程栏目给大家介绍Laravel框架怎么使用钉钉聊天机器人接收系统警报 ,希望对大家有所帮助!

钉钉提供的聊天机器人功能,可以使用自定义机器人的 webhook 我们可以将错误日志及时的推送到相关人员。

1.获取钉钉聊天机器人 Access Token

在使用本扩展之前,你需要ding-doc.dingtalk.com/doc#/servera...获取相关信息。

2.所需知识

  • Carbon
  • GuzzleHttp

3.编写代码

<?php

namespace 所在命名空间;

use CarbonCarbon;
use GuzzleHttpClient;
use GuzzleHttpExceptionGuzzleException;
use IlluminateSupportFacadesLog;

class Ding
{
    CONST BASE_URI   = "https://oapi.dingtalk.com/";
    CONST NOTICE_URL = [
        "robot/send?access_token=*******",
        &#39;robot/send?access_token=*********&#39;
    ];

    /**
     * Description: 钉钉文本通知
     * @param        $data
     * @param array $at
     * @param bool $isAtAll
     * @throws GuzzleException
     */
    public static function text($data, $at = [], $isAtAll = false)
    {
        $notice_url = self::NOTICE_URL[rand(0, count(self::NOTICE_URL) - 1)];
        $data = "#### 异常触发时间".Carbon::now()->format(&#39;Y-m-d H:i:s&#39;)."n".$data;
        $pushData = [
            &#39;msgtype&#39; => &#39;text&#39;,
            &#39;text&#39;    => [&#39;content&#39; => $data],
            &#39;at&#39;      => [&#39;atMobiles&#39; => $at, &#39;isAtAll&#39; => $isAtAll],
        ];
        self::getData($notice_url, $pushData);
    }

    /**
     * Description: 钉钉预警markdown
     * @param        $data
     * @param array $at
     * @param bool $isAtAll
     * @throws GuzzleException
     */
    public static function markdown($data, $at = [], $isAtAll = false)
    {
        $notice_url = self::NOTICE_URL[rand(0, count(self::NOTICE_URL) - 1)];
        $data[&#39;text&#39;] = "#### 异常触发时间".Carbon::now()->format(&#39;Y-m-d H:i:s&#39;)."n".$data[&#39;text&#39;];
        $pushData = [
            &#39;msgtype&#39;  => &#39;markdown&#39;,
            &#39;markdown&#39; => $data,
            &#39;at&#39;       => [&#39;atMobiles&#39; => $at, &#39;isAtAll&#39; => $isAtAll],
        ];
        self::getData($notice_url, $pushData);
    }

    /**
     * Description: 钉钉预警处理
     * @param $notice_url
     * @param $pushData
     * @throws GuzzleException
     */
    public static function getData($notice_url, $pushData)
    {
        try {
            $client = new Client([&#39;base_uri&#39; => self::BASE_URI, &#39;timeout&#39; => 3.0]);
            $res    = $client->request(&#39;POST&#39;,
                $notice_url,
                [&#39;headers&#39; => [&#39;Content-Type&#39; => &#39;application/json;charset=utf-8&#39;,],
                    &#39;json&#39;    => $pushData,
                    &#39;verify&#39;  => false]);
            $result = json_decode($res->getBody()->getContents(), true);

            if ($result[&#39;errmsg&#39;] != &#39;ok&#39; && $result[&#39;errcode&#39;] != 0) {
                Log::debug(&#39;钉钉推送数据失败&#39;, [&#39;result&#39; => $result,&#39;url&#39;=>$notice_url]);
            }
        }
        catch (Exception $e) {
            Log::debug(&#39;钉钉推送异常!&#39;,[&#39;data&#39;=>$pushData,&#39;url&#39;=>$notice_url,&#39;env&#39;=>App::environment()]);
        }
    }
}

4.其他建议

推荐使用redis队列进行操作,具体需要Event,Listener,redis相关操作具体可查看laravel官方文档

推荐:《最新的五个Laravel视频教程

以上是Laravel能用钉钉接收系统警报!的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:learnku.com。如有侵权,请联系admin@php.cn删除