2

实战模拟│企业微信机器人实时报错预警

 2 years ago
source link: https://blog.51cto.com/cxyliangzai/5377632
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

一、创建机器人

  • ​选择群​​​ ►​​右键​​​ ►​​管理聊天信息​​​ ►​​添加群机器人​
  • 完善机器人基本信息,包括头像、名称等
实战模拟│企业微信机器人实时报错预警_企业微信
实战模拟│企业微信机器人实时报错预警_实时预警_02
实战模拟│企业微信机器人实时报错预警_推送_03

二、机器人配置说明

  • 创建好的机器人都有一个唯一的​​webhook ​​地址
  • 一定要保护好自己的​​webhook ​​地址,如果一旦泄露,可以通过移除机器人,再重新创建一个去处理
  • 点击​​webhook ​​地址,可以看到文档说明,也可以进行普通的推送消息配置
  • 其中的自定义推送消息,在机器人配置说明栏目里面有详细说明,但是需要自己开发
实战模拟│企业微信机器人实时报错预警_企业微信_04
实战模拟│企业微信机器人实时报错预警_机器人_05

三、机器人信息推送

  • 当前自定义机器人​​支持文本(text)、markdown(markdown)、图片(image)、图文(news)​​四种消息类型
  • 我们只要根据它的文档说明,将指定类型的消息发送给​​webhook ​​地址即可实现消息推送
// 文本消息类型
{
"msgtype": "text",
"text": {
"content": "广州今日天气:29度,大部分多云,降雨概率:60%",
"mentioned_list":["wangqing","@all"],
"mentioned_mobile_list":["13800001111","@all"]
}
}

// markdown消息类型
{
"msgtype": "markdown",
"markdown": {
"content": "实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n
>类型:<font color=\"comment\">用户反馈</font>
>普通用户反馈:<font color=\"comment\">117例</font>
>VIP用户反馈:<font color=\"comment\">15例</font>"
}
}

// 图片消息类型
{
"msgtype": "image",
"image": {
"base64": "DATA",
"md5": "MD5"
}
}

// 图文消息类型
{
"msgtype": "news",
"news": {
"articles" : [
{
"title" : "中秋节礼品领取",
"description" : "今年中秋节公司有豪礼相送",
"url" : "www.qq.com",
"picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
}
]
}
}

四、错误预警推送

  • 这里以​​Thinkphp ​​框架为例,将错误预警整合到项目中,实现实时错误推送
  • 首先在​​config ​​配置文件添加错误处理类,执行哪个文件来处理错误
  • 配置完成后,项目只要遇到错误,程序都会走指定的文件去处理
  • 然后在该文件中完善错误推送预警逻辑即可,一般错误预警都是用​​markdown ​​类型进行推送
'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 = '填写你自己的webhook地址';

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

// 参数验证错误
if ($e instanceof ValidateException) {
$data = [
'msgtype' => 'markdown',
'markdown' => [
'content' => "来自 **<font color=\"info\">天眼</font>** 的温馨提醒,请相关同事注意。
>**描述:** <font color=\"comment\">参数验证错误</font>
>**端IP:** <font color=\"comment\">{$clientIP}</font>
>**地址:** <font color=\"comment\">{$ipAddress}</font>
>**状态:** <font color=\"comment\">{$e->getCode()}</font>
>**行数:** <font color=\"comment\">{$e->getLine()}</font>
>**文件:** <font color=\"red\">{$e->getFile()}</font>
>**提示:** <font color=\"warning\">{$e->getError()}</font>
>**信息:** <font color=\"warning\">{$e->getMessage()}</font>"
]
];

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

// 请求异常
if ($e instanceof HttpException) {
$data = [
'msgtype' => 'markdown',
'markdown' => [
'content' => "来自 **<font color=\"info\">天眼</font>** 的温馨提醒,请相关同事注意。
>**描述:** <font color=\"comment\">请求异常</font>
>**端IP:** <font color=\"comment\">{$clientIP}</font>
>**地址:** <font color=\"comment\">{$ipAddress}</font>
>**状态:** <font color=\"comment\">{$e->getCode()}</font>
>**行数:** <font color=\"comment\">{$e->getLine()}</font>
>**文件:** <font color=\"red\">{$e->getFile()}</font>
>**信息:** <font color=\"warning\">{$e->getMessage()}</font>"
]
];

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


// 其他错误交给系统处理
return parent::render($e);
}
}
实战模拟│企业微信机器人实时报错预警_机器人_06

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK