Home > PHP Framework > Laravel > body text

Laravel can use DingTalk to receive system alerts!

藏色散人
Release: 2022-01-17 10:17:15
forward
2002 people have browsed it

The following column Laravel Tutorial will introduce to you how to use the DingTalk chatbot to receive system alerts in the Laravel framework. I hope it will be helpful to everyone!

The chatbot function provided by DingTalk, you can use the webhook of the custom bot, and we can push the error log to the relevant personnel in a timely manner.

1. Obtain DingTalk Chatbot Access Token

Before using this extension, you need to obtain relevant information from ding-doc.dingtalk.com/doc#/servera...

2. Required knowledge

  • Carbon
  • GuzzleHttp

3.Writing code

<?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()]);
        }
    }
}
Copy after login

4. Other suggestions

It is recommended to use redis queue for operation. For details about Event, Listener, and redis-related operations, please check the laravel official documentation

Recommendation: "The latest five Laravel Video tutorial

The above is the detailed content of Laravel can use DingTalk to receive system alerts!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!