当前位置:网站首页>Actual combat simulation │ real time error alarm of enterprise wechat robot

Actual combat simulation │ real time error alarm of enterprise wechat robot

2022-06-13 10:16:00 InfoQ

One 、 Create robots

  • Selection group
     ► 
    Right click
     ► 
    Manage chat messages
     ► 
    Add swarm robots
  • Improve basic robot information , Including head portrait 、 Name, etc
 Add swarm robots
 Add robot button
 Perfect robot information

Two 、 Robot configuration description

  • Every created robot has a unique  
    webhook 
    Address
  • Be sure to protect your  
    webhook 
    Address , If there is a leak , You can remove the robot , Create a new one to deal with
  • Click on  
    webhook 
    Address , You can see the documentation , You can also configure common push messages
  • The custom push message , There is a detailed description in the robot configuration description column , But you need to develop it yourself
 robot webhook
 Robot configuration documentation

3、 ... and 、 Robot information push

  • Current custom robot  
    Supporting text (text)、markdown(markdown)、 picture (image)、 Image & Text (news)
    Four message types
  • We just need to follow its documentation , Send a message of the specified type to  
    webhook 
    Address to push messages
//  Text message type
{
 "msgtype": "text",
 "text": {
 "content": " Today's weather in Guangzhou :29 degree , Most of it is cloudy , Probability of rainfall :60%",
 "mentioned_list":["wangqing","@all"],
 "mentioned_mobile_list":["13800001111","@all"]
 }
}

// markdown Message type
{
 "msgtype": "markdown",
 "markdown": {
 &quot;content&quot;: &quot; Real time new user feedback <font color=\&quot;warning\&quot;>132 example </font>, Please pay attention to .\n
 > type :<font color=\&quot;comment\&quot;> User feedback </font>
 > General user feedback :<font color=\&quot;comment\&quot;>117 example </font>
 >VIP User feedback :<font color=\&quot;comment\&quot;>15 example </font>&quot;
 }
}

//  Picture message type
{
 &quot;msgtype&quot;: &quot;image&quot;,
 &quot;image&quot;: {
 &quot;base64&quot;: &quot;DATA&quot;,
 &quot;md5&quot;: &quot;MD5&quot;
 }
}

//  Graphic message type
{
 &quot;msgtype&quot;: &quot;news&quot;,
 &quot;news&quot;: {
 &quot;articles&quot; : [
 {
 &quot;title&quot; : &quot; Receiving gifts for Mid Autumn Festival &quot;,
 &quot;description&quot; : &quot; This year's Mid Autumn Festival, the company will give you a gift &quot;,
 &quot;url&quot; : &quot;www.qq.com&quot;,
 &quot;picurl&quot; : &quot;http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png&quot;
 }
 ]
 }
}

Four 、 Error alert push

  • Here we use  
    Thinkphp 
    Framework for example , Integrate error alerts into the project , Realize real-time error push
  • First, in the  
    config 
    Add error handling classes to the configuration file , Which file to execute to handle errors
  • When the configuration is complete , When a project encounters an error , The program will go through the specified file to process
  • Then improve the error push warning logic in the file , Generally, error pre-warning uses  
    markdown 
    Type
'exception_handle' => '\\app\\common\\exception\\WorkWx',
<?php
namespace app\common\exception;

use Exception;
use itbdw\Ip\IpLocation;
use app\common\util\Helper;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;

class WorkWx extends Handle
{
 const WEBHOOK = ' Fill in your own webhook Address ';

 public function render(Exception $e)
 {
 $clientIP = Helper::getClientIp();
 $clientAddress = IpLocation::getLocation($clientIP);
 unset($clientAddress['ip']);
 $ipAddress = implode('-', $clientAddress);

 //  Parameter validation error
 if ($e instanceof ValidateException) {
 $data = [
 'msgtype' => 'markdown',
 'markdown' => [
 'content' => &quot; come from  **<font color=\&quot;info\&quot;> eye </font>**  A warm reminder of , Please pay attention to .
 >** describe :** <font color=\&quot;comment\&quot;> Parameter validation error </font>
 >** End IP:** <font color=\&quot;comment\&quot;>{$clientIP}</font>
 >** Address :** <font color=\&quot;comment\&quot;>{$ipAddress}</font>
 >** state :** <font color=\&quot;comment\&quot;>{$e->getCode()}</font>
 >** Row number :** <font color=\&quot;comment\&quot;>{$e->getLine()}</font>
 >** file :** <font color=\&quot;red\&quot;>{$e->getFile()}</font>
 >** Tips :** <font color=\&quot;warning\&quot;>{$e->getError()}</font>
 >** Information :** <font color=\&quot;warning\&quot;>{$e->getMessage()}</font>&quot;
 ]
 ];

 return Helper::postCurl(self::WEBHOOK, json_encode($data));
 }

 //  Request exception
 if ($e instanceof HttpException) {
 $data = [
 'msgtype' => 'markdown',
 'markdown' => [
 'content' => &quot; come from  **<font color=\&quot;info\&quot;> eye </font>**  A warm reminder of , Please pay attention to .
 >** describe :** <font color=\&quot;comment\&quot;> Request exception </font>
 >** End IP:** <font color=\&quot;comment\&quot;>{$clientIP}</font>
 >** Address :** <font color=\&quot;comment\&quot;>{$ipAddress}</font>
 >** state :** <font color=\&quot;comment\&quot;>{$e->getCode()}</font>
 >** Row number :** <font color=\&quot;comment\&quot;>{$e->getLine()}</font>
 >** file :** <font color=\&quot;red\&quot;>{$e->getFile()}</font>
 >** Information :** <font color=\&quot;warning\&quot;>{$e->getMessage()}</font>&quot;
 ]
 ];

 return Helper::postCurl(self::WEBHOOK, json_encode($data));
 }


 //  Other errors are handled by the system
 return parent::render($e);
 }
}

 Error warning effect preview
原网站

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206131011168206.html